From 80a1f5ffcbeaba7096ab14025f54d08b008db09b Mon Sep 17 00:00:00 2001 From: DudiDude Date: Thu, 16 May 2024 11:02:02 +0200 Subject: [PATCH] ControllerServiceAndTestingImproved --- .../Controllers/api/RegisterController.php | 3 +- app/Service/User/UserRegisterService.php | 32 ++-- .../Service/User/RegisterUserServiceTest.php | 159 +++++++++++++++++- 3 files changed, 162 insertions(+), 32 deletions(-) diff --git a/app/Http/Controllers/api/RegisterController.php b/app/Http/Controllers/api/RegisterController.php index 27331dfc..99257cc3 100644 --- a/app/Http/Controllers/api/RegisterController.php +++ b/app/Http/Controllers/api/RegisterController.php @@ -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); diff --git a/app/Service/User/UserRegisterService.php b/app/Service/User/UserRegisterService.php index ccbc025e..f772d253 100644 --- a/app/Service/User/UserRegisterService.php +++ b/app/Service/User/UserRegisterService.php @@ -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; @@ -47,6 +27,8 @@ public function createUser(RegisterRequest $registerData): array $user = User::create($input); + $user = $user->fresh(); + $student->user_id = $user->id; $student->save(); @@ -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(); diff --git a/tests/Feature/Service/User/RegisterUserServiceTest.php b/tests/Feature/Service/User/RegisterUserServiceTest.php index 982144cd..110732ab 100644 --- a/tests/Feature/Service/User/RegisterUserServiceTest.php +++ b/tests/Feature/Service/User/RegisterUserServiceTest.php @@ -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'; @@ -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); } @@ -82,7 +120,8 @@ 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); } @@ -90,7 +129,8 @@ 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' => 'test@example.com', @@ -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', @@ -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', @@ -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', @@ -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' => 'test@example.com', + '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' => 'janesmith@example.com', + 'dni' => '27827083G' + ), + ), + // Missing 'specialization' field + array( + array( + 'username' => 'test_username', + 'specialization' => 'Backend', + 'email' => 'alicebrown@example.com', + 'terms' => 'true', + 'password' => 'password123' + ), + ), + // Missing 'terms' field + array( + array( + 'username' => 'Alice Brown', + 'dni' => '27827083G', + 'email' => 'alicebrown@example.com', + 'terms' => 'true', + 'password' => 'password123' + ), + ), + + // Missing 'terms' field + array( + array( + 'username' => 'Alice Brown', + 'dni' => '27827083G', + 'email' => 'alicebrown@example.com', + 'specialization' => 'Backend', + 'password' => 'password123' + ), + ), + + + ); + + return $array; + } + + + } + + +