Refactor the City, Company and their tests.
continuous-integration/drone/push Build is passing Details

main
KKlochko 2 years ago
parent a4938eb099
commit c22945926c

@ -6,6 +6,8 @@ use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use App\Models\Company;
class City extends Model
{
use HasFactory;
@ -33,12 +35,16 @@ class City extends Model
public function addCompanyId(int $company_id)
{
$this->addCompanyIds([$company_id]);
if(Company::isExist($company_id))
$this->addCompanyIds([$company_id]);
}
public function addCompanyIds(array $company_ids)
{
$companyIDs = array_merge($this->getCompanyIds(), $company_ids);
$existingCompanies = array_filter($company_ids, ['App\Models\Company', 'isExist']);
$companyIDs = array_merge($this->getCompanyIds(), $existingCompanies);
$this->companies()->sync($companyIDs);
}

@ -28,6 +28,13 @@ class Company extends Model
return $this->hasMany(Category::class);
}
public static function isExist(int $company_id): bool
{
$count = Company::where('id', $company_id)->count();
return $count != 0;
}
public static function validate_with_name(string $name)
{
$name = $name ?? '';

@ -44,7 +44,7 @@ class CityTest extends TestCase
}
}
public function testGetCompaniesIdsForEmpty()
public function testGetCompanyIdsForEmptyCity()
{
$this->setUpCity();
@ -53,7 +53,7 @@ class CityTest extends TestCase
$this->assertEquals($this->city->getCompanyIds(), []);
}
public function testAddCompaniesIdForTwoElements()
public function testAddTwoCompanyId()
{
$this->setUpCity();
$this->setUpCompanyIds();
@ -66,7 +66,7 @@ class CityTest extends TestCase
$this->assertEquals($this->city->getCompanyIds(), $this->company_ids);
}
public function testAddNonExistingCompaniesId()
public function testAddNonExistingCompanyId()
{
$this->setUpCityWithCompanies();
@ -77,7 +77,7 @@ class CityTest extends TestCase
$this->assertEquals($this->city->getCompanyIds(), $this->company_ids);
}
public function testAddNonExistingCompaniesIdFromEmpty()
public function testAddNonExistingCompanyIdForEmptyCity()
{
$this->setUpCity();
$this->company_ids = [];
@ -89,7 +89,30 @@ class CityTest extends TestCase
$this->assertEquals($this->city->getCompanyIds(), $this->company_ids);
}
public function testAddNonExistingCompaniesIdsFromEmpty()
public function testAddTwoCompanyIds()
{
$this->setUpCity();
$this->setUpCompanyIds();
$this->city->addCompanyIds($this->company_ids);
$this->assertNotNull($this->city);
$this->assertIsArray($this->city->getCompanyIds());
$this->assertEquals($this->city->getCompanyIds(), $this->company_ids);
}
public function testAddNonExistingCompanyIds()
{
$this->setUpCityWithCompanies();
$this->assertEquals($this->city->getCompanyIds(), $this->company_ids);
$this->city->addCompanyIds([23423, 1234]);
$this->assertEquals($this->city->getCompanyIds(), $this->company_ids);
}
public function testAddNonExistingCompanyIdsForEmpty()
{
$this->setUpCity();
$this->company_ids = [];
@ -101,7 +124,7 @@ class CityTest extends TestCase
$this->assertEquals($this->city->getCompanyIds(), $this->company_ids);
}
public function testRemoveCompaniesId()
public function testRemoveCompanyId()
{
$this->setUpCityWithCompanies();
@ -112,7 +135,30 @@ class CityTest extends TestCase
$this->assertEquals($this->city->getCompanyIds(), [$this->company_ids[0]]);
}
public function testRemoveCompaniesIds()
public function testRemoveNonExistingCompanyId()
{
$this->setUpCityWithCompanies();
$this->assertEquals($this->city->getCompanyIds(), $this->company_ids);
$this->city->removeCompanyId(23423);
$this->assertEquals($this->city->getCompanyIds(), $this->company_ids);
}
public function testRemoveNonExistingCompanyIdForEmptyCity()
{
$this->setUpCity();
$this->company_ids = [];
$this->assertEquals($this->city->getCompanyIds(), $this->company_ids);
$this->city->removeCompanyId(23423);
$this->assertEquals($this->city->getCompanyIds(), $this->company_ids);
}
public function testRemoveCompanyIds()
{
$this->setUpCityWithCompanies();
@ -123,7 +169,7 @@ class CityTest extends TestCase
$this->assertEquals($this->city->getCompanyIds(), [$this->company_ids[0]]);
}
public function testRemoveCompaniesIdsForTwoElements()
public function testRemoveTwoCompanyIds()
{
$this->setUpCityWithCompanies();
@ -134,7 +180,7 @@ class CityTest extends TestCase
$this->assertEquals($this->city->getCompanyIds(), []);
}
public function testRemoveNonExistingCompaniesIds()
public function testRemoveNonExistingCompanyIds()
{
$this->setUpCityWithCompanies();
@ -145,7 +191,7 @@ class CityTest extends TestCase
$this->assertEquals($this->city->getCompanyIds(), $this->company_ids);
}
public function testRemoveNonExistingCompaniesIdsFromEmpty()
public function testRemoveNonExistingCompanyIdsForEmptyCity()
{
$this->setUpCity();
$this->company_ids = [];

@ -10,30 +10,22 @@ use App\Models\Company;
class CompanyTest extends TestCase
{
/* company validation */
public function test_company_with_empty_name(): void
public function testIsExistForExistingCompany()
{
$json = Company::validate_with_name('');
$this->assertEquals($json['error'], 'The company name is empty, please, write the name!!!');
}
public function test_not_existing_company_with_name(): void
{
$name = '404 Company';
$name = 'testCompany';
$id = Company::where('name', $name)->first()->id;
$json = Company::validate_with_name($name);
$isExist = Company::isExist($id);
$this->assertEquals($json['error'], 'A company with the name does not exist!!!');
$this->assertTrue($isExist);
}
public function test_valid_company_with_name(): void
public function testIsExistForNonExistingCompany()
{
$name = 'testCompany';
$id = 1234;
$json = Company::validate_with_name($name);
$isExist = Company::isExist($id);
$this->assertEquals($json['ok'], 'A company with the name is valid.');
$this->assertFalse($isExist);
}
}

Loading…
Cancel
Save