-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathPresenterTest.php
164 lines (151 loc) · 4.87 KB
/
PresenterTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
/*
* This file is part of Aplus Framework Routing Library.
*
* (c) Natan Felles <natanfelles@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Tests\Routing;
use Framework\HTTP\Request;
use Framework\HTTP\Response;
use Framework\Routing\RouteCollection;
use Framework\Routing\Router;
use PHPUnit\Framework\TestCase;
use Tests\Routing\Support\UsersRouteActionsPresenter;
final class PresenterTest extends TestCase
{
protected Response $response;
protected Router $router;
protected function setUp() : void
{
$this->prepare([
'SERVER_PROTOCOL' => 'HTTP/1.1',
'REQUEST_METHOD' => 'GET',
'HTTP_HOST' => 'admin.domain.tld',
'REQUEST_URI' => '/users',
]);
}
/**
* @param array<string,mixed> $server
*/
protected function prepare(array $server = []) : void
{
foreach ($server as $key => $value) {
$_SERVER[$key] = $value;
}
$this->response = new Response(new Request());
$this->router = new Router($this->response);
$this->router->serve(
'http://admin.domain.tld',
static function (RouteCollection $routes) : void {
$routes->presenter('/users', UsersRouteActionsPresenter::class, 'admin.users');
}
);
}
public function testIndex() : void
{
self::assertSame(
UsersRouteActionsPresenter::class . '::index',
$this->router->match()->run()->getBody()
);
self::assertSame('admin.users.index', $this->router->getMatchedRoute()->getName());
}
public function testNew() : void
{
$this->prepare([
'REQUEST_URI' => '/users/new',
]);
self::assertSame(
UsersRouteActionsPresenter::class . '::new',
$this->router->match()->run()->getBody()
);
self::assertSame('admin.users.new', $this->router->getMatchedRoute()->getName());
}
public function testCreate() : void
{
$this->prepare([
'REQUEST_METHOD' => 'POST',
]);
self::assertSame(
UsersRouteActionsPresenter::class . '::create',
$this->router->match()->run()->getBody()
);
self::assertSame('admin.users.create', $this->router->getMatchedRoute()->getName());
}
public function testShow() : void
{
$this->prepare([
'REQUEST_URI' => '/users/25',
]);
self::assertSame(
UsersRouteActionsPresenter::class . '::show/25',
$this->router->match()->run()->getBody()
);
self::assertSame('admin.users.show', $this->router->getMatchedRoute()->getName());
}
public function testEdit() : void
{
$this->prepare([
'REQUEST_URI' => '/users/25/edit',
]);
self::assertSame(
UsersRouteActionsPresenter::class . '::edit/25',
$this->router->match()->run()->getBody()
);
self::assertSame('admin.users.edit', $this->router->getMatchedRoute()->getName());
}
public function testUpdate() : void
{
$this->prepare([
'REQUEST_METHOD' => 'POST',
'REQUEST_URI' => '/users/25/update',
]);
self::assertSame(
UsersRouteActionsPresenter::class . '::update/25',
$this->router->match()->run()->getBody()
);
self::assertSame('admin.users.update', $this->router->getMatchedRoute()->getName());
}
public function testRemove() : void
{
$this->prepare([
'REQUEST_URI' => '/users/25/remove',
]);
self::assertSame(
UsersRouteActionsPresenter::class . '::remove/25',
$this->router->match()->run()->getBody()
);
self::assertSame('admin.users.remove', $this->router->getMatchedRoute()->getName());
}
public function testDelete() : void
{
$this->prepare([
'REQUEST_METHOD' => 'POST',
'REQUEST_URI' => '/users/25/delete',
]);
self::assertSame(
UsersRouteActionsPresenter::class . '::delete/25',
$this->router->match()->run()->getBody()
);
self::assertSame('admin.users.delete', $this->router->getMatchedRoute()->getName());
}
public function testOptions() : void
{
$this->prepare([
'REQUEST_METHOD' => 'OPTIONS',
'REQUEST_URI' => '/users/25',
]);
$this->router->setAutoOptions();
self::assertSame(
'',
$this->router->match()->run()->getBody()
);
self::assertSame(
'GET, HEAD, OPTIONS',
$this->response->getHeader('Allow')
);
self::assertSame('auto-allow-200', $this->router->getMatchedRoute()->getName());
}
}