This commit is contained in:
@@ -21,6 +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\CityValidationByName;
|
use App\Models\Validation\CityValidationByName;
|
||||||
use App\Models\Validation\CompanyValidationByName;
|
use App\Models\Validation\CompanyValidationByName;
|
||||||
|
|
||||||
@@ -117,10 +118,9 @@ class CartController extends Controller
|
|||||||
$matrixUsername = $request->input('matrixUsername') ?? '';
|
$matrixUsername = $request->input('matrixUsername') ?? '';
|
||||||
$cityName = $request->input('cityName') ?? '';
|
$cityName = $request->input('cityName') ?? '';
|
||||||
|
|
||||||
// check for not valid user
|
$validator = new UserValidationByMatrixUsername($matrixUsername);
|
||||||
$validation = User::validateWithMatrixUsername($matrixUsername);
|
if(!$validator->isValid())
|
||||||
if(array_key_exists('error', $validation))
|
return response()->json($validator->getMessageMap());
|
||||||
return response()->json($validation);
|
|
||||||
|
|
||||||
$validator = new CityValidationByName($cityName);
|
$validator = new CityValidationByName($cityName);
|
||||||
if(!$validator->isValid())
|
if(!$validator->isValid())
|
||||||
@@ -161,10 +161,9 @@ class CartController extends Controller
|
|||||||
$matrixUsername = $request->input('matrixUsername') ?? '';
|
$matrixUsername = $request->input('matrixUsername') ?? '';
|
||||||
$companyName = $request->input('companyName') ?? '';
|
$companyName = $request->input('companyName') ?? '';
|
||||||
|
|
||||||
// check for not valid user
|
$validator = new UserValidationByMatrixUsername($matrixUsername);
|
||||||
$validation = User::validateWithMatrixUsername($matrixUsername);
|
if(!$validator->isValid())
|
||||||
if(array_key_exists('error', $validation))
|
return response()->json($validator->getMessageMap());
|
||||||
return response()->json($validation);
|
|
||||||
|
|
||||||
$validator = new CompanyValidationByName($companyName);
|
$validator = new CompanyValidationByName($companyName);
|
||||||
if(!$validator->isValid())
|
if(!$validator->isValid())
|
||||||
@@ -203,10 +202,9 @@ class CartController extends Controller
|
|||||||
$itemName = $request->input('itemName') ?? '';
|
$itemName = $request->input('itemName') ?? '';
|
||||||
$itemCount = $request->input('itemCount') ?? '';
|
$itemCount = $request->input('itemCount') ?? '';
|
||||||
|
|
||||||
// check for not valid user
|
$validator = new UserValidationByMatrixUsername($matrixUsername);
|
||||||
$validation = User::validateWithMatrixUsername($matrixUsername);
|
if(!$validator->isValid())
|
||||||
if(array_key_exists('error', $validation))
|
return response()->json($validator->getMessageMap());
|
||||||
return response()->json($validation);
|
|
||||||
|
|
||||||
// check for not valid item
|
// check for not valid item
|
||||||
$validation = Item::validate_with_name($itemName);
|
$validation = Item::validate_with_name($itemName);
|
||||||
|
|||||||
+12
-15
@@ -8,7 +8,9 @@ use Illuminate\Foundation\Auth\User as Authenticatable;
|
|||||||
use Illuminate\Notifications\Notifiable;
|
use Illuminate\Notifications\Notifiable;
|
||||||
use Laravel\Sanctum\HasApiTokens;
|
use Laravel\Sanctum\HasApiTokens;
|
||||||
|
|
||||||
class User extends Authenticatable
|
use App\Models\Validation\ValidationByNameInterface;
|
||||||
|
|
||||||
|
class User extends Authenticatable implements ValidationByNameInterface
|
||||||
{
|
{
|
||||||
use HasApiTokens, HasFactory, Notifiable;
|
use HasApiTokens, HasFactory, Notifiable;
|
||||||
|
|
||||||
@@ -23,24 +25,19 @@ class User extends Authenticatable
|
|||||||
'phone',
|
'phone',
|
||||||
];
|
];
|
||||||
|
|
||||||
public static function validateWithMatrixUsername(string $matrixUsername)
|
|
||||||
{
|
{
|
||||||
$matrixUsername = $matrixUsername ?? '';
|
|
||||||
|
|
||||||
if($matrixUsername == '')
|
|
||||||
return [
|
|
||||||
'error' => 'The username is empty, please, write username!!!'
|
|
||||||
];
|
|
||||||
|
|
||||||
$user = User::where('matrix_username', $matrixUsername)->first();
|
|
||||||
|
|
||||||
if(!$user)
|
public static function isExistByName(string $name): bool
|
||||||
return [
|
{
|
||||||
'error' => 'A user with the username does not exist!!!'
|
$count = User::where('matrix_username', $name)->count();
|
||||||
];
|
|
||||||
|
|
||||||
return [
|
return $count != 0;
|
||||||
'ok' => 'A user with the username is valid.'
|
}
|
||||||
];
|
|
||||||
|
public static function isNameValid(string $name): bool
|
||||||
|
{
|
||||||
|
return $name != '';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,20 @@
|
|||||||
|
<?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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
<?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(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,38 +2,44 @@
|
|||||||
|
|
||||||
namespace Tests\Feature;
|
namespace Tests\Feature;
|
||||||
|
|
||||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
||||||
use Illuminate\Foundation\Testing\WithFaker;
|
|
||||||
use Tests\TestCase;
|
use Tests\TestCase;
|
||||||
|
|
||||||
use Illuminate\Http\Request;
|
use App\Models\Validation\UserValidationByMatrixUsername;
|
||||||
|
|
||||||
use App\Models\User;
|
|
||||||
|
|
||||||
class UserValidationTest extends TestCase
|
class UserValidationTest extends TestCase
|
||||||
{
|
{
|
||||||
public function testEmptyUsernameWithMatrixUsername(): void
|
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 testCityValidationWithName(string $name, string $key, string $message, bool $isValid): void
|
||||||
{
|
{
|
||||||
$json = User::validateWithMatrixUsername('');
|
$validator = new UserValidationByMatrixUsername($name);
|
||||||
|
$json = $validator->getMessageMap();
|
||||||
|
|
||||||
$this->assertEquals($json['error'], 'The username is empty, please, write username!!!');
|
$this->assertEquals($json[$key], $message);
|
||||||
}
|
$this->assertEquals($validator->isValid(), $isValid);
|
||||||
|
|
||||||
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!!!');
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user