1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yiisoft\Yii\Web\Tests\Middleware; |
4
|
|
|
|
5
|
|
|
use Nyholm\Psr7\Factory\Psr17Factory; |
6
|
|
|
use Nyholm\Psr7\Response; |
7
|
|
|
use Nyholm\Psr7\ServerRequest; |
8
|
|
|
use PHPUnit\Framework\TestCase; |
9
|
|
|
use Psr\Http\Message\ResponseInterface; |
10
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
11
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
12
|
|
|
use Yiisoft\Http\Method; |
13
|
|
|
use Yiisoft\Router\RouteNotFoundException; |
14
|
|
|
use Yiisoft\Router\UrlGeneratorInterface; |
15
|
|
|
use Yiisoft\Yii\Web\Middleware\Redirect; |
16
|
|
|
|
17
|
|
|
final class RedirectTest extends TestCase |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @test |
21
|
|
|
*/ |
22
|
|
|
public function invalidArguments(): void |
23
|
|
|
{ |
24
|
|
|
$this->expectException(\InvalidArgumentException::class); |
25
|
|
|
$this->createRedirectMiddleware()->process($this->createRequest(), $this->createRequestHandler()); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @test |
30
|
|
|
*/ |
31
|
|
|
public function generateUri(): void |
32
|
|
|
{ |
33
|
|
|
$middleware = $this->createRedirectMiddleware() |
34
|
|
|
->toRoute('test/route', [ |
35
|
|
|
'param1' => 1, |
36
|
|
|
'param2' => 2, |
37
|
|
|
]); |
38
|
|
|
|
39
|
|
|
$response = $middleware->process($this->createRequest(), $this->createRequestHandler()); |
40
|
|
|
$header = $response->getHeader('Location'); |
41
|
|
|
|
42
|
|
|
$this->assertSame($header[0], 'test/route?param1=1¶m2=2'); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @test |
47
|
|
|
*/ |
48
|
|
|
public function temporaryReturnCode303(): void |
49
|
|
|
{ |
50
|
|
|
$middleware = $this->createRedirectMiddleware() |
51
|
|
|
->toRoute('test/route') |
52
|
|
|
->temporary(); |
53
|
|
|
|
54
|
|
|
$response = $middleware->process($this->createRequest(), $this->createRequestHandler()); |
55
|
|
|
|
56
|
|
|
$this->assertSame($response->getStatusCode(), 303); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @test |
61
|
|
|
*/ |
62
|
|
|
public function permanentReturnCode301(): void |
63
|
|
|
{ |
64
|
|
|
$middleware = $this->createRedirectMiddleware() |
65
|
|
|
->toRoute('test/route') |
66
|
|
|
->permanent(); |
67
|
|
|
|
68
|
|
|
$response = $middleware->process($this->createRequest(), $this->createRequestHandler()); |
69
|
|
|
|
70
|
|
|
$this->assertSame($response->getStatusCode(), 301); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @test |
75
|
|
|
*/ |
76
|
|
|
public function statusReturnCode400(): void |
77
|
|
|
{ |
78
|
|
|
$middleware = $this->createRedirectMiddleware() |
79
|
|
|
->toRoute('test/route') |
80
|
|
|
->status(400); |
81
|
|
|
|
82
|
|
|
$response = $middleware->process($this->createRequest(), $this->createRequestHandler()); |
83
|
|
|
|
84
|
|
|
$this->assertSame($response->getStatusCode(), 400); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @test |
89
|
|
|
*/ |
90
|
|
|
public function setUri(): void |
91
|
|
|
{ |
92
|
|
|
$middleware = $this->createRedirectMiddleware() |
93
|
|
|
->toUrl('test/custom/route'); |
94
|
|
|
|
95
|
|
|
$response = $middleware->process($this->createRequest(), $this->createRequestHandler()); |
96
|
|
|
$header = $response->getHeader('Location'); |
97
|
|
|
|
98
|
|
|
$this->assertSame($header[0], 'test/custom/route'); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
private function createRequestHandler(): RequestHandlerInterface |
102
|
|
|
{ |
103
|
|
|
return new class() implements RequestHandlerInterface { |
104
|
|
|
public function handle(ServerRequestInterface $request): ResponseInterface |
105
|
|
|
{ |
106
|
|
|
return new Response(200); |
107
|
|
|
} |
108
|
|
|
}; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
private function createRequest(string $method = Method::GET, string $uri = '/'): ServerRequestInterface |
112
|
|
|
{ |
113
|
|
|
return new ServerRequest($method, $uri); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
private function createUrlGenerator(): UrlGeneratorInterface |
117
|
|
|
{ |
118
|
|
|
return new class() implements UrlGeneratorInterface { |
119
|
|
|
private $prefix = ''; |
120
|
|
|
public function generate(string $name, array $parameters = []): string |
121
|
|
|
{ |
122
|
|
|
return $name . '?' . http_build_query($parameters); |
123
|
|
|
} |
124
|
|
|
public function getUriPrefix(): string |
125
|
|
|
{ |
126
|
|
|
return $this->prefix; |
127
|
|
|
} |
128
|
|
|
public function setUriPrefix(string $prefix): void |
129
|
|
|
{ |
130
|
|
|
$this->prefix = $prefix; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public function generateAbsolute( |
134
|
|
|
string $name, |
135
|
|
|
array $parameters = [], |
136
|
|
|
string $scheme = null, |
137
|
|
|
string $host = null |
138
|
|
|
): string { |
139
|
|
|
return $name . '?' . http_build_query($parameters); |
140
|
|
|
} |
141
|
|
|
}; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
private function createRedirectMiddleware(): Redirect |
145
|
|
|
{ |
146
|
|
|
return new Redirect(new Psr17Factory(), $this->createUrlGenerator()); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|