Skip to content

Commit

Permalink
ControllerServiceAndTestingImproved
Browse files Browse the repository at this point in the history
  • Loading branch information
JustTheDude001 committed May 16, 2024
1 parent d610ebb commit 80a1f5f
Show file tree
Hide file tree
Showing 3 changed files with 162 additions and 32 deletions.
3 changes: 2 additions & 1 deletion app/Http/Controllers/api/RegisterController.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ public function register(RegisterRequest $request): JsonResponse
try {
$result = $this->userService->createUser($request);

if(empty($result['token']) == False && empty($result['token']) == False){
//if(empty($result['token']) == False && empty($result['token']) == False){
if($result != False){
return $this->sendResponse($result, 'User registered successfully.');
}else{
return $this->sendError(['message'=>'ProcessFailed'], 'User register failed.', 401);
Expand Down
32 changes: 10 additions & 22 deletions app/Service/User/UserRegisterService.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,9 @@
class UserRegisterService
{

public function createUser(RegisterRequest $registerData): array
public function createUser(RegisterRequest $registerData): array | bool
{
/*
$input = $registerData->all();
$input['password'] = bcrypt($input['password']);
DB::transaction(function() {
$user = User::create($input);
$student = new Student();
$student->user_id = $user->id;
$student->save();
$resume = new Resume();
$resume->student_id = $student->id;
$resume->specialization = $input['specialization'];
$resume->save();
});
$success['token'] = $user->createToken('ITAcademy')->accessToken;
$success['email'] = $user->email ?? Null;
return $success;
*/


$input = $registerData->all();
$input['password'] = bcrypt($input['password']);
$user = new User;
Expand All @@ -47,6 +27,8 @@ public function createUser(RegisterRequest $registerData): array

$user = User::create($input);

$user = $user->fresh();

$student->user_id = $user->id;
$student->save();

Expand All @@ -61,12 +43,18 @@ public function createUser(RegisterRequest $registerData): array
} catch (\PDOException $e) {
// Woopsy
DB::rollBack();
return False;
}

/*
if(empty($user->email)){
return False;
}*/

$success['email'] = $user->email;

return $success;
//return False;
/*
$input = $registerData->all();
Expand Down
159 changes: 150 additions & 9 deletions tests/Feature/Service/User/RegisterUserServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,50 @@ class RegisterUserServiceTest extends TestCase
use DatabaseTransactions; //ensures that any database modifications made during testing are reverted once the test is complete

protected $userService;

protected $validationMessage = array (
// username
'username.required' => 'El username es requerido',
'username.string' => 'El username debe ser un texto',
'username.min' => 'El username debe tener al menos 3 caracteres',

// dni
'dni.required' => 'El dni es requerido',
'dni.unique' => 'El dni ya existe',
'dni.string' => 'El dni debe ser un texto',
'dni.max' => 'El dni no debe ser mayor a :max caracteres',
'dni.regex' => 'El dni no debe contener caracteres especiales',

// email
'email.required' => 'El email es requerido',
'email.string' => 'El email debe ser un texto',
'email.max' => 'El email no debe ser mayor a :max caracteres',
'email.unique' => 'El email ya existe',

// password
'password.required' => 'La contraseña es requerida',
'password.confirmed' => 'La confirmacion de la contraseña no coincide',
'password.regex' => 'La contraseña debe contener al menos una mayúscula y un carácter especial, y tener una longitud minima de 8 caracteres',

// specialization
'specialization.required' => 'La especialidad es requerida',
'specialization.in' => 'La especialidad no es valida',

// terms
'terms.required' => 'Debes aceptar los terminos y condiciones',
'terms.in' => 'Debes aceptar los terminos y condiciones',

);


protected function setUp(): void
{
parent::setUp();
$this->userService = new UserRegisterService();
}




private function createUserData()
{
$userData['username'] = 'test_username';
Expand Down Expand Up @@ -62,7 +99,8 @@ public function test_user_creation_with_invalid_data()

$succes = $this->userService->createUser($registerData);

$this->assertEquals(True, empty($success['email']));
//$this->assertEquals(True, empty($success['email']));
$this->assertEquals(False, $succes);

}

Expand All @@ -82,15 +120,17 @@ public function test_user_creation_with_empty_data()

$succes = $this->userService->createUser($registerData);

$this->assertEquals(True, empty($success['email']));
//$this->assertEquals(True, empty($success['email']));
$this->assertEquals(False, $succes);

}

public function test_required_fields_for_user_creation()
{
// Missing 'username' field
$registerData1 = new RegisterRequest([
'username' => 'test_username',
//'username' => 'test_username',
'username' => '',
'specialization' => 'Backend',
'terms' => 'true',
'email' => '[email protected]',
Expand All @@ -101,6 +141,7 @@ public function test_required_fields_for_user_creation()
$this->expectException(Exception::class);
$this->userService->createUser($registerData1);


// Missing 'email' field
$registerData2 = new RegisterRequest([
'username' => 'test_username',
Expand All @@ -111,8 +152,8 @@ public function test_required_fields_for_user_creation()
]);

$this->expectException(Exception::class);
$this->userService->createUser($registerData2);

$succes = $this->userService->createUser($registerData1);
// Missing 'password' field
$registerData3 = new RegisterRequest([
'username' => 'test_username',
Expand All @@ -123,8 +164,8 @@ public function test_required_fields_for_user_creation()
]);

$this->expectException(Exception::class);
$this->userService->createUser($registerData3);

$succes = $this->userService->createUser($registerData1);
// Missing 'dni' field
$registerData4 = new RegisterRequest([
'username' => 'test_username',
Expand Down Expand Up @@ -153,6 +194,106 @@ public function test_required_fields_for_user_creation()
]);

$this->expectException(Exception::class);
$this->userService->createUser($registerData4);
$succes = $this->userService->createUser($registerData1);
}

/**
* @dataProvider required_fields_for_user_creation_provider
*/
public function test_required_fields_for_user_creation_my(array $array)
{
// Missing 'username' field
$registerData1 = new RegisterRequest([
//'username' => 'test_username',
'username' => $array['username'] ?? "",
'specialization' => $array['specialization'] ?? "",
'terms' => $array['terms'] ?? "",
'email' => $array['email'] ?? "",
'password' => $array['password'] ?? "",
'dni' => $array['dni'] ?? "",
]);

//$this->expectException(Exception::class);
$this->userService->createUser($registerData1);
$succes = $this->userService->createUser($registerData1);
$this->assertEquals(False, $succes);
}

static function required_fields_for_user_creation_provider()
{
$array = array(
// Missing 'username' field
array(
array(
//'username' => 'hoho',
'specialization' => 'Backend',
'terms' => 'true',
'email' => '[email protected]',
'password' => 'password123',
'dni' => '27827083G'
),
),
// Missing 'email' field
array(
array(
'username' => 'test_username',
'specialization' => 'Backend',
'terms' => 'true',
'password' => 'password123',
'dni' => '27827083G'
),
),
// Missing 'dni' field
array(
array(
'username' => 'test_username',
'specialization' => 'Backend',
'terms' => 'true',
'email' => '[email protected]',
'dni' => '27827083G'
),
),
// Missing 'specialization' field
array(
array(
'username' => 'test_username',
'specialization' => 'Backend',
'email' => '[email protected]',
'terms' => 'true',
'password' => 'password123'
),
),
// Missing 'terms' field
array(
array(
'username' => 'Alice Brown',
'dni' => '27827083G',
'email' => '[email protected]',
'terms' => 'true',
'password' => 'password123'
),
),

// Missing 'terms' field
array(
array(
'username' => 'Alice Brown',
'dni' => '27827083G',
'email' => '[email protected]',
'specialization' => 'Backend',
'password' => 'password123'
),
),


);

return $array;
}



}



0 comments on commit 80a1f5f

Please sign in to comment.