Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
28 / 28
100.00% covered (success)
100.00%
6 / 6
CRAP
100.00% covered (success)
100.00%
1 / 1
LoanReturnContext
100.00% covered (success)
100.00%
28 / 28
100.00% covered (success)
100.00%
6 / 6
13
100.00% covered (success)
100.00%
1 / 1
 whenIReturnTheBook
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
2
 thenTheLoanIsMarkedAs
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
4
 thenTheBookIsMarkedAs
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
4
 thenIShouldGetTheError
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 thenAnExceptionShouldBeRaised
100.00% covered (success)
100.00%
2 / 2
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\ReturnBookController;
13use AppDemo\Loan\Domain\LoanRepository;
14use AppDemo\Loan\Domain\LoanStatus;
15use AppDemo\Shared\Application\Service\SessionService;
16use AppDemo\Shared\Domain\Interface\RbacPermissionServiceInterface;
17use AppDemo\Shared\Domain\UserContext;
18use Assert\Assert;
19use Behat\Step\Then;
20use Behat\Step\When;
21use Exception;
22use Nyholm\Psr7\Response;
23use Nyholm\Psr7\ServerRequest as Request;
24use Tests\AppDemo\Acceptance\Trait\BrowsePageTrait;
25
26final class LoanReturnContext extends BehatContext
27{
28    use BrowsePageTrait;
29
30    private Response $response;
31    private Exception $exception;
32
33    #[When('I return the loan #:id')]
34    public function whenIReturnTheBook(int $bookId): void
35    {
36        $controller = self::$container->get(ReturnBookController::class);
37        $rbacService = self::$container->get(RbacPermissionServiceInterface::class);
38        $userContext = new UserContext($this->user, $rbacService);
39
40        $request = new Request('POST', '/')
41            ->withAttribute('user_context', $userContext)
42        ;
43
44        try {
45            $this->response = $controller->returnBook($request, new Response(), ['id' => $bookId]);
46        } catch (Exception $exception) {
47            $this->exception = $exception;
48        }
49    }
50
51    #[Then('/the loan #(\d+) is marked as (active|returned)/')]
52    #[Then('/the loan #(\d+) should be marked as (active|returned)/')]
53    public function thenTheLoanIsMarkedAs(int $id, string $status): void
54    {
55        $repository = $this->getRepository();
56        $items = $repository->findAll();
57
58        $status = $status === 'returned' ? LoanStatus::Returned : LoanStatus::Active;
59
60        foreach ($items as $item) {
61            if ($item->getId()->getValue() === $id) {
62                Assert::that($item->getStatus()->value)->same($status->value);
63            }
64        }
65    }
66
67    #[Then('/the book #(\d+) is marked as (available|borrowed)/')]
68    #[Then('/the book #(\d+) should be marked as (available|borrowed)/')]
69    public function thenTheBookIsMarkedAs(int $id, string $status): void
70    {
71        $repository = $this->getRepository();
72        $items = $repository->findAll();
73
74        $status = $status === 'available' ? LoanStatus::Returned : LoanStatus::Active;
75
76        foreach ($items as $item) {
77            if ($item->getBookId()->getValue() === $id) {
78                Assert::that($item->getStatus()->value)->same($status->value);
79            }
80        }
81    }
82
83    #[Then('/I should get the (warning|danger) "(.*)"/')]
84    public function thenIShouldGetTheError(string $type, string $message): void
85    {
86        $session = self::$container->get(SessionService::class);
87        $flashes = $session->getFlash()->get($type);
88        Assert::that(count($flashes))->same(1);
89        Assert::that($message)->inArray($flashes);
90    }
91
92    #[Then('an exception ":message" should be raised')]
93    public function thenAnExceptionShouldBeRaised(string $message): void
94    {
95        Assert::that($this->exception)->isInstanceOf(Exception::class);
96        Assert::that($this->exception->getMessage())->same($message);
97    }
98
99    private function getRepository(): LoanRepository
100    {
101        return self::$container->get(LoanRepository::class);
102    }
103}