Refactor the User validator.
continuous-integration/drone/push Build is passing Details

main
KKlochko 2 years ago
parent 41acdd861b
commit 6db3465407

@ -115,7 +115,7 @@ class CartController extends Controller
$cityName = $request->input('cityName') ?? ''; $cityName = $request->input('cityName') ?? '';
// check for not valid user // check for not valid user
$validation = User::validate_with_matrix_username($matrixUsername); $validation = User::validateWithMatrixUsername($matrixUsername);
if(array_key_exists('error', $validation)) if(array_key_exists('error', $validation))
return response()->json($validation); return response()->json($validation);
@ -160,7 +160,7 @@ class CartController extends Controller
$companyName = $request->input('companyName') ?? ''; $companyName = $request->input('companyName') ?? '';
// check for not valid user // check for not valid user
$validation = User::validate_with_matrix_username($matrixUsername); $validation = User::validateWithMatrixUsername($matrixUsername);
if(array_key_exists('error', $validation)) if(array_key_exists('error', $validation))
return response()->json($validation); return response()->json($validation);
@ -203,7 +203,7 @@ class CartController extends Controller
$itemCount = $request->input('itemCount') ?? ''; $itemCount = $request->input('itemCount') ?? '';
// check for not valid user // check for not valid user
$validation = User::validate_with_matrix_username($matrixUsername); $validation = User::validateWithMatrixUsername($matrixUsername);
if(array_key_exists('error', $validation)) if(array_key_exists('error', $validation))
return response()->json($validation); return response()->json($validation);

@ -23,16 +23,16 @@ class User extends Authenticatable
'phone', '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 [ return [
'error' => 'The username is empty, please, write username!!!' '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) if(!$user)
return [ return [

@ -90,31 +90,4 @@ class UserTest extends TestCase
'username' => $this->username '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.');
}
} }

@ -0,0 +1,39 @@
<?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!!!');
}
}
Loading…
Cancel
Save