Compare commits

...
2 Commits
Author SHA1 Message Date
KKlochko f85bc047b8 Refactor to use CompanyInformativeValidatorByNameFactory.
continuous-integration/drone/push Build is passing
2023-08-29 17:02:59 +03:00
KKlochko a37992c2e9 Add CompanyInformativeValidatorByNameFactory. 2023-08-29 17:00:28 +03:00
6 changed files with 92 additions and 58 deletions
@@ -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);
}
}
@@ -0,0 +1,46 @@
<?php
namespace App\Models\Validation\Messages\Factories;
use App\Models\Validation\Messages\Factories\InformativeValidatorFactory;
use App\Models\Validation\Messages\InformativeValidator;
use App\Models\Validation\Messages\OkInformativeValidator;
use App\Models\Validation\Messages\NextInformativeValidator;
use App\Models\Validation\Validators\EmptyNameValidator;
use App\Models\Validation\Validators\CompanyValidatorByName;
class CompanyInformativeValidatorByNameFactory extends InformativeValidatorFactory
{
protected array $messages;
protected string $name;
public function __construct(string $name)
{
$this->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!!!',
];
$this->name = $name;
}
function create(): InformativeValidator
{
$okValidator = new OkInformativeValidator($this->messages['found']);
$nameValidator = new NextInformativeValidator(
$this->messages['not_found'],
new CompanyValidatorByName($this->name, $okValidator),
$okValidator
);
return new NextInformativeValidator(
$this->messages['invalid_name'],
new EmptyNameValidator($this->name, $nameValidator),
$nameValidator
);
}
}
@@ -0,0 +1,25 @@
<?php
namespace App\Models\Validation\Validators;
use App\Models\Validation\Validators\Validator;
use App\Models\Company;
class CompanyValidatorByName extends Validator {
private string $name;
protected Validator $nextValidator;
public function __construct(string $name, Validator $nextValidator)
{
$this->name = $name;
$this->nextValidator = $nextValidator;
}
public function isCurrentValid(): bool
{
$count = Company::where('name', $this->name)->count();
return $count != 0;
}
}
@@ -4,17 +4,17 @@ namespace Tests\Feature;
use Tests\TestCase; use Tests\TestCase;
use App\Models\Validation\CompanyValidationByName; use App\Models\Validation\Messages\Factories\CompanyInformativeValidatorByNameFactory;
class CompanyValidationByNameTest extends TestCase class CompanyInformativeValidatorByNameFactoryTest extends TestCase
{ {
public function dataProvider() { public function dataProvider() {
return [ return [
'Invalid Case' => [ 'Found Case' => [
'name' => '', 'name' => 'testCompany',
'key' => 'error', 'key' => 'ok',
'message' => 'The company name is empty, please, write the name!!!', 'message' => 'A company with the name is valid.',
'isValid' => false, 'isValid' => true,
], ],
'Not Found Case' => [ 'Not Found Case' => [
'name' => '404 Company', 'name' => '404 Company',
@@ -22,24 +22,25 @@ class CompanyValidationByNameTest extends TestCase
'message' => 'A company with the name does not exist!!!', 'message' => 'A company with the name does not exist!!!',
'isValid' => false, 'isValid' => false,
], ],
'Found Case' => [ 'Invalid Case' => [
'name' => 'testCompany', 'name' => '',
'key' => 'ok', 'key' => 'error',
'message' => 'A company with the name is valid.', 'message' => 'The company name is empty, please, write the name!!!',
'isValid' => true, 'isValid' => false,
] ],
]; ];
} }
/** /**
* @dataProvider dataProvider * @dataProvider dataProvider
*/ */
public function testCompanyValidationWithName(string $name, string $key, string $message, bool $isValid): void public function testValidatorFactory(string $name, string $key, string $message, bool $isValid): void
{ {
$validator = new CompanyValidationByName($name); $factory = new CompanyInformativeValidatorByNameFactory($name);
$json = $validator->getMessageMap(); $validator = $factory->create();
$this->assertEquals($json[$key], $message); $this->assertEquals($validator->getMessage(), $message);
$this->assertEquals($validator->getOkStatus(), [$key => $message]);
$this->assertEquals($validator->isValid(), $isValid); $this->assertEquals($validator->isValid(), $isValid);
} }
} }