Refactor to use CompanyInformativeValidatorByNameFactory.
continuous-integration/drone/push Build is passing
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -23,7 +23,7 @@ use App\Http\Resources\API\v2\CartItemCollection;
|
|||||||
|
|
||||||
use App\Models\Validation\UserValidationByMatrixUsername;
|
use App\Models\Validation\UserValidationByMatrixUsername;
|
||||||
use App\Models\Validation\CityValidationByName;
|
use App\Models\Validation\CityValidationByName;
|
||||||
use App\Models\Validation\CompanyValidationByName;
|
use App\Models\Validation\Messages\Factories\CompanyInformativeValidatorByNameFactory;
|
||||||
use App\Models\Validation\Messages\Factories\ItemInformativeValidatorByNameFactory;
|
use App\Models\Validation\Messages\Factories\ItemInformativeValidatorByNameFactory;
|
||||||
|
|
||||||
class CartController extends Controller
|
class CartController extends Controller
|
||||||
@@ -166,9 +166,9 @@ class CartController extends Controller
|
|||||||
if(!$validator->isValid())
|
if(!$validator->isValid())
|
||||||
return response()->json($validator->getMessageMap());
|
return response()->json($validator->getMessageMap());
|
||||||
|
|
||||||
$validator = new CompanyValidationByName($companyName);
|
$validator = (new CompanyInformativeValidatorByNameFactory($companyName))->create();
|
||||||
if(!$validator->isValid())
|
if(!$validator->isValid())
|
||||||
return response()->json($validator->getMessageMap());
|
return response()->json($validator->getOkStatus());
|
||||||
|
|
||||||
// Get objects
|
// Get objects
|
||||||
$user = User::where('matrix_username', $matrixUsername)->first();
|
$user = User::where('matrix_username', $matrixUsername)->first();
|
||||||
|
|||||||
@@ -1,18 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace App\Models\Validation;
|
|
||||||
|
|
||||||
use App\Models\Validation\ModelValidationByName;
|
|
||||||
use App\Models\Validation\Messages\CompanyMessagesFactory;
|
|
||||||
|
|
||||||
class CompanyValidationByName extends ModelValidationByName
|
|
||||||
{
|
|
||||||
public function __construct(string $name)
|
|
||||||
{
|
|
||||||
parent::__construct(
|
|
||||||
$name,
|
|
||||||
'App\Models\Company',
|
|
||||||
(new CompanyMessagesFactory())->create(),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,20 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace App\Models\Validation\Messages;
|
|
||||||
|
|
||||||
use App\Models\Validation\Messages\BaseMessages;
|
|
||||||
|
|
||||||
class CompanyMessagesFactory
|
|
||||||
{
|
|
||||||
protected array $messages = [
|
|
||||||
'found' => 'A company with the name is valid.',
|
|
||||||
'not_found' => 'A company with the name does not exist!!!',
|
|
||||||
'invalid_name' => 'The company name is empty, please, write the name!!!',
|
|
||||||
];
|
|
||||||
|
|
||||||
public function create()
|
|
||||||
{
|
|
||||||
return new BaseMessages($this->messages);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@@ -1,45 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace Tests\Feature;
|
|
||||||
|
|
||||||
use Tests\TestCase;
|
|
||||||
|
|
||||||
use App\Models\Validation\CompanyValidationByName;
|
|
||||||
|
|
||||||
class CompanyValidationByNameTest extends TestCase
|
|
||||||
{
|
|
||||||
public function dataProvider() {
|
|
||||||
return [
|
|
||||||
'Invalid Case' => [
|
|
||||||
'name' => '',
|
|
||||||
'key' => 'error',
|
|
||||||
'message' => 'The company name is empty, please, write the name!!!',
|
|
||||||
'isValid' => false,
|
|
||||||
],
|
|
||||||
'Not Found Case' => [
|
|
||||||
'name' => '404 Company',
|
|
||||||
'key' => 'error',
|
|
||||||
'message' => 'A company with the name does not exist!!!',
|
|
||||||
'isValid' => false,
|
|
||||||
],
|
|
||||||
'Found Case' => [
|
|
||||||
'name' => 'testCompany',
|
|
||||||
'key' => 'ok',
|
|
||||||
'message' => 'A company with the name is valid.',
|
|
||||||
'isValid' => true,
|
|
||||||
]
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @dataProvider dataProvider
|
|
||||||
*/
|
|
||||||
public function testCompanyValidationWithName(string $name, string $key, string $message, bool $isValid): void
|
|
||||||
{
|
|
||||||
$validator = new CompanyValidationByName($name);
|
|
||||||
$json = $validator->getMessageMap();
|
|
||||||
|
|
||||||
$this->assertEquals($json[$key], $message);
|
|
||||||
$this->assertEquals($validator->isValid(), $isValid);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user