Compare commits
32
Commits
6db3465407
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
91e3b3234f | ||
|
|
5c40bc6bf3 | ||
|
|
85bd79e59c | ||
|
|
c592931101 | ||
|
|
32cc601868 | ||
|
|
4c1e0bec07 | ||
|
|
9244aa11bc | ||
|
|
4b3bf81c76 | ||
|
|
02cdb1f792 | ||
|
|
2b37a4c2e7 | ||
|
|
c415a77272 | ||
|
|
b43759f3f4 | ||
|
|
f85bc047b8 | ||
|
|
a37992c2e9 | ||
|
|
36a803131e | ||
|
|
9a83b398c0 | ||
|
|
e057d2de27 | ||
|
|
3ffc82ee7c | ||
|
|
7662d98bce | ||
|
|
da847273b8 | ||
|
|
a4c0b3463b | ||
|
|
4430b04f11 | ||
|
|
66b8373da3 | ||
|
|
8fda7eae83 | ||
|
|
8c62393d3f | ||
|
|
c9974b007a | ||
|
|
1a7c585aa7 | ||
|
|
f437d777a5 | ||
|
|
135f4c0c93 | ||
|
|
c22945926c | ||
|
|
a4938eb099 | ||
|
|
394f821d35 |
@@ -21,6 +21,11 @@ use App\DotsAPI\Fetcher\v2\AuthApiFetcher;
|
||||
use App\DotsAPI\Fetcher\v2\AuthApiSender;
|
||||
use App\Http\Resources\API\v2\CartItemCollection;
|
||||
|
||||
use App\Models\Validation\Validators\Informative\Factories\UserInformativeValidatorByMatrixUserNameFactory;
|
||||
use App\Models\Validation\Validators\Informative\Factories\CityInformativeValidatorByNameFactory;
|
||||
use App\Models\Validation\Validators\Informative\Factories\CompanyInformativeValidatorByNameFactory;
|
||||
use App\Models\Validation\Validators\Informative\Factories\ItemInformativeValidatorByNameFactory;
|
||||
|
||||
class CartController extends Controller
|
||||
{
|
||||
/**
|
||||
@@ -114,15 +119,15 @@ class CartController extends Controller
|
||||
$matrixUsername = $request->input('matrixUsername') ?? '';
|
||||
$cityName = $request->input('cityName') ?? '';
|
||||
|
||||
// check for not valid user
|
||||
$validation = User::validateWithMatrixUsername($matrixUsername);
|
||||
if(array_key_exists('error', $validation))
|
||||
return response()->json($validation);
|
||||
$validators = [
|
||||
(new UserInformativeValidatorByMatrixUserNameFactory($matrixUsername))->create(),
|
||||
(new CityInformativeValidatorByNameFactory($cityName))->create()
|
||||
];
|
||||
|
||||
// check for not valid city
|
||||
$validation = City::validate_with_name($cityName);
|
||||
if(array_key_exists('error', $validation))
|
||||
return response()->json($validation);
|
||||
foreach($validators as $validator) {
|
||||
if(!$validator->isValid())
|
||||
return response()->json($validator->getOkStatus());
|
||||
}
|
||||
|
||||
// Get objects
|
||||
$user = User::where('matrix_username', $matrixUsername)->first();
|
||||
@@ -159,15 +164,15 @@ class CartController extends Controller
|
||||
$matrixUsername = $request->input('matrixUsername') ?? '';
|
||||
$companyName = $request->input('companyName') ?? '';
|
||||
|
||||
// check for not valid user
|
||||
$validation = User::validateWithMatrixUsername($matrixUsername);
|
||||
if(array_key_exists('error', $validation))
|
||||
return response()->json($validation);
|
||||
$validators = [
|
||||
(new UserInformativeValidatorByMatrixUserNameFactory($matrixUsername))->create(),
|
||||
(new CompanyInformativeValidatorByNameFactory($companyName))->create()
|
||||
];
|
||||
|
||||
// check for not valid company
|
||||
$validation = Company::validate_with_name($companyName);
|
||||
if(array_key_exists('error', $validation))
|
||||
return response()->json($validation);
|
||||
foreach($validators as $validator) {
|
||||
if(!$validator->isValid())
|
||||
return response()->json($validator->getOkStatus());
|
||||
}
|
||||
|
||||
// Get objects
|
||||
$user = User::where('matrix_username', $matrixUsername)->first();
|
||||
@@ -202,15 +207,15 @@ class CartController extends Controller
|
||||
$itemName = $request->input('itemName') ?? '';
|
||||
$itemCount = $request->input('itemCount') ?? '';
|
||||
|
||||
// check for not valid user
|
||||
$validation = User::validateWithMatrixUsername($matrixUsername);
|
||||
if(array_key_exists('error', $validation))
|
||||
return response()->json($validation);
|
||||
$validators = [
|
||||
(new UserInformativeValidatorByMatrixUserNameFactory($matrixUsername))->create(),
|
||||
(new ItemInformativeValidatorByNameFactory($itemName))->create()
|
||||
];
|
||||
|
||||
// check for not valid item
|
||||
$validation = Item::validate_with_name($itemName);
|
||||
if(array_key_exists('error', $validation))
|
||||
return response()->json($validation);
|
||||
foreach($validators as $validator) {
|
||||
if(!$validator->isValid())
|
||||
return response()->json($validator->getOkStatus());
|
||||
}
|
||||
|
||||
if($itemCount == 0)
|
||||
return response()->json([
|
||||
|
||||
+14
-23
@@ -5,6 +5,9 @@ namespace App\Models;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
|
||||
use App\Models\Company;
|
||||
|
||||
class City extends Model
|
||||
{
|
||||
@@ -21,6 +24,11 @@ class City extends Model
|
||||
return $this->belongsToMany(Company::class, 'cities_companies', 'city_id', 'company_id');
|
||||
}
|
||||
|
||||
public function userAddresses(): HasMany
|
||||
{
|
||||
return $this->hasMany(UserAddress::class);
|
||||
}
|
||||
|
||||
public function getCompanyIds(): array
|
||||
{
|
||||
return $this->companies()->pluck('company_id')->toArray();
|
||||
@@ -33,16 +41,20 @@ class City extends Model
|
||||
|
||||
public function addCompanyId(int $company_id)
|
||||
{
|
||||
if(Company::isExist($company_id))
|
||||
$this->addCompanyIds([$company_id]);
|
||||
}
|
||||
|
||||
public function addCompanyIds(array $company_ids)
|
||||
{
|
||||
$companyIDs = array_merge($this->getCompanyIds(), $company_ids);
|
||||
$existingCompanies = array_filter($company_ids, ['App\Models\Company', 'isExist']);
|
||||
|
||||
$companyIDs = array_merge($this->getCompanyIds(), $existingCompanies);
|
||||
|
||||
$this->companies()->sync($companyIDs);
|
||||
}
|
||||
|
||||
public function removeCompanyID(int $company_id)
|
||||
public function removeCompanyId(int $company_id)
|
||||
{
|
||||
$this->companies()->detach($company_id);
|
||||
}
|
||||
@@ -51,25 +63,4 @@ class City extends Model
|
||||
{
|
||||
$this->companies()->detach($company_ids);
|
||||
}
|
||||
|
||||
public static function validate_with_name(string $name)
|
||||
{
|
||||
$name = $name ?? '';
|
||||
|
||||
if($name == '')
|
||||
return [
|
||||
'error' => 'The city name is empty, please, write the name!!!'
|
||||
];
|
||||
|
||||
$city = City::where('name', $name)->first();
|
||||
|
||||
if(!$city)
|
||||
return [
|
||||
'error' => 'A city with the name does not exist!!!'
|
||||
];
|
||||
|
||||
return [
|
||||
'ok' => 'A city with the name is valid.'
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
+3
-17
@@ -28,24 +28,10 @@ class Company extends Model
|
||||
return $this->hasMany(Category::class);
|
||||
}
|
||||
|
||||
public static function validate_with_name(string $name)
|
||||
public static function isExist(int $id): bool
|
||||
{
|
||||
$name = $name ?? '';
|
||||
$count = Company::where('id', $id)->count();
|
||||
|
||||
if($name == '')
|
||||
return [
|
||||
'error' => 'The company name is empty, please, write the name!!!'
|
||||
];
|
||||
|
||||
$company = company::where('name', $name)->first();
|
||||
|
||||
if(!$company)
|
||||
return [
|
||||
'error' => 'A company with the name does not exist!!!'
|
||||
];
|
||||
|
||||
return [
|
||||
'ok' => 'A company with the name is valid.'
|
||||
];
|
||||
return $count != 0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -86,26 +86,5 @@ class Item extends Model
|
||||
|
||||
return $its_company->id == $company->id;
|
||||
}
|
||||
|
||||
public static function validate_with_name(string $name)
|
||||
{
|
||||
$name = $name ?? '';
|
||||
|
||||
if($name == '')
|
||||
return [
|
||||
'error' => 'The item name is empty, please, write the name!!!'
|
||||
];
|
||||
|
||||
$item = Item::where('name', $name)->first();
|
||||
|
||||
if(!$item)
|
||||
return [
|
||||
'error' => 'A item with the name does not exist!!!'
|
||||
];
|
||||
|
||||
return [
|
||||
'ok' => 'A item with the name is valid.'
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+6
-18
@@ -7,6 +7,7 @@ use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||
use Illuminate\Notifications\Notifiable;
|
||||
use Laravel\Sanctum\HasApiTokens;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
|
||||
class User extends Authenticatable
|
||||
{
|
||||
@@ -23,24 +24,11 @@ class User extends Authenticatable
|
||||
'phone',
|
||||
];
|
||||
|
||||
public static function validateWithMatrixUsername(string $matrixUsername)
|
||||
|
||||
|
||||
|
||||
public function userAddresses(): HasMany
|
||||
{
|
||||
$matrixUsername = $matrixUsername ?? '';
|
||||
|
||||
if($matrixUsername == '')
|
||||
return [
|
||||
'error' => 'The username is empty, please, write username!!!'
|
||||
];
|
||||
|
||||
$user = User::where('matrix_username', $matrixUsername)->first();
|
||||
|
||||
if(!$user)
|
||||
return [
|
||||
'error' => 'A user with the username does not exist!!!'
|
||||
];
|
||||
|
||||
return [
|
||||
'ok' => 'A user with the username is valid.'
|
||||
];
|
||||
return $this->hasMany(UserAddress::class);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
class UserAddress extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*
|
||||
* @var array<int, string>
|
||||
*/
|
||||
protected $fillable = [
|
||||
'title',
|
||||
'type',
|
||||
'street',
|
||||
'house',
|
||||
'flat',
|
||||
'stage',
|
||||
'note',
|
||||
'city_id',
|
||||
'user_id',
|
||||
];
|
||||
|
||||
public function user(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
|
||||
public function city(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(City::class);
|
||||
}
|
||||
}
|
||||
@@ -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!!!",
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 != "";
|
||||
}
|
||||
}
|
||||
+26
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
+26
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
+57
@@ -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
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
+11
@@ -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;
|
||||
}
|
||||
|
||||
+26
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
+26
@@ -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;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Validation\Validators;
|
||||
|
||||
use App\Models\Validation\Validators\NextValidator;
|
||||
|
||||
class OkValidator extends NextValidator {
|
||||
public function isCurrentValid(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public function isValid(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Validation\Validators;
|
||||
|
||||
use App\Models\Validation\Validators\NextValidator;
|
||||
|
||||
class UpperRangeValidator extends NextValidator {
|
||||
private int $value;
|
||||
private int $rangeLimit;
|
||||
protected Validator $nextValidator;
|
||||
|
||||
public function __construct(int $value, int $rangeLimit, Validator $nextValidator)
|
||||
{
|
||||
$this->value = $value;
|
||||
$this->rangeLimit = $rangeLimit;
|
||||
$this->nextValidator = $nextValidator;
|
||||
}
|
||||
|
||||
public function isCurrentValid(): bool
|
||||
{
|
||||
if($this->value > $this->rangeLimit)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
interface ValidationInterface {
|
||||
public function isValid(): bool;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Validation\Validators;
|
||||
|
||||
abstract class Validator {
|
||||
abstract public function isValid(): bool;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace Database\Factories;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
|
||||
/**
|
||||
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\UserAddress>
|
||||
*/
|
||||
class UserAddressFactory extends Factory
|
||||
{
|
||||
/**
|
||||
* Define the model's default state.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function definition(): array
|
||||
{
|
||||
return [
|
||||
//
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create('user_addresses', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->string('title', 100);
|
||||
$table->smallInteger('type')->default(0);
|
||||
$table->string('street', 255);
|
||||
$table->string('house', 10)->nullable();
|
||||
$table->string('flat', 10)->nullable();
|
||||
$table->string('stage', 10)->nullable();
|
||||
$table->string('note', 100);
|
||||
|
||||
$table->unsignedBiginteger('city_id')->unsigned();
|
||||
$table->unsignedBiginteger('user_id')->unsigned();
|
||||
|
||||
$table->foreign('city_id')->references('id')
|
||||
->on('cities')->onDelete('cascade');
|
||||
|
||||
$table->foreign('user_id')->references('id')
|
||||
->on('users')->onDelete('cascade');
|
||||
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('user_addresses');
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace Database\Seeders;
|
||||
|
||||
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
|
||||
use Illuminate\Database\Seeder;
|
||||
|
||||
class UserAddressSeeder extends Seeder
|
||||
{
|
||||
/**
|
||||
* Run the database seeds.
|
||||
*/
|
||||
public function run(): void
|
||||
{
|
||||
//
|
||||
}
|
||||
}
|
||||
@@ -303,7 +303,7 @@ class CartTest extends TestCase
|
||||
$response->assertStatus(200);
|
||||
|
||||
$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->assertJson([
|
||||
'error' => 'The username is empty, please, write username!!!'
|
||||
'error' => 'The username is invalid, please, check that you are registered or signed in!!!'
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
+182
-16
@@ -4,36 +4,202 @@ namespace Tests\Feature;
|
||||
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Foundation\Testing\WithFaker;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
use Tests\TestCase;
|
||||
|
||||
use App\Models\City;
|
||||
use App\Models\Company;
|
||||
|
||||
use function PHPUnit\Framework\assertNotNull;
|
||||
|
||||
class CityTest extends TestCase
|
||||
{
|
||||
/* City validation */
|
||||
use DatabaseTransactions;
|
||||
|
||||
public function test_city_with_empty_name(): void
|
||||
protected $city;
|
||||
protected $company_ids = [];
|
||||
|
||||
public function setUpCityWithCompanies(): void
|
||||
{
|
||||
$json = City::validate_with_name('');
|
||||
$this->setUpCity();
|
||||
$this->setUpCompanyIds();
|
||||
|
||||
$this->assertEquals($json['error'], 'The city name is empty, please, write the name!!!');
|
||||
$this->city->addCompanyIds($this->company_ids);
|
||||
}
|
||||
|
||||
public function test_not_existing_city_with_name(): void
|
||||
{
|
||||
$name = '404 City';
|
||||
|
||||
$json = City::validate_with_name($name);
|
||||
|
||||
$this->assertEquals($json['error'], 'A city with the name does not exist!!!');
|
||||
}
|
||||
|
||||
public function test_valid_city_with_name(): void
|
||||
public function setUpCity(): void
|
||||
{
|
||||
$name = 'testCity';
|
||||
$this->city = City::where('name', $name)->first();
|
||||
}
|
||||
|
||||
$json = City::validate_with_name($name);
|
||||
public function setUpCompanyIds(): void
|
||||
{
|
||||
$company_names = ['testCompany', 'testCompany2'];
|
||||
$this->company_ids = [];
|
||||
|
||||
$this->assertEquals($json['ok'], 'A city with the name is valid.');
|
||||
foreach($company_names as $name) {
|
||||
$company = Company::where('name', $name)->first();
|
||||
array_push($this->company_ids, $company->id);
|
||||
}
|
||||
}
|
||||
|
||||
public function testGetCompanyIdsForEmptyCity()
|
||||
{
|
||||
$this->setUpCity();
|
||||
|
||||
$this->assertNotNull($this->city);
|
||||
$this->assertIsArray($this->city->getCompanyIds());
|
||||
$this->assertEquals($this->city->getCompanyIds(), []);
|
||||
}
|
||||
|
||||
public function testAddTwoCompanyId()
|
||||
{
|
||||
$this->setUpCity();
|
||||
$this->setUpCompanyIds();
|
||||
|
||||
foreach($this->company_ids as $id)
|
||||
$this->city->addCompanyId($id);
|
||||
|
||||
$this->assertNotNull($this->city);
|
||||
$this->assertIsArray($this->city->getCompanyIds());
|
||||
$this->assertEquals($this->city->getCompanyIds(), $this->company_ids);
|
||||
}
|
||||
|
||||
public function testAddNonExistingCompanyId()
|
||||
{
|
||||
$this->setUpCityWithCompanies();
|
||||
|
||||
$this->assertEquals($this->city->getCompanyIds(), $this->company_ids);
|
||||
|
||||
$this->city->AddCompanyId(23423);
|
||||
|
||||
$this->assertEquals($this->city->getCompanyIds(), $this->company_ids);
|
||||
}
|
||||
|
||||
public function testAddNonExistingCompanyIdForEmptyCity()
|
||||
{
|
||||
$this->setUpCity();
|
||||
$this->company_ids = [];
|
||||
|
||||
$this->assertEquals($this->city->getCompanyIds(), $this->company_ids);
|
||||
|
||||
$this->city->addCompanyId(23423);
|
||||
|
||||
$this->assertEquals($this->city->getCompanyIds(), $this->company_ids);
|
||||
}
|
||||
|
||||
public function testAddTwoCompanyIds()
|
||||
{
|
||||
$this->setUpCity();
|
||||
$this->setUpCompanyIds();
|
||||
|
||||
$this->city->addCompanyIds($this->company_ids);
|
||||
|
||||
$this->assertNotNull($this->city);
|
||||
$this->assertIsArray($this->city->getCompanyIds());
|
||||
$this->assertEquals($this->city->getCompanyIds(), $this->company_ids);
|
||||
}
|
||||
|
||||
public function testAddNonExistingCompanyIds()
|
||||
{
|
||||
$this->setUpCityWithCompanies();
|
||||
|
||||
$this->assertEquals($this->city->getCompanyIds(), $this->company_ids);
|
||||
|
||||
$this->city->addCompanyIds([23423, 1234]);
|
||||
|
||||
$this->assertEquals($this->city->getCompanyIds(), $this->company_ids);
|
||||
}
|
||||
|
||||
public function testAddNonExistingCompanyIdsForEmpty()
|
||||
{
|
||||
$this->setUpCity();
|
||||
$this->company_ids = [];
|
||||
|
||||
$this->assertEquals($this->city->getCompanyIds(), $this->company_ids);
|
||||
|
||||
$this->city->addCompanyIds([23423, 1234]);
|
||||
|
||||
$this->assertEquals($this->city->getCompanyIds(), $this->company_ids);
|
||||
}
|
||||
|
||||
public function testRemoveCompanyId()
|
||||
{
|
||||
$this->setUpCityWithCompanies();
|
||||
|
||||
$this->assertEquals($this->city->getCompanyIds(), $this->company_ids);
|
||||
|
||||
$this->city->removeCompanyId($this->company_ids[1]);
|
||||
|
||||
$this->assertEquals($this->city->getCompanyIds(), [$this->company_ids[0]]);
|
||||
}
|
||||
|
||||
public function testRemoveNonExistingCompanyId()
|
||||
{
|
||||
$this->setUpCityWithCompanies();
|
||||
|
||||
$this->assertEquals($this->city->getCompanyIds(), $this->company_ids);
|
||||
|
||||
$this->city->removeCompanyId(23423);
|
||||
|
||||
$this->assertEquals($this->city->getCompanyIds(), $this->company_ids);
|
||||
}
|
||||
|
||||
public function testRemoveNonExistingCompanyIdForEmptyCity()
|
||||
{
|
||||
$this->setUpCity();
|
||||
$this->company_ids = [];
|
||||
|
||||
$this->assertEquals($this->city->getCompanyIds(), $this->company_ids);
|
||||
|
||||
$this->city->removeCompanyId(23423);
|
||||
|
||||
$this->assertEquals($this->city->getCompanyIds(), $this->company_ids);
|
||||
}
|
||||
|
||||
public function testRemoveCompanyIds()
|
||||
{
|
||||
$this->setUpCityWithCompanies();
|
||||
|
||||
$this->assertEquals($this->city->getCompanyIds(), $this->company_ids);
|
||||
|
||||
$this->city->removeCompanyIds([$this->company_ids[1]]);
|
||||
|
||||
$this->assertEquals($this->city->getCompanyIds(), [$this->company_ids[0]]);
|
||||
}
|
||||
|
||||
public function testRemoveTwoCompanyIds()
|
||||
{
|
||||
$this->setUpCityWithCompanies();
|
||||
|
||||
$this->assertEquals($this->city->getCompanyIds(), $this->company_ids);
|
||||
|
||||
$this->city->removeCompanyIds($this->company_ids);
|
||||
|
||||
$this->assertEquals($this->city->getCompanyIds(), []);
|
||||
}
|
||||
|
||||
public function testRemoveNonExistingCompanyIds()
|
||||
{
|
||||
$this->setUpCityWithCompanies();
|
||||
|
||||
$this->assertEquals($this->city->getCompanyIds(), $this->company_ids);
|
||||
|
||||
$this->city->removeCompanyIds([23423, 1234]);
|
||||
|
||||
$this->assertEquals($this->city->getCompanyIds(), $this->company_ids);
|
||||
}
|
||||
|
||||
public function testRemoveNonExistingCompanyIdsForEmptyCity()
|
||||
{
|
||||
$this->setUpCity();
|
||||
$this->company_ids = [];
|
||||
|
||||
$this->assertEquals($this->city->getCompanyIds(), $this->company_ids);
|
||||
|
||||
$this->city->removeCompanyIds([23423, 1234]);
|
||||
|
||||
$this->assertEquals($this->city->getCompanyIds(), $this->company_ids);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,30 +10,22 @@ use App\Models\Company;
|
||||
|
||||
class CompanyTest extends TestCase
|
||||
{
|
||||
/* company validation */
|
||||
|
||||
public function test_company_with_empty_name(): void
|
||||
{
|
||||
$json = Company::validate_with_name('');
|
||||
|
||||
$this->assertEquals($json['error'], 'The company name is empty, please, write the name!!!');
|
||||
}
|
||||
|
||||
public function test_not_existing_company_with_name(): void
|
||||
{
|
||||
$name = '404 Company';
|
||||
|
||||
$json = Company::validate_with_name($name);
|
||||
|
||||
$this->assertEquals($json['error'], 'A company with the name does not exist!!!');
|
||||
}
|
||||
|
||||
public function test_valid_company_with_name(): void
|
||||
public function testIsExistForExistingCompany()
|
||||
{
|
||||
$name = 'testCompany';
|
||||
$id = Company::where('name', $name)->first()->id;
|
||||
|
||||
$json = Company::validate_with_name($name);
|
||||
$isExist = Company::isExist($id);
|
||||
|
||||
$this->assertEquals($json['ok'], 'A company with the name is valid.');
|
||||
$this->assertTrue($isExist);
|
||||
}
|
||||
|
||||
public function testIsExistForNonExistingCompany()
|
||||
{
|
||||
$id = 1234;
|
||||
|
||||
$isExist = Company::isExist($id);
|
||||
|
||||
$this->assertFalse($isExist);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,19 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
// use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Tests\TestCase;
|
||||
|
||||
class ExampleTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* A basic test example.
|
||||
*/
|
||||
public function test_the_application_returns_a_successful_response(): void
|
||||
{
|
||||
$response = $this->get('/');
|
||||
|
||||
$response->assertStatus(200);
|
||||
}
|
||||
}
|
||||
@@ -38,31 +38,4 @@ class ItemTest extends TestCase
|
||||
|
||||
$this->assertFalse($is_belong);
|
||||
}
|
||||
|
||||
/* Item validation */
|
||||
|
||||
public function test_item_with_empty_name(): void
|
||||
{
|
||||
$json = Item::validate_with_name('');
|
||||
|
||||
$this->assertEquals($json['error'], 'The item name is empty, please, write the name!!!');
|
||||
}
|
||||
|
||||
public function test_not_existing_item_with_name(): void
|
||||
{
|
||||
$name = '404 Item';
|
||||
|
||||
$json = Item::validate_with_name($name);
|
||||
|
||||
$this->assertEquals($json['error'], 'A item with the name does not exist!!!');
|
||||
}
|
||||
|
||||
public function test_valid_item_with_name(): void
|
||||
{
|
||||
$name = 'Pizza Polo';
|
||||
|
||||
$json = Item::validate_with_name($name);
|
||||
|
||||
$this->assertEquals($json['ok'], 'A item with the name is valid.');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,39 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Foundation\Testing\WithFaker;
|
||||
use Tests\TestCase;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
use App\Models\User;
|
||||
|
||||
class UserValidationTest extends TestCase
|
||||
{
|
||||
public function testEmptyUsernameWithMatrixUsername(): void
|
||||
{
|
||||
$json = User::validateWithMatrixUsername('');
|
||||
|
||||
$this->assertEquals($json['error'], 'The username is empty, please, write username!!!');
|
||||
}
|
||||
|
||||
public function testValidUserWithMatrixUsername(): void
|
||||
{
|
||||
$matrixUsername = '@test:test.com';
|
||||
|
||||
$json = User::validateWithMatrixUsername($matrixUsername);
|
||||
|
||||
$this->assertEquals($json['ok'], 'A user with the username is valid.');
|
||||
}
|
||||
|
||||
public function testNotExistingUserWithMatrixUsername(): void
|
||||
{
|
||||
$matrixUsername = '@kostia:test.com';
|
||||
|
||||
$json = User::validateWithMatrixUsername($matrixUsername);
|
||||
|
||||
$this->assertEquals($json['error'], 'A user with the username does not exist!!!');
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
+47
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use Tests\TestCase;
|
||||
|
||||
use App\Models\Validation\Validators\Informative\Factories\CompanyInformativeValidatorByNameFactory;
|
||||
|
||||
class CompanyInformativeValidatorByNameFactoryTest extends TestCase
|
||||
{
|
||||
public function dataProvider() {
|
||||
return [
|
||||
'Found Case' => [
|
||||
'name' => 'testCompany',
|
||||
'key' => 'ok',
|
||||
'message' => 'A company with the name is valid.',
|
||||
'isValid' => true,
|
||||
],
|
||||
'Not Found Case' => [
|
||||
'name' => '404 Company',
|
||||
'key' => 'error',
|
||||
'message' => 'A company with the name does not exist!!!',
|
||||
'isValid' => false,
|
||||
],
|
||||
'Invalid Case' => [
|
||||
'name' => '',
|
||||
'key' => 'error',
|
||||
'message' => 'The company 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 CompanyInformativeValidatorByNameFactory($name);
|
||||
$validator = $factory->create();
|
||||
|
||||
$this->assertEquals($validator->getMessage(), $message);
|
||||
$this->assertEquals($validator->getOkStatus(), [$key => $message]);
|
||||
$this->assertEquals($validator->isValid(), $isValid);
|
||||
}
|
||||
}
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use Tests\TestCase;
|
||||
|
||||
use App\Models\Validation\Validators\Informative\Factories\ItemInformativeValidatorByNameFactory;
|
||||
|
||||
class ItemInformativeValidatorByNameFactoryTest 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 testValidatorFactory(string $name, string $key, string $message, bool $isValid): void
|
||||
{
|
||||
$factory = new ItemInformativeValidatorByNameFactory($name);
|
||||
$validator = $factory->create();
|
||||
|
||||
$this->assertEquals($validator->getMessage(), $message);
|
||||
$this->assertEquals($validator->getOkStatus(), [$key => $message]);
|
||||
$this->assertEquals($validator->isValid(), $isValid);
|
||||
}
|
||||
}
|
||||
+47
@@ -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,16 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class ExampleTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* A basic test example.
|
||||
*/
|
||||
public function test_that_true_is_true(): void
|
||||
{
|
||||
$this->assertTrue(true);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
use App\Models\Validation\Validators\OkValidator;
|
||||
|
||||
class OkValidatorTest extends TestCase
|
||||
{
|
||||
public function testIsValid(): void
|
||||
{
|
||||
$validator = new OkValidator();
|
||||
|
||||
$this->assertTrue($validator->isValid());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
use App\Models\Validation\Validators\UpperRangeValidator;
|
||||
use App\Models\Validation\Validators\OkValidator;
|
||||
|
||||
class UpperRangeValidatorTest extends TestCase
|
||||
{
|
||||
public function dataProvider() {
|
||||
return [
|
||||
'Valid Case for 10' => [
|
||||
'value' => 10,
|
||||
'limit' => 10,
|
||||
'isValid' => true,
|
||||
],
|
||||
'Valid Case for 255' => [
|
||||
'value' => 0,
|
||||
'limit' => 255,
|
||||
'isValid' => true,
|
||||
],
|
||||
'Invalid Case for 255' => [
|
||||
'value' => 11,
|
||||
'limit' => 10,
|
||||
'isValid' => false,
|
||||
],
|
||||
'Invalid Case for 255' => [
|
||||
'value' => 256,
|
||||
'limit' => 255,
|
||||
'isValid' => false,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public function setUpValidator(int $value, int $rangeLimit): UpperRangeValidator
|
||||
{
|
||||
return new UpperRangeValidator($value, $rangeLimit, new OkValidator());
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider dataProvider
|
||||
*/
|
||||
public function testGetMessage(int $value, int $limit, bool $isValid): void
|
||||
{
|
||||
$validator = $this->setUpValidator($value, $limit);
|
||||
|
||||
$this->assertEquals($validator->isValid(), $isValid);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user