Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
25 / 25 |
|
100.00% |
6 / 6 |
CRAP | |
100.00% |
1 / 1 |
| LibraryListBooksContext | |
100.00% |
25 / 25 |
|
100.00% |
6 / 6 |
10 | |
100.00% |
1 / 1 |
| whenICallTheListBooksApi | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| thenIShouldGetBooks | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
| thenIShouldGetTheBookByAuthor | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
4 | |||
| thenIShouldSeeBooks | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| thenIShouldSeeTheBookByAuthor | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| thenIShouldSeeTheCreateButton | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | // ╔════════════════════════════════════════════════════════════╗ |
| 4 | // ║ MIT Licence (#Expat) - https://opensource.org/licenses/MIT ║ |
| 5 | // ║ Copyright 2026 Frederic Poeydomenge <dyno@phexium.com> ║ |
| 6 | // ╚════════════════════════════════════════════════════════════╝ |
| 7 | |
| 8 | declare(strict_types=1); |
| 9 | |
| 10 | namespace Tests\AppDemo\Acceptance; |
| 11 | |
| 12 | use AppDemo\Library\Application\Api\ListBooksController; |
| 13 | use Assert\Assert; |
| 14 | use Behat\Step\Then; |
| 15 | use Behat\Step\When; |
| 16 | use Nyholm\Psr7\Response; |
| 17 | use Nyholm\Psr7\ServerRequest as Request; |
| 18 | use Tests\AppDemo\Acceptance\Trait\BrowsePageTrait; |
| 19 | |
| 20 | final class LibraryListBooksContext extends BehatContext |
| 21 | { |
| 22 | use BrowsePageTrait; |
| 23 | |
| 24 | private Response $response; |
| 25 | |
| 26 | private Request $request; |
| 27 | |
| 28 | #[When('I call the List Books API')] |
| 29 | public function whenICallTheListBooksApi(): void |
| 30 | { |
| 31 | $controller = self::$container->get(ListBooksController::class); |
| 32 | |
| 33 | $this->response = $controller->index(new Request('get', '/'), new Response()); |
| 34 | } |
| 35 | |
| 36 | #[Then('I should get :count books')] |
| 37 | public function thenIShouldGetBooks(int $count): void |
| 38 | { |
| 39 | $body = (string) $this->response->getBody(); |
| 40 | |
| 41 | $data = json_decode($body, true); |
| 42 | |
| 43 | Assert::that($data)->keyExists('count'); |
| 44 | Assert::that($data['count'])->same($count); |
| 45 | |
| 46 | Assert::that($data)->keyExists('books'); |
| 47 | Assert::that($data['books'])->count($count); |
| 48 | } |
| 49 | |
| 50 | #[Then('I should get the book ":title" by ":author"')] |
| 51 | public function thenIShouldGetTheBookByAuthor(string $title, string $author): void |
| 52 | { |
| 53 | $body = (string) $this->response->getBody(); |
| 54 | |
| 55 | $data = json_decode($body, true); |
| 56 | |
| 57 | Assert::that($data)->keyExists('books'); |
| 58 | |
| 59 | $found = false; |
| 60 | |
| 61 | foreach ($data['books'] as $book) { |
| 62 | if ($book['title'] === $title && $book['author'] === $author) { |
| 63 | $found = true; |
| 64 | break; |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | Assert::that($found)->true(); |
| 69 | } |
| 70 | |
| 71 | #[Then('I should see :count books')] |
| 72 | public function thenIShouldSeeBooks(int $count): void |
| 73 | { |
| 74 | $body = (string) $this->response->getBody(); |
| 75 | |
| 76 | Assert::that($body)->contains('Total number of books in the collection: <strong>'.$count.'</strong>'); |
| 77 | } |
| 78 | |
| 79 | #[Then('I should see the book ":title" by ":author"')] |
| 80 | public function thenIShouldSeeTheBookByAuthor(string $title, string $author): void |
| 81 | { |
| 82 | $body = (string) $this->response->getBody(); |
| 83 | |
| 84 | Assert::that($body)->contains(htmlentities($title)); |
| 85 | Assert::that($body)->contains(htmlentities($author)); |
| 86 | } |
| 87 | |
| 88 | #[Then('/I should (not )?see the create button/')] |
| 89 | public function thenIShouldSeeTheCreateButton(bool $not = false): void |
| 90 | { |
| 91 | $body = (string) $this->response->getBody(); |
| 92 | |
| 93 | if ($not === false) { |
| 94 | Assert::that($body)->contains('Add a new book'); |
| 95 | } else { |
| 96 | Assert::that($body)->notContains('Add a new book'); |
| 97 | } |
| 98 | } |
| 99 | } |