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\Router; |
13
|
|
|
|
14
|
|
|
use Laminas\Diactoros\ServerRequest; |
15
|
|
|
use Psr\Http\Message\ResponseInterface; |
16
|
|
|
|
17
|
|
|
class IntegrationTest extends TestCase |
18
|
|
|
{ |
19
|
|
|
private $app; |
20
|
|
|
|
21
|
|
|
public function setUp(): void |
22
|
|
|
{ |
23
|
|
|
parent::setUp(); |
24
|
|
|
$this->app = $this->makeApp(['DEBUG' => true]); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function testRoute(): void |
28
|
|
|
{ |
29
|
|
|
$r = $this->get('/'); |
30
|
|
|
$this->assertStringContainsString('index', $r->getBody()->__toString()); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function testRoute2(): void |
34
|
|
|
{ |
35
|
|
|
$r = $this->post('/'); |
36
|
|
|
$this->assertStringContainsString('method', $r->getBody()->__toString()); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function testRoute3(): void |
40
|
|
|
{ |
41
|
|
|
$r = $this->get('/page/test'); |
42
|
|
|
|
43
|
|
|
$this->assertSame('page-test', $r->getBody()->__toString()); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function testRoute4(): void |
47
|
|
|
{ |
48
|
|
|
$r = $this->get('/page/about'); |
49
|
|
|
|
50
|
|
|
$this->assertSame('about', $r->getBody()->__toString()); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function testRoutesWithoutNames(): void |
54
|
|
|
{ |
55
|
|
|
$r = $this->get('/nameless'); |
56
|
|
|
$this->assertSame('index', $r->getBody()->__toString()); |
57
|
|
|
|
58
|
|
|
$r = $this->post('/nameless'); |
59
|
|
|
$this->assertSame('method', $r->getBody()->__toString()); |
60
|
|
|
|
61
|
|
|
$r = $this->get('/nameless/route'); |
62
|
|
|
$this->assertSame('route', $r->getBody()->__toString()); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function get( |
66
|
|
|
$uri, |
67
|
|
|
array $query = [], |
68
|
|
|
array $headers = [], |
69
|
|
|
array $cookies = [] |
70
|
|
|
): ResponseInterface { |
71
|
|
|
return $this->app->getHttp()->handle($this->request($uri, 'GET', $query, $headers, $cookies)); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function getWithAttributes( |
75
|
|
|
$uri, |
76
|
|
|
array $attributes, |
77
|
|
|
array $headers = [] |
78
|
|
|
): ResponseInterface { |
79
|
|
|
$r = $this->request($uri, 'GET', [], $headers, []); |
80
|
|
|
foreach ($attributes as $k => $v) { |
81
|
|
|
$r = $r->withAttribute($k, $v); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $this->app->getHttp()->handle($r); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function post( |
88
|
|
|
$uri, |
89
|
|
|
array $data = [], |
90
|
|
|
array $headers = [], |
91
|
|
|
array $cookies = [] |
92
|
|
|
): ResponseInterface { |
93
|
|
|
return $this->app->getHttp()->handle( |
94
|
|
|
$this->request($uri, 'POST', [], $headers, $cookies)->withParsedBody($data) |
95
|
|
|
); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function request( |
99
|
|
|
$uri, |
100
|
|
|
string $method, |
101
|
|
|
array $query = [], |
102
|
|
|
array $headers = [], |
103
|
|
|
array $cookies = [] |
104
|
|
|
): ServerRequest { |
105
|
|
|
$headers = array_merge([ |
106
|
|
|
'accept-language' => 'en' |
107
|
|
|
], $headers); |
108
|
|
|
|
109
|
|
|
return new ServerRequest( |
110
|
|
|
[], |
111
|
|
|
[], |
112
|
|
|
$uri, |
113
|
|
|
$method, |
114
|
|
|
'php://input', |
115
|
|
|
$headers, |
116
|
|
|
$cookies, |
117
|
|
|
$query |
118
|
|
|
); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public function fetchCookies(array $header) |
122
|
|
|
{ |
123
|
|
|
$result = []; |
124
|
|
|
foreach ($header as $line) { |
125
|
|
|
$cookie = explode('=', $line); |
126
|
|
|
$result[$cookie[0]] = rawurldecode(substr($cookie[1], 0, strpos($cookie[1], ';'))); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return $result; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|