Passed
Push — master ( 5679ef...aad9e9 )
by Kirill
04:11
created

FirewallTest::testNoActorException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 19
rs 9.9332
cc 1
nc 1
nop 0
1
<?php
2
3
/**
4
 * Spiral Framework.
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\Tests\Auth;
13
14
use PHPUnit\Framework\TestCase;
15
use Psr\Http\Message\ResponseInterface;
16
use Psr\Http\Message\ServerRequestInterface;
17
use Spiral\Auth\Exception\AuthException;
18
use Spiral\Auth\HttpTransportInterface;
19
use Spiral\Auth\Middleware\AuthMiddleware;
20
use Spiral\Auth\Middleware\Firewall\AbstractFirewall;
21
use Spiral\Auth\Middleware\Firewall\ExceptionFirewall;
22
use Spiral\Auth\Middleware\Firewall\OverwriteFirewall;
23
use Spiral\Auth\Transport\HeaderTransport;
24
use Spiral\Auth\TransportRegistry;
25
use Spiral\Core\Container;
26
use Spiral\Http\Config\HttpConfig;
27
use Spiral\Http\Http;
28
use Spiral\Http\Pipeline;
29
use Spiral\Tests\Auth\Diactoros\ResponseFactory;
30
use Laminas\Diactoros\ServerRequest;
31
use Laminas\Diactoros\Uri;
32
use Spiral\Tests\Auth\Stub\TestProvider;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Spiral\Tests\Auth\TestProvider. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
33
use Spiral\Tests\Auth\Stub\TestStorage;
34
35
class FirewallTest extends TestCase
36
{
37
    private $container;
38
39
    public function setUp(): void
40
    {
41
        $this->container = new Container();
42
    }
43
44
    public function testExceptionOK(): void
45
    {
46
        $http = $this->getCore(
47
            new ExceptionFirewall(new AuthException()),
48
            new HeaderTransport()
49
        );
50
51
        $http->setHandler(
52
            static function (ServerRequestInterface $request, ResponseInterface $response): void {
0 ignored issues
show
Unused Code introduced by
The parameter $response is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

52
            static function (ServerRequestInterface $request, /** @scrutinizer ignore-unused */ ResponseInterface $response): void {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

52
            static function (/** @scrutinizer ignore-unused */ ServerRequestInterface $request, ResponseInterface $response): void {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
53
                echo 'OK';
54
            }
55
        );
56
57
        $response = $http->handle(new ServerRequest([], [], null, 'GET', 'php://input', [
58
            'X-Auth-Token' => 'ok'
59
        ]));
60
61
        self::assertSame('OK', (string)$response->getBody());
62
    }
63
64
    public function testNoActorException(): void
65
    {
66
        $http = $this->getCore(
67
            new ExceptionFirewall(new AuthException('no user')),
68
            new HeaderTransport()
69
        );
70
71
        $http->setHandler(
72
            static function (ServerRequestInterface $request, ResponseInterface $response): void {
0 ignored issues
show
Unused Code introduced by
The parameter $response is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

72
            static function (ServerRequestInterface $request, /** @scrutinizer ignore-unused */ ResponseInterface $response): void {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

72
            static function (/** @scrutinizer ignore-unused */ ServerRequestInterface $request, ResponseInterface $response): void {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
73
                echo 'OK';
74
            }
75
        );
76
77
        $this->expectException(AuthException::class);
78
        $response = $http->handle(new ServerRequest([], [], null, 'GET', 'php://input', [
79
            'X-Auth-Token' => 'no-actor'
80
        ]));
81
82
        self::assertSame('OK', (string)$response->getBody());
83
    }
84
85
    public function testBadTokenException(): void
86
    {
87
        $http = $this->getCore(
88
            new ExceptionFirewall(new AuthException('no user')),
89
            new HeaderTransport()
90
        );
91
92
        $http->setHandler(
93
            static function (ServerRequestInterface $request, ResponseInterface $response): void {
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

93
            static function (/** @scrutinizer ignore-unused */ ServerRequestInterface $request, ResponseInterface $response): void {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $response is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

93
            static function (ServerRequestInterface $request, /** @scrutinizer ignore-unused */ ResponseInterface $response): void {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
94
                echo 'OK';
95
            }
96
        );
97
98
        $this->expectException(AuthException::class);
99
        $response = $http->handle(new ServerRequest([], [], null, 'GET', 'php://input', [
100
            'X-Auth-Token' => 'bad'
101
        ]));
102
103
        self::assertSame('OK', (string)$response->getBody());
104
    }
105
106
    public function testOverwriteOK(): void
107
    {
108
        $http = $this->getCore(
109
            new OverwriteFirewall(new Uri('/login')),
110
            new HeaderTransport()
111
        );
112
113
        $http->setHandler(
114
            static function (ServerRequestInterface $request, ResponseInterface $response): void {
0 ignored issues
show
Unused Code introduced by
The parameter $response is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

114
            static function (ServerRequestInterface $request, /** @scrutinizer ignore-unused */ ResponseInterface $response): void {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
115
                echo $request->getUri();
116
            }
117
        );
118
119
        $response = $http->handle(new ServerRequest([], [], new Uri('/admin'), 'GET', 'php://input', [
120
            'X-Auth-Token' => 'ok'
121
        ]));
122
123
        self::assertSame('/admin', (string)$response->getBody());
124
    }
125
126
    public function testNoActorOverwrite(): void
127
    {
128
        $http = $this->getCore(
129
            new OverwriteFirewall(new Uri('/login')),
130
            new HeaderTransport()
131
        );
132
133
        $http->setHandler(
134
            static function (ServerRequestInterface $request, ResponseInterface $response): void {
0 ignored issues
show
Unused Code introduced by
The parameter $response is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

134
            static function (ServerRequestInterface $request, /** @scrutinizer ignore-unused */ ResponseInterface $response): void {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
135
                echo $request->getUri();
136
            }
137
        );
138
139
        $response = $http->handle(new ServerRequest([], [], new Uri('/admin'), 'GET', 'php://input', [
140
            'X-Auth-Token' => 'no-actor'
141
        ]));
142
143
        self::assertSame('/login', (string)$response->getBody());
144
    }
145
146
    public function testBadTokenOverwrite(): void
147
    {
148
        $http = $this->getCore(
149
            new OverwriteFirewall(new Uri('/login')),
150
            new HeaderTransport()
151
        );
152
153
        $http->setHandler(
154
            static function (ServerRequestInterface $request, ResponseInterface $response): void {
0 ignored issues
show
Unused Code introduced by
The parameter $response is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

154
            static function (ServerRequestInterface $request, /** @scrutinizer ignore-unused */ ResponseInterface $response): void {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
155
                echo $request->getUri();
156
            }
157
        );
158
159
        $response = $http->handle(new ServerRequest([], [], new Uri('/admin'), 'GET', 'php://input', [
160
            'X-Auth-Token' => 'bad'
161
        ]));
162
163
        self::assertSame('/login', (string)$response->getBody());
164
    }
165
166
    protected function getCore(AbstractFirewall $firewall, HttpTransportInterface $transport): Http
167
    {
168
        $config = new HttpConfig([
169
            'basePath'   => '/',
170
            'headers'    => [
171
                'Content-Type' => 'text/html; charset=UTF-8'
172
            ],
173
            'middleware' => [],
174
        ]);
175
176
        $http = new Http(
177
            $config,
178
            new Pipeline($this->container),
179
            new ResponseFactory($config),
180
            $this->container
181
        );
182
183
        $http->getPipeline()->pushMiddleware(
184
            new AuthMiddleware(
185
                $this->container,
186
                new TestProvider(),
187
                new TestStorage(),
188
                $reg = new TransportRegistry()
189
            )
190
        );
191
        $http->getPipeline()->pushMiddleware($firewall);
192
193
        $reg->setTransport('transport', $transport);
194
195
        return $http;
196
    }
197
}
198