Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
LibraryDeleteBookContext
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
5 / 5
7
100.00% covered (success)
100.00%
1 / 1
 whenIDeleteTheBook
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
2
 thenIShouldBeRedirected
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 thenTheBookShouldBeDeleted
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 thenTheHtmlPageShouldRaiseAnException
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getRepository
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
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\Application\Http\DeleteBookController as DeleteBookControllerHtml;
13use AppDemo\Library\Domain\BookRepository;
14use Assert\Assert;
15use Behat\Step\Then;
16use Behat\Step\When;
17use Exception;
18use Nyholm\Psr7\Response;
19use Nyholm\Psr7\ServerRequest as Request;
20use Tests\AppDemo\Acceptance\Trait\BrowsePageTrait;
21
22final class LibraryDeleteBookContext extends BehatContext
23{
24    use BrowsePageTrait;
25
26    private ?Response $response = null;
27
28    private int $initialBookCount;
29
30    private ?Exception $exception = null;
31
32    #[When('I delete the book ID=:id')]
33    public function whenIDeleteTheBook(int $id): void
34    {
35        $this->initialBookCount = $this->getRepository()->findAll()->count();
36
37        $controller = self::$container->get(DeleteBookControllerHtml::class);
38
39        $request = new Request('POST', '/books/'.$id.'/delete');
40
41        try {
42            $this->response = $controller->deleteBook($request, new Response(), ['id' => (string) $id]);
43        } catch (Exception $exception) {
44            $this->exception = $exception;
45        }
46    }
47
48    #[Then('I should be redirected')]
49    public function thenIShouldBeRedirected(): void
50    {
51        Assert::that($this->response)->notNull();
52        Assert::that($this->response->getStatusCode())->same(302);
53    }
54
55    #[Then('/the book should( not)? be deleted/')]
56    public function thenTheBookShouldBeDeleted(bool $not = false): void
57    {
58        $shouldBeDeleted = $not === false;
59
60        $newBookCount = $this->getRepository()->findAll()->count();
61        Assert::that($newBookCount)->same($this->initialBookCount + ($shouldBeDeleted ? -1 : 0));
62    }
63
64    #[Then('the HTML page should raise an exception ":message"')]
65    public function thenTheHtmlPageShouldRaiseAnException(string $message): void
66    {
67        Assert::that($this->exception)->notNull();
68        Assert::that($this->exception)->isInstanceOf(Exception::class);
69        Assert::that($this->exception->getMessage())->same($message);
70    }
71
72    private function getRepository(): BookRepository
73    {
74        return self::$container->get(BookRepository::class);
75    }
76}