-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
266 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# RESTful Webservices in Symfony | ||
|
||
## Coding Challenge 9 - Let's implement the missing pieces | ||
|
||
### Tasks | ||
|
||
Let's implement the missing pieces: | ||
|
||
- deleting attendees and workshops | ||
- adding/removing an attendee to/from a workshop |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Error; | ||
|
||
final class ApiError | ||
{ | ||
public function __construct( | ||
private readonly string $message, | ||
private readonly string $detail | ||
) { | ||
} | ||
|
||
public function getMessage(): string | ||
{ | ||
return $this->message; | ||
} | ||
|
||
public function getDetail(): string | ||
{ | ||
return $this->detail; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Error; | ||
|
||
final class ApiErrorCollection | ||
{ | ||
private array $errors; | ||
|
||
public function addApiError(ApiError $error): self | ||
{ | ||
$this->errors[] = $error; | ||
|
||
return $this; | ||
} | ||
|
||
public function getErrors(): array | ||
{ | ||
return $this->errors; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\EventListener; | ||
|
||
use App\Error\ApiError; | ||
use App\Error\ApiErrorCollection; | ||
use Symfony\Component\EventDispatcher\Attribute\AsEventListener; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\HttpKernel\Event\ExceptionEvent; | ||
use Symfony\Component\HttpKernel\KernelEvents; | ||
use Symfony\Component\Serializer\Exception\NotEncodableValueException; | ||
use Symfony\Component\Serializer\SerializerInterface; | ||
use Symfony\Component\Validator\ConstraintViolationInterface; | ||
use Symfony\Component\Validator\Exception\ValidationFailedException; | ||
|
||
#[AsEventListener(event: KernelEvents::EXCEPTION, method: 'onKernelException')] | ||
class ExceptionListener | ||
{ | ||
public function __construct( | ||
private readonly SerializerInterface $serializer | ||
) { | ||
} | ||
|
||
public function onKernelException(ExceptionEvent $event): void | ||
{ | ||
$errorCollection = null; | ||
|
||
$throwable = $event->getThrowable(); | ||
|
||
if ($throwable instanceof NotEncodableValueException) { | ||
$errorCollection = $this->handleNotEncodableValueException($throwable); | ||
} | ||
if ($throwable instanceof ValidationFailedException) { | ||
$errorCollection = $this->handleValidationFailedException($throwable); | ||
} | ||
|
||
if (null === $errorCollection) { | ||
$errorCollection = new ApiErrorCollection(); | ||
$errorCollection->addApiError( | ||
new ApiError( | ||
'Error.', | ||
$throwable->getMessage() | ||
) | ||
); | ||
} | ||
|
||
$serializedErrors = $this->serializer->serialize($errorCollection, $event->getRequest()->getRequestFormat()); | ||
|
||
$event->setResponse(new Response($serializedErrors, Response::HTTP_UNPROCESSABLE_ENTITY)); | ||
} | ||
|
||
private function handleValidationFailedException(ValidationFailedException $validationFailedException): ApiErrorCollection | ||
{ | ||
$errorCollection = new ApiErrorCollection(); | ||
|
||
/* @var ConstraintViolationInterface $violation */ | ||
foreach ($validationFailedException->getViolations() as $violation) { | ||
$errorCollection->addApiError( | ||
new ApiError( | ||
'Validation failed.', | ||
$violation->getPropertyPath().': '.$violation->getMessage() | ||
) | ||
); | ||
} | ||
|
||
return $errorCollection; | ||
} | ||
|
||
private function handleNotEncodableValueException(NotEncodableValueException $notEncodableValueException): ApiErrorCollection | ||
{ | ||
$errorCollection = new ApiErrorCollection(); | ||
|
||
$errorCollection->addApiError( | ||
new ApiError( | ||
'Encoding failed.', | ||
$notEncodableValueException->getMessage() | ||
) | ||
); | ||
|
||
return $errorCollection; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,16 +47,16 @@ public function test_it_should_throw_an_UnprocessableEntityHttpException(string | |
$this->browser->request('POST', '/attendees', [], [], [], $requestBody); | ||
|
||
static::assertResponseStatusCodeSame(422); | ||
static::assertStringContainsString( | ||
'UnprocessableEntityHttpException', | ||
$this->browser->getResponse()->getContent() | ||
); | ||
|
||
$this->assertMatchesJsonSnapshot($this->browser->getResponse()->getContent()); | ||
} | ||
|
||
public static function provideUnprocessableAttendeeData(): \Generator | ||
{ | ||
yield 'no data' => ['']; | ||
yield 'empty data' => ['{}']; | ||
yield 'wrong json one' => ['{']; | ||
yield 'wrong json two' => ['}']; | ||
yield 'missing firstname' => ['{"lastname": "Paulsen", "email": "[email protected]"}']; | ||
yield 'missing lastname' => ['{"firstname": "Paul", "email": "[email protected]"}']; | ||
yield 'missing email' => ['{"firstname": "Paul", "lastname": "Paulsen"}']; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
...test_it_should_throw_an_UnprocessableEntityHttpException with data set empty data__1.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"errors": [ | ||
{ | ||
"message": "Validation failed.", | ||
"detail": "firstname: This value should not be blank." | ||
}, | ||
{ | ||
"message": "Validation failed.", | ||
"detail": "lastname: This value should not be blank." | ||
}, | ||
{ | ||
"message": "Validation failed.", | ||
"detail": "email: This value should not be blank." | ||
} | ||
] | ||
} |
8 changes: 8 additions & 0 deletions
8
...t_it_should_throw_an_UnprocessableEntityHttpException with data set missing email__1.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"errors": [ | ||
{ | ||
"message": "Validation failed.", | ||
"detail": "email: This value should not be blank." | ||
} | ||
] | ||
} |
8 changes: 8 additions & 0 deletions
8
..._should_throw_an_UnprocessableEntityHttpException with data set missing firstname__1.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"errors": [ | ||
{ | ||
"message": "Validation failed.", | ||
"detail": "firstname: This value should not be blank." | ||
} | ||
] | ||
} |
8 changes: 8 additions & 0 deletions
8
...t_should_throw_an_UnprocessableEntityHttpException with data set missing lastname__1.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"errors": [ | ||
{ | ||
"message": "Validation failed.", | ||
"detail": "lastname: This value should not be blank." | ||
} | ||
] | ||
} |
8 changes: 8 additions & 0 deletions
8
...t__test_it_should_throw_an_UnprocessableEntityHttpException with data set no data__1.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"errors": [ | ||
{ | ||
"message": "Encoding failed.", | ||
"detail": "Syntax error" | ||
} | ||
] | ||
} |
8 changes: 8 additions & 0 deletions
8
...est_it_should_throw_an_UnprocessableEntityHttpException with data set wrong email__1.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"errors": [ | ||
{ | ||
"message": "Validation failed.", | ||
"detail": "email: This value is not a valid email address." | ||
} | ||
] | ||
} |
8 changes: 8 additions & 0 deletions
8
..._it_should_throw_an_UnprocessableEntityHttpException with data set wrong json one__1.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"errors": [ | ||
{ | ||
"message": "Encoding failed.", | ||
"detail": "Syntax error" | ||
} | ||
] | ||
} |
8 changes: 8 additions & 0 deletions
8
..._it_should_throw_an_UnprocessableEntityHttpException with data set wrong json two__1.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"errors": [ | ||
{ | ||
"message": "Encoding failed.", | ||
"detail": "Syntax error" | ||
} | ||
] | ||
} |
8 changes: 8 additions & 0 deletions
8
...t__test_it_should_throw_an_UnprocessableEntityHttpException with data set no data__1.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"errors": [ | ||
{ | ||
"message": "Encoding failed.", | ||
"detail": "Syntax error" | ||
} | ||
] | ||
} |
8 changes: 8 additions & 0 deletions
8
...est_it_should_throw_an_UnprocessableEntityHttpException with data set wrong email__1.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"errors": [ | ||
{ | ||
"message": "Validation failed.", | ||
"detail": "email: This value is not a valid email address." | ||
} | ||
] | ||
} |
8 changes: 8 additions & 0 deletions
8
..._it_should_throw_an_UnprocessableEntityHttpException with data set wrong json one__1.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"errors": [ | ||
{ | ||
"message": "Encoding failed.", | ||
"detail": "Syntax error" | ||
} | ||
] | ||
} |
8 changes: 8 additions & 0 deletions
8
..._it_should_throw_an_UnprocessableEntityHttpException with data set wrong json two__1.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"errors": [ | ||
{ | ||
"message": "Encoding failed.", | ||
"detail": "Syntax error" | ||
} | ||
] | ||
} |