Passed
Push — master ( 4cd94b...0d0336 )
by Mihail
02:45
created

SessionAuthenticatedMiddlewareTest::test__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Koded\Session;
4
5
use Koded\Http\{ServerRequest, ServerResponse, StatusCode};
6
use Koded\Stdlib\Config;
7
use PHPUnit\Framework\TestCase;
8
use Psr\Http\Message\{ResponseInterface, ServerRequestInterface};
9
use Psr\Http\Server\RequestHandlerInterface;
10
11
class SessionAuthenticatedMiddlewareTest extends TestCase
12
{
13
14
    /** @var SessionAuthenticatedMiddleware */
15
    private $middleware;
16
17
    public function test__construct()
18
    {
19
        $this->assertAttributeEquals('/signin', 'redirectTo', $this->middleware);
20
    }
21
22
    public function test_process_when_not_authenticated()
23
    {
24
        $handler = new class implements RequestHandlerInterface
25
        {
26
            public function handle(ServerRequestInterface $request): ResponseInterface
27
            {
28
                return new ServerResponse('hello');
29
            }
30
        };
31
32
        $this->assertFalse(isset($_SESSION[SessionAuthenticatedMiddleware::AUTHENTICATED]));
33
34
        $response = $this->middleware->process(new ServerRequest, $handler);
35
36
        $this->assertEquals('/signin', $response->getHeaderLine('location'));
37
        $this->assertSame(StatusCode::TEMPORARY_REDIRECT, $response->getStatusCode());
38
        $this->assertEmpty((string)$response->getBody());
39
    }
40
41
    public function test_process_ajax_when_not_authenticated()
42
    {
43
        $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XmlHttpRequest';
44
45
        $handler = new class implements RequestHandlerInterface
46
        {
47
            public function handle(ServerRequestInterface $request): ResponseInterface
48
            {
49
                return new ServerResponse();
50
            }
51
        };
52
53
        $this->assertFalse(isset($_SESSION[SessionAuthenticatedMiddleware::AUTHENTICATED]));
54
55
        $response = $this->middleware->process(new ServerRequest, $handler);
56
57
        $this->assertEquals('', $response->getHeaderLine('location'));
58
        $this->assertSame(StatusCode::UNAUTHORIZED, $response->getStatusCode());
59
        $this->assertSame('{"location":"/signin","status":401}', (string)$response->getBody());
60
    }
61
62
    public function test_process_when_authenticated()
63
    {
64
        $_SESSION[SessionAuthenticatedMiddleware::AUTHENTICATED] = true;
65
        
66
        $handler = new class implements RequestHandlerInterface
67
        {
68
            public function handle(ServerRequestInterface $request): ResponseInterface
69
            {
70
                return new ServerResponse('hello');
71
            }
72
        };
73
74
        $response = $this->middleware->process(new ServerRequest, $handler);
75
76
        $this->assertEquals('', $response->getHeaderLine('location'));
77
        $this->assertSame(StatusCode::OK, $response->getStatusCode());
78
        $this->assertEquals('hello', (string)$response->getBody());
79
    }
80
81
    protected function setUp()
82
    {
83
        $this->middleware = new SessionAuthenticatedMiddleware((new Config)->import([
84
            SessionAuthenticatedMiddleware::LOGIN_URI => '/signin',
85
        ]));
86
    }
87
}
88