Refactor to use InformativeValidators instead of ModelValidation.
continuous-integration/drone/push Build is failing
continuous-integration/drone/push Build is failing
This commit is contained in:
+1
-14
@@ -7,10 +7,9 @@ use Illuminate\Database\Eloquent\Model;
|
|||||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||||
|
|
||||||
use App\Models\Validation\ValidationByNameInterface;
|
|
||||||
use App\Models\Company;
|
use App\Models\Company;
|
||||||
|
|
||||||
class City extends Model implements ValidationByNameInterface
|
class City extends Model
|
||||||
{
|
{
|
||||||
use HasFactory;
|
use HasFactory;
|
||||||
|
|
||||||
@@ -64,16 +63,4 @@ class City extends Model implements ValidationByNameInterface
|
|||||||
{
|
{
|
||||||
$this->companies()->detach($company_ids);
|
$this->companies()->detach($company_ids);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function isExistByName(string $name): bool
|
|
||||||
{
|
|
||||||
$count = City::where('name', $name)->count();
|
|
||||||
|
|
||||||
return $count != 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static function isNameValid(string $name): bool
|
|
||||||
{
|
|
||||||
return $name != '';
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-15
@@ -7,9 +7,7 @@ use Illuminate\Database\Eloquent\Model;
|
|||||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||||
|
|
||||||
use App\Models\Validation\ValidationByNameInterface;
|
class Company extends Model
|
||||||
|
|
||||||
class Company extends Model implements ValidationByNameInterface
|
|
||||||
{
|
{
|
||||||
use HasFactory;
|
use HasFactory;
|
||||||
|
|
||||||
@@ -36,16 +34,4 @@ class Company extends Model implements ValidationByNameInterface
|
|||||||
|
|
||||||
return $count != 0;
|
return $count != 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function isExistByName(string $name): bool
|
|
||||||
{
|
|
||||||
$count = Company::where('name', $name)->count();
|
|
||||||
|
|
||||||
return $count != 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static function isNameValid(string $name): bool
|
|
||||||
{
|
|
||||||
return $name != '';
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-15
@@ -7,9 +7,7 @@ use Illuminate\Database\Eloquent\Model;
|
|||||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||||
|
|
||||||
use App\Models\Validation\ValidationByNameInterface;
|
class Item extends Model
|
||||||
|
|
||||||
class Item extends Model implements ValidationByNameInterface
|
|
||||||
{
|
{
|
||||||
use HasFactory;
|
use HasFactory;
|
||||||
|
|
||||||
@@ -88,17 +86,5 @@ class Item extends Model implements ValidationByNameInterface
|
|||||||
|
|
||||||
return $its_company->id == $company->id;
|
return $its_company->id == $company->id;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function isExistByName(string $name): bool
|
|
||||||
{
|
|
||||||
$count = Item::where('name', $name)->count();
|
|
||||||
|
|
||||||
return $count != 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static function isNameValid(string $name): bool
|
|
||||||
{
|
|
||||||
return $name != '';
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user