diff --git a/src/Admin/src/Handler/AdminHandler.php b/src/Admin/src/Handler/AdminHandler.php index 07be306..486fcb2 100644 --- a/src/Admin/src/Handler/AdminHandler.php +++ b/src/Admin/src/Handler/AdminHandler.php @@ -56,7 +56,7 @@ public function delete(ServerRequestInterface $request): ResponseInterface $this->adminService->deleteAdmin($admin); - return $this->infoResponse(Message::ADMIN_DELETED); + return $this->noContentResponse(); } /** diff --git a/src/App/src/Handler/ResponseTrait.php b/src/App/src/Handler/ResponseTrait.php index 9e8a843..09c7d72 100644 --- a/src/App/src/Handler/ResponseTrait.php +++ b/src/App/src/Handler/ResponseTrait.php @@ -19,6 +19,18 @@ trait ResponseTrait protected HalResponseFactory $responseFactory; protected ResourceGenerator $resourceGenerator; + public function emptyResponse(int $status = StatusCodeInterface::STATUS_NO_CONTENT): ResponseInterface + { + return new EmptyResponse($status, ['Content-Type' => 'text/plain']); + } + + public function jsonResponse( + array|string $messages = [], + int $status = StatusCodeInterface::STATUS_OK + ): ResponseInterface { + return new JsonResponse($messages, $status); + } + public function createResponse(ServerRequestInterface $request, mixed $instance): ResponseInterface { return $this->responseFactory->createResponse( @@ -34,9 +46,14 @@ public function createdResponse(ServerRequestInterface $request, mixed $instance return $response->withStatus(StatusCodeInterface::STATUS_CREATED); } + public function noContentResponse(): ResponseInterface + { + return $this->emptyResponse(); + } + public function notFoundResponse(): ResponseInterface { - return new EmptyResponse(StatusCodeInterface::STATUS_NOT_FOUND, ['Content-Type' => 'text/plain']); + return $this->emptyResponse(StatusCodeInterface::STATUS_NOT_FOUND); } public function errorResponse( @@ -61,14 +78,7 @@ public function infoResponse( ], $status); } - public function jsonResponse( - array|string $messages = [], - int $status = StatusCodeInterface::STATUS_OK - ): ResponseInterface { - return new JsonResponse($messages, $status); - } - - public function notAcceptedResponse(string $message): ResponseInterface + public function notAcceptableResponse(string $message): ResponseInterface { return $this->errorResponse($message, StatusCodeInterface::STATUS_NOT_ACCEPTABLE); } diff --git a/src/App/src/Message.php b/src/App/src/Message.php index 71bd1a4..92eab7b 100644 --- a/src/App/src/Message.php +++ b/src/App/src/Message.php @@ -7,11 +7,8 @@ class Message { public const ADMIN_CREATED = 'Admin account has been created.'; - public const ADMIN_DELETED = 'Admin account has been deleted.'; public const ADMIN_NOT_ACTIVATED = 'This account is deactivated.'; public const ADMIN_ROLE_MISSING = 'Admin role \'%s\' is missing.'; - public const AVATAR_CREATED = 'Avatar has been successfully created.'; - public const AVATAR_DELETED = 'Avatar has been successfully deleted.'; public const AVATAR_MISSING = 'This user account has no avatar associated with it.'; public const DUPLICATE_EMAIL = 'An account with this email address already exists.'; public const DUPLICATE_IDENTITY = 'An account with this identity already exists.'; @@ -21,18 +18,14 @@ class Message public const INVALID_ACTIVATION_CODE = 'Invalid activation code.'; public const INVALID_CLIENT_ID = 'Invalid client_id.'; public const INVALID_CONFIG = 'Invalid configuration value: \'%s\''; - public const INVALID_EMAIL = 'Invalid email.'; public const INVALID_VALUE = 'The value specified for \'%s\' is invalid.'; - public const INVALID_IDENTIFIER = 'User cannot be found by supplied identifier.'; public const MAIL_SENT_RECOVER_IDENTITY = 'If the provided email identifies an account in our system, ' . 'you will receive an email with your account\'s identity.'; public const MAIL_SENT_RESET_PASSWORD = 'If the provided email identifies an account in our system, ' . 'you will receive an email with further instructions on resetting your account\'s password.'; public const MAIL_SENT_USER_ACTIVATION = 'User activation mail has been successfully sent to \'%s\''; - public const MAIL_NOT_SENT = 'Could not send mail.'; public const MAIL_NOT_SENT_TO = 'Could not send mail to \'%s\'.'; public const MISSING_CONFIG = 'Missing configuration value: \'%s\'.'; - public const MISSING_PARAMETER = 'Missing parameter: \'%s\''; public const NOT_FOUND_BY_NAME = 'Unable to find %s identified by name: %s'; public const NOT_FOUND_BY_UUID = 'Unable to find %s identified by UUID: %s'; public const RESET_PASSWORD_EXPIRED = 'Password reset request for hash: \'%s\' is invalid (expired).'; @@ -50,11 +43,8 @@ class Message public const USER_NOT_FOUND_BY_EMAIL = 'Could not find account identified by email \'%s\''; public const USER_NOT_FOUND_BY_IDENTITY = 'Could not find account by identity \'%s\''; public const VALIDATOR_MIN_LENGTH = '%s must be at least %d characters long.'; - public const VALIDATOR_MAX_LENGTH = '%s cannot be longer than %d characters.'; - public const VALIDATOR_MIN_MAX_LENGTH = '%s length must be between %d and %d characters.'; public const VALIDATOR_PASSWORD_MISMATCH = 'Password confirmation does not match the provided password.'; public const VALIDATOR_REQUIRED_FIELD = 'This field is required and cannot be empty.'; public const VALIDATOR_REQUIRED_FIELD_BY_NAME = '%s is required and cannot be empty.'; - public const VALIDATOR_SKIP_OR_FILL = 'If this field is specified, then it must be filled in.'; public const VALIDATOR_REQUIRED_UPLOAD = 'A file must be uploaded first.'; } diff --git a/src/App/src/Middleware/ContentNegotiationMiddleware.php b/src/App/src/Middleware/ContentNegotiationMiddleware.php index cd95cc7..53c433b 100644 --- a/src/App/src/Middleware/ContentNegotiationMiddleware.php +++ b/src/App/src/Middleware/ContentNegotiationMiddleware.php @@ -46,7 +46,7 @@ public function process( $accept = $this->formatAcceptRequest($request->getHeaderLine('Accept')); if (! $this->checkAccept($routeName, $accept)) { - return $this->notAcceptedResponse('Not Acceptable'); + return $this->notAcceptableResponse('Not Acceptable'); } $contentType = $request->getHeaderLine('Content-Type'); @@ -58,7 +58,7 @@ public function process( $responseContentType = $response->getHeaderLine('Content-Type'); if (! $this->validateResponseContentType($responseContentType, $accept)) { - return $this->notAcceptedResponse('Unable to resolve Accept header to a representation'); + return $this->notAcceptableResponse('Unable to resolve Accept header to a representation'); } return $response; @@ -80,7 +80,7 @@ public function checkAccept(string $routeName, array $accept): bool } $acceptList = $this->config['default']['Accept'] ?? []; - if (isset($this->config[$routeName])) { + if (! empty($this->config[$routeName]['Accept'])) { $acceptList = $this->config[$routeName]['Accept'] ?? []; } @@ -98,7 +98,7 @@ public function checkContentType(string $routeName, string $contentType): bool } $acceptList = $this->config['default']['Content-Type'] ?? []; - if (isset($this->config[$routeName])) { + if (! empty($this->config[$routeName]['Content-Type'])) { $acceptList = $this->config[$routeName]['Content-Type'] ?? []; } diff --git a/src/User/src/Handler/AccountAvatarHandler.php b/src/User/src/Handler/AccountAvatarHandler.php index d2206ac..be79d6d 100644 --- a/src/User/src/Handler/AccountAvatarHandler.php +++ b/src/User/src/Handler/AccountAvatarHandler.php @@ -50,7 +50,7 @@ public function delete(ServerRequestInterface $request): ResponseInterface $this->userAvatarService->removeAvatar($user); - return $this->infoResponse(Message::AVATAR_DELETED); + return $this->noContentResponse(); } /** diff --git a/src/User/src/Handler/AccountHandler.php b/src/User/src/Handler/AccountHandler.php index 3388d5c..05e8f78 100644 --- a/src/User/src/Handler/AccountHandler.php +++ b/src/User/src/Handler/AccountHandler.php @@ -45,9 +45,9 @@ public function __construct( */ public function delete(ServerRequestInterface $request): ResponseInterface { - $user = $this->userService->deleteUser($request->getAttribute(User::class)); + $this->userService->deleteUser($request->getAttribute(User::class)); - return $this->createResponse($request, $user); + return $this->noContentResponse(); } public function get(ServerRequestInterface $request): ResponseInterface diff --git a/src/User/src/Handler/UserAvatarHandler.php b/src/User/src/Handler/UserAvatarHandler.php index 5a5c6f5..cba8d01 100644 --- a/src/User/src/Handler/UserAvatarHandler.php +++ b/src/User/src/Handler/UserAvatarHandler.php @@ -59,7 +59,7 @@ public function delete(ServerRequestInterface $request): ResponseInterface $this->userAvatarService->removeAvatar($user); - return $this->infoResponse(Message::AVATAR_DELETED); + return $this->noContentResponse(); } /** diff --git a/src/User/src/Handler/UserHandler.php b/src/User/src/Handler/UserHandler.php index d589fc4..1ae8d60 100644 --- a/src/User/src/Handler/UserHandler.php +++ b/src/User/src/Handler/UserHandler.php @@ -56,9 +56,9 @@ public function delete(ServerRequestInterface $request): ResponseInterface throw new NotFoundException(sprintf(Message::NOT_FOUND_BY_UUID, 'user', $uuid)); } - $user = $this->userService->deleteUser($user); + $this->userService->deleteUser($user); - return $this->createResponse($request, $user); + return $this->noContentResponse(); } /** diff --git a/test/Functional/AbstractFunctionalTest.php b/test/Functional/AbstractFunctionalTest.php index f3b8e6f..173f997 100644 --- a/test/Functional/AbstractFunctionalTest.php +++ b/test/Functional/AbstractFunctionalTest.php @@ -241,7 +241,7 @@ protected function put( protected function delete( string $uri, array $queryParams = [], - array $headers = ['Accept' => 'application/json'], + array $headers = ['Accept' => 'application/json, text/plain'], array $cookies = [] ): ResponseInterface { $request = $this->createRequest( @@ -344,6 +344,11 @@ protected function assertResponseGone(ResponseInterface $response): void $this->assertSame(StatusCodeInterface::STATUS_GONE, $response->getStatusCode()); } + protected function assertResponseNoContent(ResponseInterface $response): void + { + $this->assertSame(StatusCodeInterface::STATUS_NO_CONTENT, $response->getStatusCode()); + } + protected function assertBetween(int $value, int $from, int $to): void { $this->assertThat( diff --git a/test/Functional/AdminTest.php b/test/Functional/AdminTest.php index ce6d856..80a0e85 100644 --- a/test/Functional/AdminTest.php +++ b/test/Functional/AdminTest.php @@ -234,7 +234,7 @@ public function testAdminCanDeleteAdminAccount(): void $response = $this->delete('/admin/' . $admin->getUuid()->toString()); - $this->assertResponseOk($response); + $this->assertResponseNoContent($response); $adminRepository = $this->getEntityManager()->getRepository(Admin::class); $admin = $adminRepository->find($admin->getUuid()->toString()); @@ -444,7 +444,7 @@ public function testAdminCanDeleteUserAccount(): void $response = $this->delete('/user/' . $user->getUuid()->toString()); - $this->assertResponseOk($response); + $this->assertResponseNoContent($response); $userRepository = $this->getEntityManager()->getRepository(User::class); $user = $userRepository->find($user->getUuid()->toString()); diff --git a/test/Functional/UserTest.php b/test/Functional/UserTest.php index 95e59d2..dbab610 100644 --- a/test/Functional/UserTest.php +++ b/test/Functional/UserTest.php @@ -211,7 +211,7 @@ public function testDeleteMyAvatar(): void $this->loginAs($user->getIdentity(), self::DEFAULT_PASSWORD); $response = $this->delete('/user/my-avatar'); - $this->assertResponseOk($response); + $this->assertResponseNoContent($response); } public function testActivateMyAccountInvalidCode(): void @@ -289,7 +289,7 @@ public function testDeleteMyAccount(): void $this->loginAs($user->getIdentity(), self::DEFAULT_PASSWORD); $response = $this->delete('/user/my-account'); - $this->assertResponseOk($response); + $this->assertResponseNoContent($response); $userRepository = $this->getEntityManager()->getRepository(User::class); $deletedUser = $userRepository->find($user->getUuid()->toString());