Compare commits

..
20 Commits
Author SHA1 Message Date
KKlochko 91e3b3234f Refactor to use InformativeValidators instead of ModelValidation.
continuous-integration/drone/push Build is passing
2023-09-08 14:01:36 +03:00
KKlochko 5c40bc6bf3 Refactor to use UserInformativeValidatorByMatrixUserNameFactory.
continuous-integration/drone/push Build is passing
2023-09-07 18:38:03 +03:00
KKlochko 85bd79e59c Add InformativeValidatorFactory and MessageFactory for User.
continuous-integration/drone/push Build is passing
2023-09-06 17:21:05 +03:00
KKlochko c592931101 Add UserValidatorByMatrixUserName.
continuous-integration/drone/push Build is passing
2023-09-05 18:05:43 +03:00
KKlochko 32cc601868 Add CityInformativeValidatorByNameFactory.
continuous-integration/drone/push Build is passing
2023-09-04 20:34:26 +03:00
KKlochko 4c1e0bec07 Refactor InformativeValidators to move the modules into Validators.
continuous-integration/drone/push Build is passing
2023-09-03 11:54:39 +03:00
KKlochko 9244aa11bc Add CityValidatorByName.
continuous-integration/drone/push Build is passing
2023-09-02 20:48:29 +03:00
KKlochko 4b3bf81c76 Refactor ValidatorByNameFactories to use MessageByNameFactory.
continuous-integration/drone/push Build is passing
2023-09-01 12:22:34 +03:00
KKlochko 02cdb1f792 Add MessageFactory, MessageByNameFactory. 2023-09-01 12:22:08 +03:00
KKlochko 2b37a4c2e7 Refactor ValidatorByNameFactories of Company and Item models.
continuous-integration/drone/push Build is passing
2023-08-31 13:05:49 +03:00
KKlochko c415a77272 Add InformativeValidatorByNameFactory. 2023-08-31 13:02:00 +03:00
KKlochko b43759f3f4 Refactor Validators.
continuous-integration/drone/push Build is passing
2023-08-30 17:28:24 +03:00
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
KKlochko 36a803131e Refactor to use ItemInformativeValidatorByNameFactory.
continuous-integration/drone/push Build is passing
2023-08-28 14:28:14 +03:00
KKlochko 9a83b398c0 Add ItemInformativeValidatorByNameFactory.
continuous-integration/drone/push Build is passing
2023-08-28 14:17:17 +03:00
KKlochko e057d2de27 Add EmptyNameValidator and ItemValidatorByName. 2023-08-27 20:31:26 +03:00
KKlochko 3ffc82ee7c Refactor InformativeValidator. 2023-08-27 20:25:43 +03:00
KKlochko 7662d98bce Add Informative Validators.
continuous-integration/drone/push Build is passing
2023-08-26 20:30:25 +03:00
KKlochko da847273b8 Update Validators to extend ABC instead of implementing interface. 2023-08-26 19:55:27 +03:00
54 changed files with 925 additions and 536 deletions
+25 -19
View File
@@ -21,10 +21,10 @@ use App\DotsAPI\Fetcher\v2\AuthApiFetcher;
use App\DotsAPI\Fetcher\v2\AuthApiSender; use App\DotsAPI\Fetcher\v2\AuthApiSender;
use App\Http\Resources\API\v2\CartItemCollection; use App\Http\Resources\API\v2\CartItemCollection;
use App\Models\Validation\UserValidationByMatrixUsername; use App\Models\Validation\Validators\Informative\Factories\UserInformativeValidatorByMatrixUserNameFactory;
use App\Models\Validation\CityValidationByName; use App\Models\Validation\Validators\Informative\Factories\CityInformativeValidatorByNameFactory;
use App\Models\Validation\CompanyValidationByName; use App\Models\Validation\Validators\Informative\Factories\CompanyInformativeValidatorByNameFactory;
use App\Models\Validation\ItemValidationByName; use App\Models\Validation\Validators\Informative\Factories\ItemInformativeValidatorByNameFactory;
class CartController extends Controller class CartController extends Controller
{ {
@@ -119,13 +119,15 @@ class CartController extends Controller
$matrixUsername = $request->input('matrixUsername') ?? ''; $matrixUsername = $request->input('matrixUsername') ?? '';
$cityName = $request->input('cityName') ?? ''; $cityName = $request->input('cityName') ?? '';
$validator = new UserValidationByMatrixUsername($matrixUsername); $validators = [
if(!$validator->isValid()) (new UserInformativeValidatorByMatrixUserNameFactory($matrixUsername))->create(),
return response()->json($validator->getMessageMap()); (new CityInformativeValidatorByNameFactory($cityName))->create()
];
$validator = new CityValidationByName($cityName); foreach($validators as $validator) {
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();
@@ -162,13 +164,15 @@ class CartController extends Controller
$matrixUsername = $request->input('matrixUsername') ?? ''; $matrixUsername = $request->input('matrixUsername') ?? '';
$companyName = $request->input('companyName') ?? ''; $companyName = $request->input('companyName') ?? '';
$validator = new UserValidationByMatrixUsername($matrixUsername); $validators = [
if(!$validator->isValid()) (new UserInformativeValidatorByMatrixUserNameFactory($matrixUsername))->create(),
return response()->json($validator->getMessageMap()); (new CompanyInformativeValidatorByNameFactory($companyName))->create()
];
$validator = new CompanyValidationByName($companyName); foreach($validators as $validator) {
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();
@@ -203,13 +207,15 @@ class CartController extends Controller
$itemName = $request->input('itemName') ?? ''; $itemName = $request->input('itemName') ?? '';
$itemCount = $request->input('itemCount') ?? ''; $itemCount = $request->input('itemCount') ?? '';
$validator = new UserValidationByMatrixUsername($matrixUsername); $validators = [
if(!$validator->isValid()) (new UserInformativeValidatorByMatrixUserNameFactory($matrixUsername))->create(),
return response()->json($validator->getMessageMap()); (new ItemInformativeValidatorByNameFactory($itemName))->create()
];
$validator = new ItemValidationByName($itemName); foreach($validators as $validator) {
if(!$validator->isValid()) if(!$validator->isValid())
return response()->json($validator->getMessageMap()); return response()->json($validator->getOkStatus());
}
if($itemCount == 0) if($itemCount == 0)
return response()->json([ return response()->json([
+1 -14
View File
@@ -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
View File
@@ -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
View File
@@ -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 -15
View File
@@ -9,9 +9,7 @@ use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens; use Laravel\Sanctum\HasApiTokens;
use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\Relations\HasMany;
use App\Models\Validation\ValidationByNameInterface; class User extends Authenticatable
class User extends Authenticatable implements ValidationByNameInterface
{ {
use HasApiTokens, HasFactory, Notifiable; use HasApiTokens, HasFactory, Notifiable;
@@ -29,18 +27,6 @@ class User extends Authenticatable implements ValidationByNameInterface
public static function isExistByName(string $name): bool
{
$count = User::where('matrix_username', $name)->count();
return $count != 0;
}
public static function isNameValid(string $name): bool
{
return $name != '';
}
public function userAddresses(): HasMany public function userAddresses(): HasMany
{ {
return $this->hasMany(UserAddress::class); return $this->hasMany(UserAddress::class);
@@ -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,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,18 +0,0 @@
<?php
namespace App\Models\Validation;
use App\Models\Validation\ModelValidationByName;
use App\Models\Validation\Messages\ItemMessagesFactory;
class ItemValidationByName extends ModelValidationByName
{
public function __construct(string $name)
{
parent::__construct(
$name,
'App\Models\Item',
(new ItemMessagesFactory())->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,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,35 @@
<?php
namespace App\Models\Validation\Messages\Factories;
use App\Models\Validation\Messages\Factories\MessageFactory;
class MessageByNameFactory extends MessageFactory
{
private string $name;
public function __construct(string $name)
{
$this->setName($name);
}
public function setName(string $name): void
{
$this->name = $name;
}
public function getName(): string
{
return $this->name;
}
public function create(): array
{
return [
'found' => "A $this->name with the name is valid.",
'not_found' => "A $this->name with the name does not exist!!!",
'invalid_name' => "The $this->name name is empty, please, write the name!!!",
];
}
}
@@ -0,0 +1,9 @@
<?php
namespace App\Models\Validation\Messages\Factories;
abstract class MessageFactory
{
abstract function create(): array;
}
@@ -0,0 +1,18 @@
<?php
namespace App\Models\Validation\Messages\Factories;
use App\Models\Validation\Messages\Factories\MessageFactory;
class UsernameMessageByNameFactory extends MessageFactory
{
public function create(): array
{
return [
'found' => "A user with the username is valid.",
'not_found' => "A user with the username does not exist!!!",
'invalid_name' => "The username is invalid, please, check that you are registered or signed in!!!",
];
}
}
@@ -1,20 +0,0 @@
<?php
namespace App\Models\Validation\Messages;
use App\Models\Validation\Messages\BaseMessages;
class ItemMessagesFactory
{
protected array $messages = [
'found' => 'A item with the name is valid.',
'not_found' => 'A item with the name does not exist!!!',
'invalid_name' => 'The item name is empty, please, write the name!!!',
];
public function create()
{
return new BaseMessages($this->messages);
}
}
@@ -1,20 +0,0 @@
<?php
namespace App\Models\Validation\Messages;
use App\Models\Validation\Messages\BaseMessages;
class UserMessagesFactory
{
protected array $messages = [
'found' => 'A user with the username is valid.',
'not_found' => 'A user with the username does not exist!!!',
'invalid_name' => 'The username is empty, please, write username!!!',
];
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,18 +0,0 @@
<?php
namespace App\Models\Validation;
use App\Models\Validation\ModelValidationByName;
use App\Models\Validation\Messages\UserMessagesFactory;
class UserValidationByMatrixUsername extends ModelValidationByName
{
public function __construct(string $name)
{
parent::__construct(
$name,
'App\Models\User',
(new UserMessagesFactory())->create(),
);
}
}
@@ -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';
}
}
@@ -0,0 +1,16 @@
<?php
namespace App\Models\Validation\Validators;
use App\Models\Validation\Validators\NextValidatorByName;
use App\Models\City;
class CityValidatorByName extends NextValidatorByName
{
public function isCurrentValid(): bool
{
$count = City::where('name', $this->getName())->count();
return $count != 0;
}
}
@@ -0,0 +1,16 @@
<?php
namespace App\Models\Validation\Validators;
use App\Models\Validation\Validators\NextValidatorByName;
use App\Models\Company;
class CompanyValidatorByName extends NextValidatorByName {
public function isCurrentValid(): bool
{
$count = Company::where('name', $this->getName())->count();
return $count != 0;
}
}
@@ -0,0 +1,21 @@
<?php
namespace App\Models\Validation\Validators;
use App\Models\Validation\Validators\NextValidator;
class EmptyNameValidator extends NextValidator {
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,26 @@
<?php
namespace App\Models\Validation\Validators\Informative\Factories;
use App\Models\Validation\Validators\Informative\Factories\InformativeValidatorByNameFactory;
use App\Models\Validation\Validators\Informative\InformativeValidator;
use App\Models\Validation\Messages\Factories\MessageByNameFactory;
use App\Models\Validation\Validators\Validator;
use App\Models\Validation\Validators\CityValidatorByName;
class CityInformativeValidatorByNameFactory extends InformativeValidatorByNameFactory
{
public function __construct(string $name)
{
$this->setMessages((new MessageByNameFactory('city'))->create());
$this->setName($name);
}
public function getValidatorByName(InformativeValidator $okValidator): Validator
{
return new CityValidatorByName($this->name, $okValidator);
}
}
@@ -0,0 +1,26 @@
<?php
namespace App\Models\Validation\Validators\Informative\Factories;
use App\Models\Validation\Validators\Informative\Factories\InformativeValidatorByNameFactory;
use App\Models\Validation\Validators\Informative\InformativeValidator;
use App\Models\Validation\Messages\Factories\MessageByNameFactory;
use App\Models\Validation\Validators\Validator;
use App\Models\Validation\Validators\CompanyValidatorByName;
class CompanyInformativeValidatorByNameFactory extends InformativeValidatorByNameFactory
{
public function __construct(string $name)
{
$this->setMessages((new MessageByNameFactory('company'))->create());
$this->setName($name);
}
public function getValidatorByName(InformativeValidator $okValidator): Validator
{
return new CompanyValidatorByName($this->name, $okValidator);
}
}
@@ -0,0 +1,57 @@
<?php
namespace App\Models\Validation\Validators\Informative\Factories;
use App\Models\Validation\Validators\Informative\Factories\InformativeValidatorFactory;
use App\Models\Validation\Validators\Informative\InformativeValidator;
use App\Models\Validation\Validators\Informative\OkInformativeValidator;
use App\Models\Validation\Validators\Informative\NextInformativeValidator;
use App\Models\Validation\Validators\EmptyNameValidator;
use App\Models\Validation\Validators\Validator;
abstract class InformativeValidatorByNameFactory extends InformativeValidatorFactory
{
protected array $messages;
protected string $name;
public function setMessages(array $messages): void
{
$this->messages = $messages;
}
public function getMessages(): array
{
return $this->messages;
}
public function setName(string $name): void
{
$this->name = $name;
}
public function getName(): string
{
return $this->name;
}
abstract public function getValidatorByName(InformativeValidator $okValidator): Validator;
public function create(): InformativeValidator
{
$okValidator = new OkInformativeValidator($this->messages['found']);
$nameValidator = new NextInformativeValidator(
$this->messages['not_found'],
$this->getValidatorByName($okValidator),
$okValidator
);
return new NextInformativeValidator(
$this->messages['invalid_name'],
new EmptyNameValidator($this->name, $nameValidator),
$nameValidator
);
}
}
@@ -0,0 +1,11 @@
<?php
namespace App\Models\Validation\Validators\Informative\Factories;
use App\Models\Validation\Validators\Informative\InformativeValidator;
abstract class InformativeValidatorFactory
{
abstract function create(): InformativeValidator;
}
@@ -0,0 +1,26 @@
<?php
namespace App\Models\Validation\Validators\Informative\Factories;
use App\Models\Validation\Validators\Informative\Factories\InformativeValidatorByNameFactory;
use App\Models\Validation\Validators\Informative\InformativeValidator;
use App\Models\Validation\Messages\Factories\MessageByNameFactory;
use App\Models\Validation\Validators\Validator;
use App\Models\Validation\Validators\ItemValidatorByName;
class ItemInformativeValidatorByNameFactory extends InformativeValidatorByNameFactory
{
public function __construct(string $name)
{
$this->setMessages((new MessageByNameFactory('item'))->create());
$this->setName($name);
}
public function getValidatorByName(InformativeValidator $okValidator): Validator
{
return new ItemValidatorByName($this->name, $okValidator);
}
}
@@ -0,0 +1,26 @@
<?php
namespace App\Models\Validation\Validators\Informative\Factories;
use App\Models\Validation\Validators\Informative\Factories\InformativeValidatorByNameFactory;
use App\Models\Validation\Validators\Informative\InformativeValidator;
use App\Models\Validation\Messages\Factories\UsernameMessageByNameFactory;
use App\Models\Validation\Validators\Validator;
use App\Models\Validation\Validators\UserValidatorByMatrixUserName;
class UserInformativeValidatorByMatrixUserNameFactory extends InformativeValidatorByNameFactory
{
public function __construct(string $name)
{
$this->setMessages((new UsernameMessageByNameFactory())->create());
$this->setName($name);
}
public function getValidatorByName(InformativeValidator $okValidator): Validator
{
return new UserValidatorByMatrixUserName($this->name, $okValidator);
}
}
@@ -0,0 +1,47 @@
<?php
namespace App\Models\Validation\Validators\Informative;
use App\Models\Validation\Validators\Validator;
abstract class InformativeValidator extends Validator {
protected Validator $validator;
protected string $message;
public function isCurrentValid(): bool
{
return $this->validator->isCurrentValid();
}
public function isValid(): bool
{
return $this->validator->isValid();
}
public function setValidator($validator): void
{
$this->validator = $validator;
}
public function setMessage($message): void
{
$this->message = $message;
}
public function getMessage(): string
{
if(!$this->isCurrentValid())
return $this->message;
return $this->nextValidator->getMessage();
}
public function getOkStatus(): array
{
if(!$this->isCurrentValid())
return ['error' => $this->message];
return $this->nextValidator->getOkStatus();
}
}
@@ -0,0 +1,17 @@
<?php
namespace App\Models\Validation\Validators\Informative;
use App\Models\Validation\Validators\Informative\InformativeValidator;
use App\Models\Validation\Validators\Validator;
class NextInformativeValidator extends InformativeValidator {
protected InformativeValidator $nextValidator;
public function __construct(string $message, Validator $validator, InformativeValidator $nextValidator)
{
$this->setMessage($message);
$this->setValidator($validator);
$this->nextValidator = $nextValidator;
}
}
@@ -0,0 +1,25 @@
<?php
namespace App\Models\Validation\Validators\Informative;
use App\Models\Validation\Validators\Informative\InformativeValidator;
use App\Models\Validation\Validators\OkValidator;
class OkInformativeValidator extends InformativeValidator {
public function __construct($message)
{
$this->message = $message;
$this->validator = new OkValidator();
}
public function getMessage(): string
{
return $this->message;
}
public function getOkStatus(): array
{
return ['ok' => $this->getMessage()];
}
}
@@ -0,0 +1,16 @@
<?php
namespace App\Models\Validation\Validators;
use App\Models\Validation\Validators\NextValidatorByName;
use App\Models\Item;
class ItemValidatorByName extends NextValidatorByName {
public function isCurrentValid(): bool
{
$count = Item::where('name', $this->getName())->count();
return $count != 0;
}
}
@@ -0,0 +1,26 @@
<?php
namespace App\Models\Validation\Validators;
use App\Models\Validation\Validators\Validator;
abstract class NextValidator extends Validator {
protected Validator $nextValidator;
public function setNextValidator(Validator $validator): void
{
$this->nextValidator = $validator;
}
public function getNextValidator(): Validator
{
return $this->nextValidator;
}
abstract public function isCurrentValid(): bool;
public function isValid(): bool
{
return $this->isCurrentValid() && $this->getNextValidator()->isValid();
}
}
@@ -0,0 +1,28 @@
<?php
namespace App\Models\Validation\Validators;
use App\Models\Validation\Validators\NextValidator;
abstract class NextValidatorByName extends NextValidator {
private string $name;
public function __construct(string $name, Validator $nextValidator)
{
$this->setName($name);
$this->setNextValidator($nextValidator);
}
public function setName(string $name): void
{
$this->name = $name;
}
public function getName(): string
{
return $this->name;
}
abstract public function isCurrentValid(): bool;
}
@@ -2,9 +2,14 @@
namespace App\Models\Validation\Validators; namespace App\Models\Validation\Validators;
use App\Models\Validation\Validators\ValidationInterface; use App\Models\Validation\Validators\NextValidator;
class OkValidator extends NextValidator {
public function isCurrentValid(): bool
{
return true;
}
class OkValidator implements ValidationInterface {
public function isValid(): bool public function isValid(): bool
{ {
return true; return true;
@@ -2,15 +2,14 @@
namespace App\Models\Validation\Validators; namespace App\Models\Validation\Validators;
use App\Models\Validation\Validators\ValidationInterface; use App\Models\Validation\Validators\NextValidator;
use App\Models\Validation\Validators\ValidationTrait;
class UpperRangeValidator implements ValidationInterface { class UpperRangeValidator extends NextValidator {
private ValidationInterface $nextValidator;
private int $value; private int $value;
private int $rangeLimit; private int $rangeLimit;
protected Validator $nextValidator;
public function __construct(int $value, int $rangeLimit, ValidationInterface $nextValidator) public function __construct(int $value, int $rangeLimit, Validator $nextValidator)
{ {
$this->value = $value; $this->value = $value;
$this->rangeLimit = $rangeLimit; $this->rangeLimit = $rangeLimit;
@@ -24,10 +23,5 @@ class UpperRangeValidator implements ValidationInterface {
return true; return true;
} }
public function isValid(): bool
{
return $this->isCurrentValid() && $this->nextValidator->isValid();
}
} }
@@ -0,0 +1,17 @@
<?php
namespace App\Models\Validation\Validators;
use App\Models\Validation\Validators\NextValidatorByName;
use App\Models\User;
class UserValidatorByMatrixUserName extends NextValidatorByName
{
public function isCurrentValid(): bool
{
$count = User::where('matrix_username', $this->getName())->count();
return $count != 0;
}
}
@@ -0,0 +1,8 @@
<?php
namespace App\Models\Validation\Validators;
abstract class Validator {
abstract public function isValid(): bool;
}
+2 -2
View File
@@ -303,7 +303,7 @@ class CartTest extends TestCase
$response->assertStatus(200); $response->assertStatus(200);
$response->assertJson([ $response->assertJson([
'error' => 'The username is empty, please, write username!!!' 'error' => 'The username is invalid, please, check that you are registered or signed in!!!'
]); ]);
} }
@@ -321,7 +321,7 @@ class CartTest extends TestCase
$response->assertStatus(200); $response->assertStatus(200);
$response->assertJson([ $response->assertJson([
'error' => 'The username is empty, please, write username!!!' 'error' => 'The username is invalid, please, check that you are registered or signed in!!!'
]); ]);
} }
} }
@@ -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,45 +0,0 @@
<?php
namespace Tests\Feature;
use Tests\TestCase;
use App\Models\Validation\UserValidationByMatrixUsername;
class UserValidationTest extends TestCase
{
public function dataProvider() {
return [
'Invalid Case' => [
'name' => '',
'key' => 'error',
'message' => 'The username is empty, please, write username!!!',
'isValid' => false,
],
'Not Found Case' => [
'name' => '@kostia:test.com',
'key' => 'error',
'message' => 'A user with the username does not exist!!!',
'isValid' => false,
],
'Found Case' => [
'name' => '@test:test.com',
'key' => 'ok',
'message' => 'A user with the username is valid.',
'isValid' => true,
]
];
}
/**
* @dataProvider dataProvider
*/
public function testUserValidationWithName(string $name, string $key, string $message, bool $isValid): void
{
$validator = new UserValidationByMatrixUsername($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\CityValidatorByName;
use App\Models\Validation\Validators\OkValidator;
class CityValidatorByNameTest extends TestCase
{
public function dataProvider() {
return [
'Invalid Case' => [
'name' => '',
'isValid' => false,
],
'Not Found Case' => [
'name' => '404 City',
'isValid' => false,
],
'Found Case' => [
'name' => 'testCity',
'isValid' => true,
]
];
}
public function setUpValidator(string $name): CityValidatorByName
{
return new CityValidatorByName($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,47 @@
<?php
namespace Tests\Feature;
use Tests\TestCase;
use App\Models\Validation\Validators\Informative\Factories\CityInformativeValidatorByNameFactory;
class CityInformativeValidatorByNameFactoryTest extends TestCase
{
public function dataProvider() {
return [
'Found Case' => [
'name' => 'testCity',
'key' => 'ok',
'message' => 'A city with the name is valid.',
'isValid' => true,
],
'Not Found Case' => [
'name' => '404 City',
'key' => 'error',
'message' => 'A city with the name does not exist!!!',
'isValid' => false,
],
'Invalid Case' => [
'name' => '',
'key' => 'error',
'message' => 'The city name is empty, please, write the name!!!',
'isValid' => false,
],
];
}
/**
* @dataProvider dataProvider
*/
public function testValidatorFactory(string $name, string $key, string $message, bool $isValid): void
{
$factory = new CityInformativeValidatorByNameFactory($name);
$validator = $factory->create();
$this->assertEquals($validator->getMessage(), $message);
$this->assertEquals($validator->getOkStatus(), [$key => $message]);
$this->assertEquals($validator->isValid(), $isValid);
}
}
@@ -4,17 +4,17 @@ namespace Tests\Feature;
use Tests\TestCase; use Tests\TestCase;
use App\Models\Validation\CompanyValidationByName; use App\Models\Validation\Validators\Informative\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);
} }
} }
@@ -4,9 +4,9 @@ namespace Tests\Feature;
use Tests\TestCase; use Tests\TestCase;
use App\Models\Validation\ItemValidationByName; use App\Models\Validation\Validators\Informative\Factories\ItemInformativeValidatorByNameFactory;
class ItemValidationByNameTest extends TestCase class ItemInformativeValidatorByNameFactoryTest extends TestCase
{ {
public function dataProvider() { public function dataProvider() {
return [ return [
@@ -34,12 +34,13 @@ class ItemValidationByNameTest extends TestCase
/** /**
* @dataProvider dataProvider * @dataProvider dataProvider
*/ */
public function testItemValidationWithName(string $name, string $key, string $message, bool $isValid): void public function testValidatorFactory(string $name, string $key, string $message, bool $isValid): void
{ {
$validator = new ItemValidationByName($name); $factory = new ItemInformativeValidatorByNameFactory($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);
} }
} }
@@ -0,0 +1,47 @@
<?php
namespace Tests\Feature;
use Tests\TestCase;
use App\Models\Validation\Validators\Informative\Factories\UserInformativeValidatorByMatrixUserNameFactory;
class UserInformativeValidatorByMatrixUserNameFactoryTest extends TestCase
{
public function dataProvider() {
return [
'Found Case' => [
'name' => '@test:test.com',
'key' => 'ok',
'message' => 'A user with the username is valid.',
'isValid' => true,
],
'Not Found Case' => [
'name' => '@kostia:test.com',
'key' => 'error',
'message' => 'A user with the username does not exist!!!',
'isValid' => false,
],
'Invalid Case' => [
'name' => '',
'key' => 'error',
'message' => 'The username is invalid, please, check that you are registered or signed in!!!',
'isValid' => false,
],
];
}
/**
* @dataProvider dataProvider
*/
public function testValidatorFactory(string $name, string $key, string $message, bool $isValid): void
{
$factory = new UserInformativeValidatorByMatrixUserNameFactory($name);
$validator = $factory->create();
$this->assertEquals($validator->getMessage(), $message);
$this->assertEquals($validator->getOkStatus(), [$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,44 @@
<?php
namespace Tests\Feature;
use Tests\TestCase;
use App\Models\Validation\Validators\UserValidatorByMatrixUserName;
use App\Models\Validation\Validators\OkValidator;
class UserValidatorByMatrixUserNameTest extends TestCase
{
public function dataProvider() {
return [
'Found Case' => [
'name' => '@test:test.com',
'isValid' => true,
],
'Not Found Case' => [
'name' => '@kostia:test.com',
'isValid' => false,
],
'Invalid Case' => [
'name' => '',
'isValid' => false,
],
];
}
public function setUpValidator(string $name): UserValidatorByMatrixUserName
{
return new UserValidatorByMatrixUserName($name, new OkValidator());
}
/**
* @dataProvider dataProvider
*/
public function testGetMessage(string $name, bool $isValid): void
{
$validator = $this->setUpValidator($name);
$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);
}
}
@@ -0,0 +1,25 @@
<?php
namespace Tests\Feature;
use Tests\TestCase;
use App\Models\Validation\Messages\Factories\MessageByNameFactory;
class MessageByNameFactoryTest extends TestCase
{
public function testCreate(): void
{
$name = 'company';
$expected_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!!!",
];
$factory = new MessageByNameFactory($name);
$this->assertEquals($factory->create(), $expected_messages);
}
}
@@ -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);
}
}
@@ -0,0 +1,26 @@
<?php
namespace Tests\Unit;
use PHPUnit\Framework\TestCase;
use App\Models\Validation\Validators\Informative\OkInformativeValidator;
class OkInformativeValidatorTest extends TestCase
{
public function testIsValid(): void
{
$message = 'All fine';
$validator = new OkInformativeValidator($message);
$this->assertTrue($validator->isValid());
}
public function testGetMessage(): void
{
$message = 'All fine';
$validator = new OkInformativeValidator($message);
$this->assertEquals($validator->getMessage(), $message);
}
}
@@ -0,0 +1,70 @@
<?php
namespace Tests\Unit;
use PHPUnit\Framework\TestCase;
use App\Models\Validation\Validators\Informative\NextInformativeValidator;
use App\Models\Validation\Validators\Informative\OkInformativeValidator;
use App\Models\Validation\Validators\UpperRangeValidator;
use App\Models\Validation\Validators\OkValidator;
use App\Models\Validation\Validators\Validator;
class UpperRangeInformativeValidatorTest extends TestCase
{
protected $messages = [
'ok' => 'valid',
'error' => 'invalid',
];
public function dataProvider() {
return [
'Valid Case for 10' => [
'value' => 10,
'limit' => 10,
'isValid' => true,
'message' => $this->messages['ok'],
],
'Valid Case for 255' => [
'value' => 0,
'limit' => 255,
'isValid' => true,
'message' => $this->messages['ok'],
],
'Invalid Case for 10' => [
'value' => 11,
'limit' => 10,
'isValid' => false,
'message' => $this->messages['error'],
],
'Invalid Case for 255' => [
'value' => 256,
'limit' => 255,
'isValid' => false,
'message' => $this->messages['error'],
],
];
}
public function setUpValidator(int $value, int $rangeLimit): NextInformativeValidator
{
$upperRangeValidator = new UpperRangeValidator($value, $rangeLimit, new OkValidator());
return new NextInformativeValidator(
$this->messages['error'],
$upperRangeValidator,
new OkInformativeValidator($this->messages['ok']),
);
}
/**
* @dataProvider dataProvider
*/
public function testGetMessage(int $value, int $limit, bool $isValid, string $message): void
{
$validator = $this->setUpValidator($value, $limit);
$this->assertEquals($validator->isValid(), $isValid);
$this->assertEquals($validator->getMessage(), $message);
}
}