Compare commits
2
Commits
6db3465407
...
a4938eb099
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a4938eb099 | ||
|
|
394f821d35 |
@@ -120,7 +120,7 @@ class CartController extends Controller
|
|||||||
return response()->json($validation);
|
return response()->json($validation);
|
||||||
|
|
||||||
// check for not valid city
|
// check for not valid city
|
||||||
$validation = City::validate_with_name($cityName);
|
$validation = City::validateWithName($cityName);
|
||||||
if(array_key_exists('error', $validation))
|
if(array_key_exists('error', $validation))
|
||||||
return response()->json($validation);
|
return response()->json($validation);
|
||||||
|
|
||||||
|
|||||||
+2
-2
@@ -42,7 +42,7 @@ class City extends Model
|
|||||||
$this->companies()->sync($companyIDs);
|
$this->companies()->sync($companyIDs);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function removeCompanyID(int $company_id)
|
public function removeCompanyId(int $company_id)
|
||||||
{
|
{
|
||||||
$this->companies()->detach($company_id);
|
$this->companies()->detach($company_id);
|
||||||
}
|
}
|
||||||
@@ -52,7 +52,7 @@ class City extends Model
|
|||||||
$this->companies()->detach($company_ids);
|
$this->companies()->detach($company_ids);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function validate_with_name(string $name)
|
public static function validateWithName(string $name)
|
||||||
{
|
{
|
||||||
$name = $name ?? '';
|
$name = $name ?? '';
|
||||||
|
|
||||||
|
|||||||
+136
-16
@@ -4,36 +4,156 @@ namespace Tests\Feature;
|
|||||||
|
|
||||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||||
use Illuminate\Foundation\Testing\WithFaker;
|
use Illuminate\Foundation\Testing\WithFaker;
|
||||||
|
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||||
use Tests\TestCase;
|
use Tests\TestCase;
|
||||||
|
|
||||||
use App\Models\City;
|
use App\Models\City;
|
||||||
|
use App\Models\Company;
|
||||||
|
|
||||||
|
use function PHPUnit\Framework\assertNotNull;
|
||||||
|
|
||||||
class CityTest extends TestCase
|
class CityTest extends TestCase
|
||||||
{
|
{
|
||||||
/* City validation */
|
use DatabaseTransactions;
|
||||||
|
|
||||||
public function test_city_with_empty_name(): void
|
protected $city;
|
||||||
|
protected $company_ids = [];
|
||||||
|
|
||||||
|
public function setUpCityWithCompanies(): void
|
||||||
{
|
{
|
||||||
$json = City::validate_with_name('');
|
$this->setUpCity();
|
||||||
|
$this->setUpCompanyIds();
|
||||||
|
|
||||||
$this->assertEquals($json['error'], 'The city name is empty, please, write the name!!!');
|
$this->city->addCompanyIds($this->company_ids);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function test_not_existing_city_with_name(): void
|
public function setUpCity(): void
|
||||||
{
|
|
||||||
$name = '404 City';
|
|
||||||
|
|
||||||
$json = City::validate_with_name($name);
|
|
||||||
|
|
||||||
$this->assertEquals($json['error'], 'A city with the name does not exist!!!');
|
|
||||||
}
|
|
||||||
|
|
||||||
public function test_valid_city_with_name(): void
|
|
||||||
{
|
{
|
||||||
$name = 'testCity';
|
$name = 'testCity';
|
||||||
|
$this->city = City::where('name', $name)->first();
|
||||||
|
}
|
||||||
|
|
||||||
$json = City::validate_with_name($name);
|
public function setUpCompanyIds(): void
|
||||||
|
{
|
||||||
|
$company_names = ['testCompany', 'testCompany2'];
|
||||||
|
$this->company_ids = [];
|
||||||
|
|
||||||
$this->assertEquals($json['ok'], 'A city with the name is valid.');
|
foreach($company_names as $name) {
|
||||||
|
$company = Company::where('name', $name)->first();
|
||||||
|
array_push($this->company_ids, $company->id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testGetCompaniesIdsForEmpty()
|
||||||
|
{
|
||||||
|
$this->setUpCity();
|
||||||
|
|
||||||
|
$this->assertNotNull($this->city);
|
||||||
|
$this->assertIsArray($this->city->getCompanyIds());
|
||||||
|
$this->assertEquals($this->city->getCompanyIds(), []);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testAddCompaniesIdForTwoElements()
|
||||||
|
{
|
||||||
|
$this->setUpCity();
|
||||||
|
$this->setUpCompanyIds();
|
||||||
|
|
||||||
|
foreach($this->company_ids as $id)
|
||||||
|
$this->city->addCompanyId($id);
|
||||||
|
|
||||||
|
$this->assertNotNull($this->city);
|
||||||
|
$this->assertIsArray($this->city->getCompanyIds());
|
||||||
|
$this->assertEquals($this->city->getCompanyIds(), $this->company_ids);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testAddNonExistingCompaniesId()
|
||||||
|
{
|
||||||
|
$this->setUpCityWithCompanies();
|
||||||
|
|
||||||
|
$this->assertEquals($this->city->getCompanyIds(), $this->company_ids);
|
||||||
|
|
||||||
|
$this->city->AddCompanyId(23423);
|
||||||
|
|
||||||
|
$this->assertEquals($this->city->getCompanyIds(), $this->company_ids);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testAddNonExistingCompaniesIdFromEmpty()
|
||||||
|
{
|
||||||
|
$this->setUpCity();
|
||||||
|
$this->company_ids = [];
|
||||||
|
|
||||||
|
$this->assertEquals($this->city->getCompanyIds(), $this->company_ids);
|
||||||
|
|
||||||
|
$this->city->addCompanyId(23423);
|
||||||
|
|
||||||
|
$this->assertEquals($this->city->getCompanyIds(), $this->company_ids);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testAddNonExistingCompaniesIdsFromEmpty()
|
||||||
|
{
|
||||||
|
$this->setUpCity();
|
||||||
|
$this->company_ids = [];
|
||||||
|
|
||||||
|
$this->assertEquals($this->city->getCompanyIds(), $this->company_ids);
|
||||||
|
|
||||||
|
$this->city->addCompanyIds([23423, 1234]);
|
||||||
|
|
||||||
|
$this->assertEquals($this->city->getCompanyIds(), $this->company_ids);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testRemoveCompaniesId()
|
||||||
|
{
|
||||||
|
$this->setUpCityWithCompanies();
|
||||||
|
|
||||||
|
$this->assertEquals($this->city->getCompanyIds(), $this->company_ids);
|
||||||
|
|
||||||
|
$this->city->removeCompanyId($this->company_ids[1]);
|
||||||
|
|
||||||
|
$this->assertEquals($this->city->getCompanyIds(), [$this->company_ids[0]]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testRemoveCompaniesIds()
|
||||||
|
{
|
||||||
|
$this->setUpCityWithCompanies();
|
||||||
|
|
||||||
|
$this->assertEquals($this->city->getCompanyIds(), $this->company_ids);
|
||||||
|
|
||||||
|
$this->city->removeCompanyIds([$this->company_ids[1]]);
|
||||||
|
|
||||||
|
$this->assertEquals($this->city->getCompanyIds(), [$this->company_ids[0]]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testRemoveCompaniesIdsForTwoElements()
|
||||||
|
{
|
||||||
|
$this->setUpCityWithCompanies();
|
||||||
|
|
||||||
|
$this->assertEquals($this->city->getCompanyIds(), $this->company_ids);
|
||||||
|
|
||||||
|
$this->city->removeCompanyIds($this->company_ids);
|
||||||
|
|
||||||
|
$this->assertEquals($this->city->getCompanyIds(), []);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testRemoveNonExistingCompaniesIds()
|
||||||
|
{
|
||||||
|
$this->setUpCityWithCompanies();
|
||||||
|
|
||||||
|
$this->assertEquals($this->city->getCompanyIds(), $this->company_ids);
|
||||||
|
|
||||||
|
$this->city->removeCompanyIds([23423, 1234]);
|
||||||
|
|
||||||
|
$this->assertEquals($this->city->getCompanyIds(), $this->company_ids);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testRemoveNonExistingCompaniesIdsFromEmpty()
|
||||||
|
{
|
||||||
|
$this->setUpCity();
|
||||||
|
$this->company_ids = [];
|
||||||
|
|
||||||
|
$this->assertEquals($this->city->getCompanyIds(), $this->company_ids);
|
||||||
|
|
||||||
|
$this->city->removeCompanyIds([23423, 1234]);
|
||||||
|
|
||||||
|
$this->assertEquals($this->city->getCompanyIds(), $this->company_ids);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,37 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Tests\Feature;
|
||||||
|
|
||||||
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||||
|
use Illuminate\Foundation\Testing\WithFaker;
|
||||||
|
use Tests\TestCase;
|
||||||
|
|
||||||
|
use App\Models\City;
|
||||||
|
|
||||||
|
class CityValidationTest extends TestCase
|
||||||
|
{
|
||||||
|
public function testCityWithEmptyName(): void
|
||||||
|
{
|
||||||
|
$json = City::validateWithName('');
|
||||||
|
|
||||||
|
$this->assertEquals($json['error'], 'The city name is empty, please, write the name!!!');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testNotExistingCityWithName(): void
|
||||||
|
{
|
||||||
|
$name = '404 City';
|
||||||
|
|
||||||
|
$json = City::validateWithName($name);
|
||||||
|
|
||||||
|
$this->assertEquals($json['error'], 'A city with the name does not exist!!!');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testValidCityWithName(): void
|
||||||
|
{
|
||||||
|
$name = 'testCity';
|
||||||
|
|
||||||
|
$json = City::validateWithName($name);
|
||||||
|
|
||||||
|
$this->assertEquals($json['ok'], 'A city with the name is valid.');
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user