Refactor to use UserInformativeValidatorByMatrixUserNameFactory.
continuous-integration/drone/push Build is passing
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -21,7 +21,7 @@ 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\Validators\Informative\Factories\CityInformativeValidatorByNameFactory;
|
use App\Models\Validation\Validators\Informative\Factories\CityInformativeValidatorByNameFactory;
|
||||||
use App\Models\Validation\Validators\Informative\Factories\CompanyInformativeValidatorByNameFactory;
|
use App\Models\Validation\Validators\Informative\Factories\CompanyInformativeValidatorByNameFactory;
|
||||||
use App\Models\Validation\Validators\Informative\Factories\ItemInformativeValidatorByNameFactory;
|
use App\Models\Validation\Validators\Informative\Factories\ItemInformativeValidatorByNameFactory;
|
||||||
@@ -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 CityInformativeValidatorByNameFactory($cityName))->create();
|
foreach($validators as $validator) {
|
||||||
if(!$validator->isValid())
|
if(!$validator->isValid())
|
||||||
return response()->json($validator->getOkStatus());
|
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 CompanyInformativeValidatorByNameFactory($companyName))->create();
|
foreach($validators as $validator) {
|
||||||
if(!$validator->isValid())
|
if(!$validator->isValid())
|
||||||
return response()->json($validator->getOkStatus());
|
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 ItemInformativeValidatorByNameFactory($itemName))->create();
|
foreach($validators as $validator) {
|
||||||
if(!$validator->isValid())
|
if(!$validator->isValid())
|
||||||
return response()->json($validator->getOkStatus());
|
return response()->json($validator->getOkStatus());
|
||||||
|
}
|
||||||
|
|
||||||
if($itemCount == 0)
|
if($itemCount == 0)
|
||||||
return response()->json([
|
return response()->json([
|
||||||
|
|||||||
+1
-15
@@ -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,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,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(),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -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\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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user