This commit is contained in:
@@ -21,6 +21,7 @@ use App\DotsAPI\Fetcher\v2\AuthApiFetcher;
|
|||||||
use App\DotsAPI\Fetcher\v2\AuthApiSender;
|
use App\DotsAPI\Fetcher\v2\AuthApiSender;
|
||||||
use App\Http\Resources\API\v2\CartItemCollection;
|
use App\Http\Resources\API\v2\CartItemCollection;
|
||||||
|
|
||||||
|
use App\Models\Validation\CityValidationByName;
|
||||||
use App\Models\Validation\CompanyValidationByName;
|
use App\Models\Validation\CompanyValidationByName;
|
||||||
|
|
||||||
class CartController extends Controller
|
class CartController extends Controller
|
||||||
@@ -121,10 +122,9 @@ class CartController extends Controller
|
|||||||
if(array_key_exists('error', $validation))
|
if(array_key_exists('error', $validation))
|
||||||
return response()->json($validation);
|
return response()->json($validation);
|
||||||
|
|
||||||
// check for not valid city
|
$validator = new CityValidationByName($cityName);
|
||||||
$validation = City::validateWithName($cityName);
|
if(!$validator->isValid())
|
||||||
if(array_key_exists('error', $validation))
|
return response()->json($validator->getMessageMap());
|
||||||
return response()->json($validation);
|
|
||||||
|
|
||||||
// Get objects
|
// Get objects
|
||||||
$user = User::where('matrix_username', $matrixUsername)->first();
|
$user = User::where('matrix_username', $matrixUsername)->first();
|
||||||
@@ -166,7 +166,6 @@ class CartController extends Controller
|
|||||||
if(array_key_exists('error', $validation))
|
if(array_key_exists('error', $validation))
|
||||||
return response()->json($validation);
|
return response()->json($validation);
|
||||||
|
|
||||||
// check for not valid company
|
|
||||||
$validator = new CompanyValidationByName($companyName);
|
$validator = new CompanyValidationByName($companyName);
|
||||||
if(!$validator->isValid())
|
if(!$validator->isValid())
|
||||||
return response()->json($validator->getMessageMap());
|
return response()->json($validator->getMessageMap());
|
||||||
|
|||||||
+9
-17
@@ -6,9 +6,10 @@ use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||||
|
|
||||||
|
use App\Models\Validation\ValidationByNameInterface;
|
||||||
use App\Models\Company;
|
use App\Models\Company;
|
||||||
|
|
||||||
class City extends Model
|
class City extends Model implements ValidationByNameInterface
|
||||||
{
|
{
|
||||||
use HasFactory;
|
use HasFactory;
|
||||||
|
|
||||||
@@ -58,24 +59,15 @@ class City extends Model
|
|||||||
$this->companies()->detach($company_ids);
|
$this->companies()->detach($company_ids);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function validateWithName(string $name)
|
public static function isExistByName(string $name): bool
|
||||||
{
|
{
|
||||||
$name = $name ?? '';
|
$count = City::where('name', $name)->count();
|
||||||
|
|
||||||
if($name == '')
|
return $count != 0;
|
||||||
return [
|
}
|
||||||
'error' => 'The city name is empty, please, write the name!!!'
|
|
||||||
];
|
|
||||||
|
|
||||||
$city = City::where('name', $name)->first();
|
public static function isNameValid(string $name): bool
|
||||||
|
{
|
||||||
if(!$city)
|
return $name != '';
|
||||||
return [
|
|
||||||
'error' => 'A city with the name does not exist!!!'
|
|
||||||
];
|
|
||||||
|
|
||||||
return [
|
|
||||||
'ok' => 'A city with the name is valid.'
|
|
||||||
];
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,18 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Models\Validation;
|
||||||
|
|
||||||
|
use App\Models\Validation\ModelValidationByName;
|
||||||
|
use App\Models\Validation\Messages\CityMessagesFactory;
|
||||||
|
|
||||||
|
class CityValidationByName extends ModelValidationByName
|
||||||
|
{
|
||||||
|
public function __construct(string $name)
|
||||||
|
{
|
||||||
|
parent::__construct(
|
||||||
|
$name,
|
||||||
|
'App\Models\City',
|
||||||
|
(new CityMessagesFactory())->create(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Models\Validation\Messages;
|
||||||
|
|
||||||
|
use App\Models\Validation\Messages\BaseMessages;
|
||||||
|
|
||||||
|
class CityMessagesFactory
|
||||||
|
{
|
||||||
|
protected array $messages = [
|
||||||
|
'found' => 'A city with the name is valid.',
|
||||||
|
'not_found' => 'A city with the name does not exist!!!',
|
||||||
|
'invalid_name' => 'The city name is empty, please, write the name!!!',
|
||||||
|
];
|
||||||
|
|
||||||
|
public function create()
|
||||||
|
{
|
||||||
|
return new BaseMessages($this->messages);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,45 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Tests\Feature;
|
||||||
|
|
||||||
|
use Tests\TestCase;
|
||||||
|
|
||||||
|
use App\Models\Validation\CityValidationByName;
|
||||||
|
|
||||||
|
class CityValidationByNameTest extends TestCase
|
||||||
|
{
|
||||||
|
public function dataProvider() {
|
||||||
|
return [
|
||||||
|
'Invalid Case' => [
|
||||||
|
'name' => '',
|
||||||
|
'key' => 'error',
|
||||||
|
'message' => 'The city name is empty, please, write the name!!!',
|
||||||
|
'isValid' => false,
|
||||||
|
],
|
||||||
|
'Not Found Case' => [
|
||||||
|
'name' => '404 City',
|
||||||
|
'key' => 'error',
|
||||||
|
'message' => 'A city with the name does not exist!!!',
|
||||||
|
'isValid' => false,
|
||||||
|
],
|
||||||
|
'Found Case' => [
|
||||||
|
'name' => 'testCity',
|
||||||
|
'key' => 'ok',
|
||||||
|
'message' => 'A city with the name is valid.',
|
||||||
|
'isValid' => true,
|
||||||
|
]
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider dataProvider
|
||||||
|
*/
|
||||||
|
public function testCityValidationWithName(string $name, string $key, string $message, bool $isValid): void
|
||||||
|
{
|
||||||
|
$validator = new CityValidationByName($name);
|
||||||
|
$json = $validator->getMessageMap();
|
||||||
|
|
||||||
|
$this->assertEquals($json[$key], $message);
|
||||||
|
$this->assertEquals($validator->isValid(), $isValid);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,37 +0,0 @@
|
|||||||
<?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