Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
26 / 26
100.00% covered (success)
100.00%
6 / 6
CRAP
100.00% covered (success)
100.00%
1 / 1
LibraryListBooksContext
100.00% covered (success)
100.00%
26 / 26
100.00% covered (success)
100.00%
6 / 6
10
100.00% covered (success)
100.00%
1 / 1
 whenICallTheListBooksApi
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 thenIShouldGetBooks
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
1
 thenIShouldGetTheBookByAuthor
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
4
 thenIShouldSeeBooks
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 thenIShouldSeeTheBookByAuthor
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 thenIShouldSeeTheCreateButton
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
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
8declare(strict_types=1);
9
10namespace Tests\AppDemo\Acceptance;
11
12use AppDemo\Library\Presentation\Api\ListBooksController;
13use Assert\Assert;
14use Behat\Step\Then;
15use Behat\Step\When;
16use Nyholm\Psr7\Response;
17use Nyholm\Psr7\ServerRequest as Request;
18use Tests\AppDemo\Acceptance\Trait\BrowsePageTrait;
19
20final 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('pagination');
44        Assert::that($data['pagination'])->keyExists('total_count');
45        Assert::that($data['pagination']['total_count'])->same($count);
46
47        Assert::that($data)->keyExists('books');
48        Assert::that($data['books'])->count($count);
49    }
50
51    #[Then('I should get the book ":title" by ":author"')]
52    public function thenIShouldGetTheBookByAuthor(string $title, string $author): void
53    {
54        $body = (string) $this->response->getBody();
55
56        $data = json_decode($body, true);
57
58        Assert::that($data)->keyExists('books');
59
60        $found = false;
61
62        foreach ($data['books'] as $book) {
63            if ($book['title'] === $title && $book['author'] === $author) {
64                $found = true;
65                break;
66            }
67        }
68
69        Assert::that($found)->true();
70    }
71
72    #[Then('I should see :count books')]
73    public function thenIShouldSeeBooks(int $count): void
74    {
75        $body = (string) $this->response->getBody();
76
77        Assert::that($body)->contains('Total number of books in the collection: <strong>'.$count.'</strong>');
78    }
79
80    #[Then('I should see the book ":title" by ":author"')]
81    public function thenIShouldSeeTheBookByAuthor(string $title, string $author): void
82    {
83        $body = (string) $this->response->getBody();
84
85        Assert::that($body)->contains(htmlentities($title));
86        Assert::that($body)->contains(htmlentities($author));
87    }
88
89    #[Then('/I should (not )?see the create button/')]
90    public function thenIShouldSeeTheCreateButton(bool $not = false): void
91    {
92        $body = (string) $this->response->getBody();
93
94        if ($not === false) {
95            Assert::that($body)->contains('Add a new book');
96        } else {
97            Assert::that($body)->notContains('Add a new book');
98        }
99    }
100}