HEX
Server: LiteSpeed
System: Linux premium260.web-hosting.com 4.18.0-553.45.1.lve.el8.x86_64 #1 SMP Wed Mar 26 12:08:09 UTC 2025 x86_64
User: aliazzsr (627)
PHP: 7.4.33
Disabled: NONE
Upload Files
File: /home/aliazzsr/api.crm.vqode.com/tests/api/CompanyStrictValidationCept.php
<?php

$I = new ApiTester($scenario);

$I->wantTo('perform strict company validation');

$I->login();

$I->comment('validating new company with unique name');

$faker = \Faker\Factory::create();
$companyTypes = array_map('intval', $I->grabColumnFromDatabase('company_type', 'id'));
$owners = array_map('intval', $I->grabColumnFromDatabase('user', 'id'));
$nonuniqueCompanyName = $faker->text;

$firstCompany = [
    'name' => $nonuniqueCompanyName,
    'company_type_id' => $faker->randomElement($companyTypes),
    'owner_id' => $faker->randomElement($owners),
];

$I->sendPOST('companies/validate', $firstCompany);
$I->seeResponseCodeIs(\Codeception\Util\HttpCode::OK);
$I->seeResponseIsJson();
$I->seeResponseContainsJson($firstCompany);

$I->comment('save new company with unique name');
$I->sendPOST('companies', $firstCompany);
$I->seeResponseCodeIs(\Codeception\Util\HttpCode::CREATED);

$I->comment('validate new company with non-unique name');
$secondCompany = [
    'name' => $nonuniqueCompanyName,
    'company_type_id' => $faker->randomElement($companyTypes),
    'owner_id' => $faker->randomElement($owners),
];
$I->sendPOST('companies/validate', $secondCompany);
$I->seeResponseCodeIs(\Codeception\Util\HttpCode::UNPROCESSABLE_ENTITY);
$I->seeResponseContainsJson([
    'field' => 'name',
    'message' => 'A duplicate company with the same name has been found',
]);

$I->comment('save new company with non-unique name');
$I->sendPOST('companies', $secondCompany);
$I->seeResponseCodeIs(\Codeception\Util\HttpCode::CREATED);