|
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; |
|
|
|
|
|
|
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 { |
|
|
|
|
|
|
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
|
|
|
|
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare 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.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/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: