From 394f821d3554fea3790a1f2700d0e543676d22e4 Mon Sep 17 00:00:00 2001 From: KKlochko Date: Tue, 15 Aug 2023 13:15:24 +0300 Subject: [PATCH] Refactor the City and its tests. --- .../Controllers/API/v2/CartController.php | 2 +- app/Models/City.php | 4 +- .../Feature/Validation/CityValidationTest.php | 37 +++++++++++++++++++ 3 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 tests/Feature/Validation/CityValidationTest.php diff --git a/app/Http/Controllers/API/v2/CartController.php b/app/Http/Controllers/API/v2/CartController.php index 186ed94..d193cbf 100644 --- a/app/Http/Controllers/API/v2/CartController.php +++ b/app/Http/Controllers/API/v2/CartController.php @@ -120,7 +120,7 @@ class CartController extends Controller return response()->json($validation); // check for not valid city - $validation = City::validate_with_name($cityName); + $validation = City::validateWithName($cityName); if(array_key_exists('error', $validation)) return response()->json($validation); diff --git a/app/Models/City.php b/app/Models/City.php index 3e6f956..233ee8f 100644 --- a/app/Models/City.php +++ b/app/Models/City.php @@ -42,7 +42,7 @@ class City extends Model $this->companies()->sync($companyIDs); } - public function removeCompanyID(int $company_id) + public function removeCompanyId(int $company_id) { $this->companies()->detach($company_id); } @@ -52,7 +52,7 @@ class City extends Model $this->companies()->detach($company_ids); } - public static function validate_with_name(string $name) + public static function validateWithName(string $name) { $name = $name ?? ''; diff --git a/tests/Feature/Validation/CityValidationTest.php b/tests/Feature/Validation/CityValidationTest.php new file mode 100644 index 0000000..4dadfcb --- /dev/null +++ b/tests/Feature/Validation/CityValidationTest.php @@ -0,0 +1,37 @@ +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.'); + } +}