parent
3ffc82ee7c
commit
e057d2de27
@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Validation\Validators;
|
||||
|
||||
use App\Models\Validation\Validators\Validator;
|
||||
|
||||
class EmptyNameValidator 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
|
||||
{
|
||||
return $this->name != "";
|
||||
}
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Validation\Validators;
|
||||
|
||||
use App\Models\Validation\Validators\Validator;
|
||||
use App\Models\Item;
|
||||
|
||||
class ItemValidatorByName 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 = Item::where('name', $this->name)->count();
|
||||
|
||||
return $count != 0;
|
||||
}
|
||||
}
|
||||
|
@ -1,45 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use Tests\TestCase;
|
||||
|
||||
use App\Models\Validation\ItemValidationByName;
|
||||
|
||||
class ItemValidationByNameTest extends TestCase
|
||||
{
|
||||
public function dataProvider() {
|
||||
return [
|
||||
'Invalid Case' => [
|
||||
'name' => '',
|
||||
'key' => 'error',
|
||||
'message' => 'The item name is empty, please, write the name!!!',
|
||||
'isValid' => false,
|
||||
],
|
||||
'Not Found Case' => [
|
||||
'name' => '404 Item',
|
||||
'key' => 'error',
|
||||
'message' => 'A item with the name does not exist!!!',
|
||||
'isValid' => false,
|
||||
],
|
||||
'Found Case' => [
|
||||
'name' => 'Pizza Polo',
|
||||
'key' => 'ok',
|
||||
'message' => 'A item with the name is valid.',
|
||||
'isValid' => true,
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider dataProvider
|
||||
*/
|
||||
public function testItemValidationWithName(string $name, string $key, string $message, bool $isValid): void
|
||||
{
|
||||
$validator = new ItemValidationByName($name);
|
||||
$json = $validator->getMessageMap();
|
||||
|
||||
$this->assertEquals($json[$key], $message);
|
||||
$this->assertEquals($validator->isValid(), $isValid);
|
||||
}
|
||||
}
|
@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use Tests\TestCase;
|
||||
|
||||
use App\Models\Validation\Validators\ItemValidatorByName;
|
||||
use App\Models\Validation\Validators\OkValidator;
|
||||
|
||||
class ItemValidatorByNameTest extends TestCase
|
||||
{
|
||||
public function dataProvider() {
|
||||
return [
|
||||
'Invalid Case' => [
|
||||
'name' => '',
|
||||
'isValid' => false,
|
||||
],
|
||||
'Not Found Case' => [
|
||||
'name' => '404 Item',
|
||||
'isValid' => false,
|
||||
],
|
||||
'Found Case' => [
|
||||
'name' => 'Pizza Polo',
|
||||
'isValid' => true,
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
public function setUpValidator(string $name): ItemValidatorByName
|
||||
{
|
||||
return new ItemValidatorByName($name, new OkValidator());
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider dataProvider
|
||||
*/
|
||||
public function testGetMessage(string $name, bool $isValid): void
|
||||
{
|
||||
$validator = $this->setUpValidator($name);
|
||||
|
||||
$this->assertEquals($validator->isValid(), $isValid);
|
||||
}
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
use App\Models\Validation\Validators\EmptyNameValidator;
|
||||
use App\Models\Validation\Validators\OkValidator;
|
||||
|
||||
class EmptyNameValidatorTest extends TestCase
|
||||
{
|
||||
public function dataProvider() {
|
||||
return [
|
||||
'Valid Case' => [
|
||||
'name' => 'name',
|
||||
'isValid' => true,
|
||||
],
|
||||
'Invalid Case' => [
|
||||
'name' => '',
|
||||
'isValid' => false,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public function setUpValidator(string $name): EmptyNameValidator
|
||||
{
|
||||
return new EmptyNameValidator($name, new OkValidator());
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider dataProvider
|
||||
*/
|
||||
public function testGetMessage(string $name, bool $isValid): void
|
||||
{
|
||||
$validator = $this->setUpValidator($name);
|
||||
|
||||
$this->assertEquals($validator->isValid(), $isValid);
|
||||
}
|
||||
}
|
Loading…
Reference in new issue