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);