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

AuthMiddlewareTest::getCore()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
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 15
rs 9.9332
cc 1
nc 1
nop 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\AuthContext;
18
use Spiral\Auth\Middleware\AuthMiddleware;
19
use Spiral\Auth\TransportRegistry;
20
use Spiral\Core\Container;
21
use Spiral\Http\Config\HttpConfig;
22
use Spiral\Http\Http;
23
use Spiral\Http\Pipeline;
24
use Spiral\Tests\Auth\Diactoros\ResponseFactory;
25
use Laminas\Diactoros\ServerRequest;
26
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...
27
use Spiral\Tests\Auth\Stub\TestStorage;
28
29
class AuthMiddlewareTest extends TestCase
30
{
31
    private $container;
32
33
    public function setUp(): void
34
    {
35
        $this->container = new Container();
36
    }
37
38
    public function testAttributeRead(): void
39
    {
40
        $http = $this->getCore([]);
41
        $http->getPipeline()->pushMiddleware(
42
            new AuthMiddleware(
43
                $this->container,
44
                new TestProvider(),
45
                new TestStorage(),
46
                new TransportRegistry()
47
            )
48
        );
49
50
        $http->setHandler(
51
            static function (ServerRequestInterface $request, ResponseInterface $response): void {
52
                $response->getBody()->write(
53
                    get_class($request->getAttribute('authContext'))
54
                );
55
            }
56
        );
57
58
        $response = $http->handle(new ServerRequest());
59
60
        self::assertSame(['text/html; charset=UTF-8'], $response->getHeader('Content-Type'));
61
        self::assertSame(AuthContext::class, (string)$response->getBody());
62
    }
63
64
    public function testNoToken(): void
65
    {
66
        $http = $this->getCore([]);
67
        $http->getPipeline()->pushMiddleware(
68
            new AuthMiddleware(
69
                $this->container,
70
                new TestProvider(),
71
                new TestStorage(),
72
                new TransportRegistry()
73
            )
74
        );
75
76
        $http->setHandler(
77
            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

77
            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...
78
                if ($request->getAttribute('authContext')->getToken() === null) {
79
                    echo 'no token';
80
                }
81
            }
82
        );
83
84
        $response = $http->handle(new ServerRequest());
85
86
        self::assertSame(['text/html; charset=UTF-8'], $response->getHeader('Content-Type'));
87
        self::assertSame('no token', (string)$response->getBody());
88
    }
89
90
    protected function getCore(array $middleware = []): Http
91
    {
92
        $config = new HttpConfig([
93
            'basePath'   => '/',
94
            'headers'    => [
95
                'Content-Type' => 'text/html; charset=UTF-8'
96
            ],
97
            'middleware' => $middleware,
98
        ]);
99
100
        return new Http(
101
            $config,
102
            new Pipeline($this->container),
103
            new ResponseFactory($config),
104
            $this->container
105
        );
106
    }
107
}
108