Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
33 / 33
100.00% covered (success)
100.00%
9 / 9
CRAP
100.00% covered (success)
100.00%
1 / 1
UserContext
100.00% covered (success)
100.00%
33 / 33
100.00% covered (success)
100.00%
9 / 9
9
100.00% covered (success)
100.00%
1 / 1
 iVisitTheLoginPage
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 iSubmitTheLoginForm
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
1
 iSubmitTheLogoutForm
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 theResponseCodeShouldBe
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 iShouldBeRedirectedTo
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 iShouldSee
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 iShouldSeeAnInputField
100.00% covered (success)
100.00%
4 / 4
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
 userShouldOrNotBeAuthenticatedInSession
100.00% covered (success)
100.00%
3 / 3
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\Shared\Domain\Interface\SessionServiceInterface;
13use AppDemo\User\Application\Http\LoginController;
14use Assert\Assert;
15use Behat\Step\Then;
16use Behat\Step\When;
17use Nyholm\Psr7\Response;
18use Nyholm\Psr7\ServerRequest as Request;
19use Tests\AppDemo\Acceptance\Trait\BrowsePageTrait;
20
21final class UserContext extends BehatContext
22{
23    use BrowsePageTrait;
24
25    private Response $response;
26
27    private string $submittedEmail;
28
29    private string $submittedPassword;
30
31    #[When('I visit the login page')]
32    public function iVisitTheLoginPage(): void
33    {
34        $request = new Request('GET', '/auth/login');
35        $response = new Response();
36        $controller = self::$container->get(LoginController::class);
37        $this->response = $controller->showLoginForm($request, $response);
38    }
39
40    #[When('I submit the login form with email :email and password :password')]
41    public function iSubmitTheLoginForm(string $email, string $password): void
42    {
43        $this->submittedEmail = $email;
44        $this->submittedPassword = $password;
45
46        $request = new Request('POST', '/auth/login');
47        $request = $request->withParsedBody([
48            'email' => $email,
49            'password' => $password,
50        ]);
51
52        $response = new Response();
53        $controller = self::$container->get(LoginController::class);
54        $this->response = $controller->login($request, $response);
55    }
56
57    #[When('I submit the logout form')]
58    public function iSubmitTheLogoutForm(): void
59    {
60        $request = new Request('POST', '/auth/logout');
61        $response = new Response();
62        $controller = self::$container->get(LoginController::class);
63        $this->response = $controller->logout($request, $response);
64    }
65
66    #[Then('the response code should be :code')]
67    public function theResponseCodeShouldBe(int $code): void
68    {
69        Assert::that($this->response->getStatusCode())->eq($code);
70    }
71
72    #[Then('I should be redirected to :location')]
73    public function iShouldBeRedirectedTo(string $location): void
74    {
75        $this->theResponseCodeShouldBe(302);
76        Assert::that($this->response->getHeaderLine('Location'))->eq($location);
77    }
78
79    #[Then('I should see :text')]
80    public function iShouldSee(string $text): void
81    {
82        $body = (string) $this->response->getBody();
83        Assert::that($body)->contains($text);
84    }
85
86    #[Then('/I should see an? (email|password) input field/')]
87    public function iShouldSeeAnInputField(string $type): void
88    {
89        $body = (string) $this->response->getBody();
90        Assert::that($body)->contains('<input');
91        Assert::that($body)->contains('type="'.$type.'"');
92        Assert::that($body)->contains('name="'.$type.'"');
93    }
94
95    #[Then('I should see a submit button')]
96    public function iShouldSeeASubmitButton(): void
97    {
98        $body = (string) $this->response->getBody();
99        Assert::that($body)->contains('<button');
100        Assert::that($body)->contains('type="submit"');
101    }
102
103    #[Then('/user should( not)? be authenticated in session/')]
104    public function userShouldOrNotBeAuthenticatedInSession(bool $not = false): void
105    {
106        $auth = $not === false;
107        $sessionService = self::$container->get(SessionServiceInterface::class);
108        Assert::that($sessionService->isUserAuthenticated())->same($auth);
109    }
110}