diff --git a/app/Http/Controllers/API/v2/CartController.php b/app/Http/Controllers/API/v2/CartController.php index 43de7b8..7d928cd 100644 --- a/app/Http/Controllers/API/v2/CartController.php +++ b/app/Http/Controllers/API/v2/CartController.php @@ -21,6 +21,7 @@ use App\DotsAPI\Fetcher\v2\AuthApiFetcher; use App\DotsAPI\Fetcher\v2\AuthApiSender; use App\Http\Resources\API\v2\CartItemCollection; +use App\Models\Validation\UserValidationByMatrixUsername; use App\Models\Validation\CityValidationByName; use App\Models\Validation\CompanyValidationByName; @@ -117,10 +118,9 @@ 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); + $validator = new UserValidationByMatrixUsername($matrixUsername); + if(!$validator->isValid()) + return response()->json($validator->getMessageMap()); $validator = new CityValidationByName($cityName); if(!$validator->isValid()) @@ -161,10 +161,9 @@ 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); + $validator = new UserValidationByMatrixUsername($matrixUsername); + if(!$validator->isValid()) + return response()->json($validator->getMessageMap()); $validator = new CompanyValidationByName($companyName); if(!$validator->isValid()) @@ -203,10 +202,9 @@ 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); + $validator = new UserValidationByMatrixUsername($matrixUsername); + if(!$validator->isValid()) + return response()->json($validator->getMessageMap()); // check for not valid item $validation = Item::validate_with_name($itemName); diff --git a/app/Models/User.php b/app/Models/User.php index 24870d6..19a424b 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -8,7 +8,9 @@ use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; use Laravel\Sanctum\HasApiTokens; -class User extends Authenticatable +use App\Models\Validation\ValidationByNameInterface; + +class User extends Authenticatable implements ValidationByNameInterface { use HasApiTokens, HasFactory, Notifiable; @@ -23,24 +25,18 @@ class User extends Authenticatable '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) - return [ - 'error' => 'A user with the username does not exist!!!' - ]; + public static function isExistByName(string $name): bool + { + $count = User::where('matrix_username', $name)->count(); + + return $count != 0; + } - return [ - 'ok' => 'A user with the username is valid.' - ]; + public static function isNameValid(string $name): bool + { + return $name != ''; } } diff --git a/app/Models/Validation/Messages/UserMessagesFactory.php b/app/Models/Validation/Messages/UserMessagesFactory.php new file mode 100644 index 0000000..95671eb --- /dev/null +++ b/app/Models/Validation/Messages/UserMessagesFactory.php @@ -0,0 +1,20 @@ + '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); + } +} + diff --git a/app/Models/Validation/UserValidationByMatrixUsername.php b/app/Models/Validation/UserValidationByMatrixUsername.php new file mode 100644 index 0000000..dc7a640 --- /dev/null +++ b/app/Models/Validation/UserValidationByMatrixUsername.php @@ -0,0 +1,18 @@ +create(), + ); + } +} diff --git a/tests/Feature/Validation/UserValidationTest.php b/tests/Feature/Validation/UserValidationTest.php index c1a3179..db7a302 100644 --- a/tests/Feature/Validation/UserValidationTest.php +++ b/tests/Feature/Validation/UserValidationTest.php @@ -2,38 +2,44 @@ namespace Tests\Feature; -use Illuminate\Foundation\Testing\RefreshDatabase; -use Illuminate\Foundation\Testing\WithFaker; use Tests\TestCase; -use Illuminate\Http\Request; - -use App\Models\User; +use App\Models\Validation\UserValidationByMatrixUsername; class UserValidationTest extends TestCase { - public function testEmptyUsernameWithMatrixUsername(): void - { - $json = User::validateWithMatrixUsername(''); - - $this->assertEquals($json['error'], 'The username is empty, please, write username!!!'); + 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, + ] + ]; } - public function testValidUserWithMatrixUsername(): void + /** + * @dataProvider dataProvider + */ + public function testCityValidationWithName(string $name, string $key, string $message, bool $isValid): 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); + $validator = new UserValidationByMatrixUsername($name); + $json = $validator->getMessageMap(); - $this->assertEquals($json['error'], 'A user with the username does not exist!!!'); + $this->assertEquals($json[$key], $message); + $this->assertEquals($validator->isValid(), $isValid); } }