From 6db3465407926d18ba0ef99c107c2cb7422ed46b Mon Sep 17 00:00:00 2001 From: KKlochko Date: Mon, 14 Aug 2023 20:45:57 +0300 Subject: [PATCH] Refactor the User validator. --- .../Controllers/API/v2/CartController.php | 6 +-- app/Models/User.php | 8 ++-- tests/Feature/UserTest.php | 27 ------------- .../Feature/Validation/UserValidationTest.php | 39 +++++++++++++++++++ 4 files changed, 46 insertions(+), 34 deletions(-) create mode 100644 tests/Feature/Validation/UserValidationTest.php diff --git a/app/Http/Controllers/API/v2/CartController.php b/app/Http/Controllers/API/v2/CartController.php index 0fbc105..186ed94 100644 --- a/app/Http/Controllers/API/v2/CartController.php +++ b/app/Http/Controllers/API/v2/CartController.php @@ -115,7 +115,7 @@ class CartController extends Controller $cityName = $request->input('cityName') ?? ''; // check for not valid user - $validation = User::validate_with_matrix_username($matrixUsername); + $validation = User::validateWithMatrixUsername($matrixUsername); if(array_key_exists('error', $validation)) return response()->json($validation); @@ -160,7 +160,7 @@ class CartController extends Controller $companyName = $request->input('companyName') ?? ''; // check for not valid user - $validation = User::validate_with_matrix_username($matrixUsername); + $validation = User::validateWithMatrixUsername($matrixUsername); if(array_key_exists('error', $validation)) return response()->json($validation); @@ -203,7 +203,7 @@ class CartController extends Controller $itemCount = $request->input('itemCount') ?? ''; // check for not valid user - $validation = User::validate_with_matrix_username($matrixUsername); + $validation = User::validateWithMatrixUsername($matrixUsername); if(array_key_exists('error', $validation)) return response()->json($validation); diff --git a/app/Models/User.php b/app/Models/User.php index 0076f36..24870d6 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -23,16 +23,16 @@ class User extends Authenticatable 'phone', ]; - public static function validate_with_matrix_username(string $matrix_username) + public static function validateWithMatrixUsername(string $matrixUsername) { - $matrix_username = $matrix_username ?? ''; + $matrixUsername = $matrixUsername ?? ''; - if($matrix_username == '') + if($matrixUsername == '') return [ 'error' => 'The username is empty, please, write username!!!' ]; - $user = User::where('matrix_username', $matrix_username)->first(); + $user = User::where('matrix_username', $matrixUsername)->first(); if(!$user) return [ diff --git a/tests/Feature/UserTest.php b/tests/Feature/UserTest.php index e8797ff..51d0097 100644 --- a/tests/Feature/UserTest.php +++ b/tests/Feature/UserTest.php @@ -90,31 +90,4 @@ class UserTest extends TestCase 'username' => $this->username ]); } - - /* User validation */ - - public function test_empty_username_with_matrix_username(): void - { - $json = User::validate_with_matrix_username(''); - - $this->assertEquals($json['error'], 'The username is empty, please, write username!!!'); - } - - public function test_not_existing_user_with_matrix_username(): void - { - $matrixUsername = '@kostia:test.com'; - - $json = User::validate_with_matrix_username($matrixUsername); - - $this->assertEquals($json['error'], 'A user with the username does not exist!!!'); - } - - public function test_valid_user_with_matrix_username(): void - { - $matrixUsername = '@test:test.com'; - - $json = User::validate_with_matrix_username($matrixUsername); - - $this->assertEquals($json['ok'], 'A user with the username is valid.'); - } } diff --git a/tests/Feature/Validation/UserValidationTest.php b/tests/Feature/Validation/UserValidationTest.php new file mode 100644 index 0000000..c1a3179 --- /dev/null +++ b/tests/Feature/Validation/UserValidationTest.php @@ -0,0 +1,39 @@ +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!!!'); + } +}