Refactor the City and its tests.
This commit is contained in:
@@ -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 ?? '';
|
||||||
|
|
||||||
|
|||||||
@@ -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