Refactor to use InformativeValidators instead of ModelValidation.
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
parent
5c40bc6bf3
commit
91e3b3234f
@ -1,18 +0,0 @@
|
||||
<?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(),
|
||||
);
|
||||
}
|
||||
}
|
@ -1,25 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Validation\Messages;
|
||||
|
||||
use App\Models\Validation\ValidationStatus;
|
||||
|
||||
class BaseMessages
|
||||
{
|
||||
protected array $messages;
|
||||
|
||||
public function __construct($messages)
|
||||
{
|
||||
$this->messages = $messages;
|
||||
}
|
||||
|
||||
public function getMessage($status): string
|
||||
{
|
||||
return match($status)
|
||||
{
|
||||
ValidationStatus::FOUND => $this->messages['found'],
|
||||
ValidationStatus::NOT_FOUND => $this->messages['not_found'],
|
||||
ValidationStatus::INVALID_NAME => $this->messages['invalid_name'],
|
||||
};
|
||||
}
|
||||
}
|
@ -1,20 +0,0 @@
|
||||
<?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);
|
||||
}
|
||||
}
|
||||
|
@ -1,50 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Validation;
|
||||
|
||||
use App\Models\Validation\ValidationByNameInterface;
|
||||
use App\Models\Validation\ValidationStatus;
|
||||
use App\Models\Validation\Messages\BaseMessages;
|
||||
|
||||
class ModelValidationByName
|
||||
{
|
||||
protected static BaseMessages $messages;
|
||||
protected static string $className;
|
||||
protected string $name;
|
||||
|
||||
public function __construct(string $name, string $className, BaseMessages $messages)
|
||||
{
|
||||
if (!in_array('App\Models\Validation\ValidationByNameInterface', class_implements($className)))
|
||||
throw new \RuntimeException("$className does not implement the ValidationByNameInterface interface.");
|
||||
|
||||
$this->name = $name;
|
||||
static::$className = $className;
|
||||
static::$messages = $messages;
|
||||
}
|
||||
|
||||
public function getStatus()
|
||||
{
|
||||
if(!call_user_func([static::$className, 'isNameValid'], $this->name))
|
||||
return ValidationStatus::INVALID_NAME;
|
||||
|
||||
if(!call_user_func([static::$className, 'isExistByName'], $this->name))
|
||||
return ValidationStatus::NOT_FOUND;
|
||||
|
||||
return ValidationStatus::FOUND;
|
||||
}
|
||||
|
||||
public function getMessageMap()
|
||||
{
|
||||
$status = $this->getStatus();
|
||||
|
||||
return [
|
||||
$status->status() => static::$messages->getMessage($status)
|
||||
];
|
||||
}
|
||||
|
||||
public function isValid()
|
||||
{
|
||||
return $this->getStatus()->isOk();
|
||||
}
|
||||
}
|
||||
|
@ -1,10 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Validation;
|
||||
|
||||
interface ValidationByNameInterface {
|
||||
public static function isNameValid(string $name);
|
||||
|
||||
public static function isExistByName(string $name);
|
||||
}
|
||||
|
@ -1,40 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Validation;
|
||||
|
||||
enum ValidationStatus
|
||||
{
|
||||
case FOUND;
|
||||
case NOT_FOUND;
|
||||
case INVALID_NAME;
|
||||
|
||||
public function value(): string
|
||||
{
|
||||
return match($this)
|
||||
{
|
||||
ValidationStatus::FOUND => 'found',
|
||||
ValidationStatus::NOT_FOUND => 'not_found',
|
||||
ValidationStatus::INVALID_NAME => 'invalid_name',
|
||||
};
|
||||
}
|
||||
|
||||
public function status(): string
|
||||
{
|
||||
return match($this)
|
||||
{
|
||||
ValidationStatus::FOUND => 'ok',
|
||||
ValidationStatus::NOT_FOUND => 'error',
|
||||
ValidationStatus::INVALID_NAME => 'error',
|
||||
};
|
||||
}
|
||||
|
||||
public function isError(): bool
|
||||
{
|
||||
return $this->status() == 'error';
|
||||
}
|
||||
|
||||
public function isOk(): bool
|
||||
{
|
||||
return $this->status() == 'ok';
|
||||
}
|
||||
}
|
@ -1,45 +0,0 @@
|
||||
<?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,51 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
use App\Models\Validation\ValidationStatus;
|
||||
use App\Models\Validation\Messages\BaseMessages;
|
||||
|
||||
class BaseMessagesTest extends TestCase
|
||||
{
|
||||
protected array $messages = [
|
||||
'found' => 'ok',
|
||||
'not_found' => '404',
|
||||
'invalid_name' => 'invalid name',
|
||||
];
|
||||
|
||||
protected BaseMessages $base_messages;
|
||||
|
||||
public function dataProvider() {
|
||||
return [
|
||||
'Invalid Case' => [
|
||||
'status' => ValidationStatus::INVALID_NAME,
|
||||
'expected_message' => $this->messages['invalid_name'],
|
||||
],
|
||||
'Not Found Case' => [
|
||||
'status' => ValidationStatus::NOT_FOUND,
|
||||
'expected_message' => $this->messages['not_found'],
|
||||
],
|
||||
'Found Case' => [
|
||||
'status' => ValidationStatus::FOUND,
|
||||
'expected_message' => $this->messages['found'],
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
public function setUp(): void
|
||||
{
|
||||
$this->base_messages = new BaseMessages($this->messages);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider dataProvider
|
||||
*/
|
||||
public function testGetMessage($status, string $expected_message): void
|
||||
{
|
||||
$message = $this->base_messages->getMessage($status);
|
||||
|
||||
$this->assertEquals($expected_message, $message);
|
||||
}
|
||||
}
|
Loading…
Reference in new issue