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

HeaderTransportTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 64
c 1
b 0
f 0
dl 0
loc 122
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A testHeaderToken() 0 22 2
A getCore() 0 30 1
A testBadHeaderToken() 0 22 2
A setUp() 0 3 1
A testDeleteToken() 0 16 1
A testCommitToken() 0 15 1
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\HttpTransportInterface;
18
use Spiral\Auth\Middleware\AuthMiddleware;
19
use Spiral\Auth\Transport\HeaderTransport;
20
use Spiral\Auth\TransportRegistry;
21
use Spiral\Core\Container;
22
use Spiral\Http\Config\HttpConfig;
23
use Spiral\Http\Http;
24
use Spiral\Http\Pipeline;
25
use Spiral\Tests\Auth\Diactoros\ResponseFactory;
26
use Laminas\Diactoros\ServerRequest;
27
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...
28
use Spiral\Tests\Auth\Stub\TestStorage;
29
use Spiral\Tests\Auth\Stub\TestToken;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Spiral\Tests\Auth\TestToken. 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...
30
31
class HeaderTransportTest extends TestCase
32
{
33
    private $container;
34
35
    public function setUp(): void
36
    {
37
        $this->container = new Container();
38
    }
39
40
    public function testHeaderToken(): void
41
    {
42
        $http = $this->getCore(new HeaderTransport());
43
44
        $http->setHandler(
45
            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

45
            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...
46
                if ($request->getAttribute('authContext')->getToken() === null) {
47
                    echo 'no token';
48
                } else {
49
                    echo $request->getAttribute('authContext')->getToken()->getID();
50
                    echo ':';
51
                    echo json_encode($request->getAttribute('authContext')->getToken()->getPayload());
52
                }
53
            }
54
        );
55
56
        $response = $http->handle(new ServerRequest([], [], null, 'GET', 'php://input', [
57
            'X-Auth-Token' => 'good-token'
58
        ]));
59
60
        self::assertSame(['text/html; charset=UTF-8'], $response->getHeader('Content-Type'));
61
        self::assertSame('good-token:{"id":"good-token"}', (string)$response->getBody());
62
    }
63
64
    public function testBadHeaderToken(): void
65
    {
66
        $http = $this->getCore(new HeaderTransport());
67
68
        $http->setHandler(
69
            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

69
            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...
70
                if ($request->getAttribute('authContext')->getToken() === null) {
71
                    echo 'no token';
72
                } else {
73
                    echo $request->getAttribute('authContext')->getToken()->getID();
74
                    echo ':';
75
                    echo json_encode($request->getAttribute('authContext')->getToken()->getPayload());
76
                }
77
            }
78
        );
79
80
        $response = $http->handle(new ServerRequest([], [], null, 'GET', 'php://input', [
81
            'X-Auth-Token' => 'bad'
82
        ]));
83
84
        self::assertSame(['text/html; charset=UTF-8'], $response->getHeader('Content-Type'));
85
        self::assertSame('no token', (string)$response->getBody());
86
    }
87
88
    public function testDeleteToken(): void
89
    {
90
        $http = $this->getCore(new HeaderTransport());
91
92
        $http->setHandler(
93
            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

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
                $request->getAttribute('authContext')->close();
95
                echo 'closed';
96
            }
97
        );
98
        $response = $http->handle(new ServerRequest([], [], null, 'GET', 'php://input', [
99
            'X-Auth-Token' => 'bad'
100
        ]));
101
102
        self::assertEmpty($response->getHeader('X-Auth-Token'));
103
        self::assertSame('closed', (string)$response->getBody());
104
    }
105
106
    public function testCommitToken(): void
107
    {
108
        $http = $this->getCore(new HeaderTransport());
109
110
        $http->setHandler(
111
            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

111
            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...
112
                $request->getAttribute('authContext')->start(
113
                    new TestToken('new-token', ['ok' => 1])
114
                );
115
            }
116
        );
117
118
        $response = $http->handle(new ServerRequest([], [], null, 'GET', 'php://input', []));
119
120
        self::assertSame(['new-token'], $response->getHeader('X-Auth-Token'));
121
    }
122
123
    protected function getCore(HttpTransportInterface $transport): Http
124
    {
125
        $config = new HttpConfig([
126
            'basePath'   => '/',
127
            'headers'    => [
128
                'Content-Type' => 'text/html; charset=UTF-8'
129
            ],
130
            'middleware' => [],
131
        ]);
132
133
        $http = new Http(
134
            $config,
135
            new Pipeline($this->container),
136
            new ResponseFactory($config),
137
            $this->container
138
        );
139
140
        $http->getPipeline()->pushMiddleware(
141
            new AuthMiddleware(
142
                $this->container,
143
                new TestProvider(),
144
                new TestStorage(),
145
                $reg = new TransportRegistry()
146
            )
147
        );
148
149
        $reg->setDefaultTransport('transport');
150
        $reg->setTransport('transport', $transport);
151
152
        return $http;
153
    }
154
}
155