Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
23 / 23
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
LoanBorrowContext
100.00% covered (success)
100.00%
23 / 23
100.00% covered (success)
100.00%
5 / 5
8
100.00% covered (success)
100.00%
1 / 1
 whenIBorrowTheBook
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
1
 thenTheBookIsMarkedAs
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
4
 iShouldSee
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 iShouldSeeASubmitButton
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\Loan\Application\Http\BorrowBookController;
13use AppDemo\Loan\Domain\LoanRepository;
14use AppDemo\Loan\Domain\LoanStatus;
15use AppDemo\Shared\Domain\Interface\RbacPermissionServiceInterface;
16use AppDemo\Shared\Domain\UserContext;
17use Assert\Assert;
18use Behat\Step\Then;
19use Behat\Step\When;
20use Nyholm\Psr7\Response;
21use Nyholm\Psr7\ServerRequest as Request;
22use Tests\AppDemo\Acceptance\Trait\BrowsePageTrait;
23
24final class LoanBorrowContext extends BehatContext
25{
26    use BrowsePageTrait;
27
28    private Response $response;
29
30    #[When('I borrow the book #:id for :num days')]
31    public function whenIBorrowTheBook(int $bookId, int $days): void
32    {
33        $controller = self::$container->get(BorrowBookController::class);
34        $rbacService = self::$container->get(RbacPermissionServiceInterface::class);
35        $userContext = new UserContext($this->user, $rbacService);
36
37        $request = new Request('POST', '/')
38            ->withAttribute('user_context', $userContext)
39            ->withParsedBody([
40                'book_id' => $bookId,
41                'loan_period' => $days,
42            ])
43        ;
44
45        $this->response = $controller->borrowBook($request, new Response());
46    }
47
48    #[Then('/the book #(\d+) is marked as (available|borrowed)/')]
49    #[Then('/the book #(\d+) should be marked as (available|borrowed)/')]
50    public function thenTheBookIsMarkedAs(int $id, string $status): void
51    {
52        $repository = $this->getRepository();
53        $items = $repository->findAll();
54
55        $status = $status === 'available' ? LoanStatus::Returned : LoanStatus::Active;
56
57        foreach ($items as $item) {
58            if ($item->getBookId()->getValue() === $id) {
59                Assert::that($item->getStatus()->value)->same($status->value);
60            }
61        }
62    }
63
64    #[Then('I should see :text')]
65    public function iShouldSee(string $text): void
66    {
67        $body = (string) $this->response->getBody();
68        Assert::that($body)->contains($text);
69    }
70
71    #[Then('I should see a submit button')]
72    public function iShouldSeeASubmitButton(): void
73    {
74        $body = (string) $this->response->getBody();
75        Assert::that($body)->contains('<button');
76        Assert::that($body)->contains('type="submit"');
77    }
78
79    private function getRepository(): LoanRepository
80    {
81        return self::$container->get(LoanRepository::class);
82    }
83}