Skip to content

Commit 0bb9ad5

Browse files
committed
minimize grouping tests
1 parent 990cc91 commit 0bb9ad5

File tree

1 file changed

+26
-89
lines changed

1 file changed

+26
-89
lines changed

tests/GroupingTest.php

Lines changed: 26 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace MiladRahimi\PhpRouter\Tests;
44

5-
use MiladRahimi\PhpRouter\Enums\GroupAttributes;
65
use MiladRahimi\PhpRouter\Enums\HttpMethods;
76
use MiladRahimi\PhpRouter\Router;
87
use MiladRahimi\PhpRouter\Tests\Classes\SampleMiddleware;
@@ -34,17 +33,13 @@ public function test_with_no_attribute()
3433
*/
3534
public function test_with_a_middleware()
3635
{
37-
$middleware = new SampleMiddleware(mt_rand(1, 9999999));
38-
39-
$groupAttributes = [GroupAttributes::MIDDLEWARE => $middleware];
36+
$middleware = new SampleMiddleware(666);
4037

4138
$router = $this->router();
4239

43-
$router->group($groupAttributes, function (Router $router) {
40+
$router->group(['middleware' => $middleware], function (Router $router) {
4441
$router->get('/', $this->controller());
45-
});
46-
47-
$router->dispatch();
42+
})->dispatch();
4843

4944
$this->assertEquals('OK', $this->outputOf($router));
5045
$this->assertContains($middleware->content, SampleMiddleware::$output);
@@ -55,20 +50,14 @@ public function test_with_a_middleware()
5550
*/
5651
public function test_with_route_and_group_middleware()
5752
{
58-
$groupMiddleware = new SampleMiddleware(mt_rand(1, 9999999));
59-
$routeMiddleware = new SampleMiddleware(mt_rand(1, 9999999));
60-
61-
$groupAttributes = [
62-
GroupAttributes::MIDDLEWARE => $groupMiddleware,
63-
];
53+
$groupMiddleware = new SampleMiddleware(13);
54+
$routeMiddleware = new SampleMiddleware(666);
6455

6556
$router = $this->router();
6657

67-
$router->group($groupAttributes, function (Router $router) use ($routeMiddleware) {
58+
$router->group(['middleware' => $groupMiddleware], function (Router $router) use ($routeMiddleware) {
6859
$router->get('/', $this->controller(), $routeMiddleware);
69-
});
70-
71-
$router->dispatch();
60+
})->dispatch();
7261

7362
$this->assertEquals('OK', $this->outputOf($router));
7463
$this->assertContains($groupMiddleware->content, SampleMiddleware::$output);
@@ -83,23 +72,13 @@ public function test_nested_groups_with_middleware()
8372
$group1Middleware = new SampleMiddleware(mt_rand(1, 9999999));
8473
$group2Middleware = new SampleMiddleware(mt_rand(1, 9999999));
8574

86-
$group1Attributes = [
87-
GroupAttributes::MIDDLEWARE => $group1Middleware,
88-
];
89-
90-
$group2Attributes = [
91-
GroupAttributes::MIDDLEWARE => $group2Middleware,
92-
];
93-
9475
$router = $this->router();
9576

96-
$router->group($group1Attributes, function (Router $router) use ($group2Attributes) {
97-
$router->group($group2Attributes, function (Router $router) {
77+
$router->group(['middleware' => $group1Middleware], function (Router $router) use ($group2Middleware) {
78+
$router->group(['middleware' => $group2Middleware], function (Router $router) {
9879
$router->get('/', $this->controller());
9980
});
100-
});
101-
102-
$router->dispatch();
81+
})->dispatch();
10382

10483
$this->assertEquals('OK', $this->outputOf($router));
10584
$this->assertContains($group1Middleware->content, SampleMiddleware::$output);
@@ -113,17 +92,11 @@ public function test_with_a_prefix()
11392
{
11493
$this->mockRequest(HttpMethods::GET, 'http://example.com/group/page');
11594

116-
$groupAttributes = [
117-
GroupAttributes::PREFIX => '/group',
118-
];
119-
12095
$router = $this->router();
12196

122-
$router->group($groupAttributes, function (Router $router) {
97+
$router->group(['prefix' => '/group'], function (Router $router) {
12398
$router->get('/page', $this->controller());
124-
});
125-
126-
$router->dispatch();
99+
})->dispatch();
127100

128101
$this->assertEquals('OK', $this->outputOf($router));
129102
}
@@ -135,23 +108,13 @@ public function test_nested_groups_with_prefix()
135108
{
136109
$this->mockRequest(HttpMethods::GET, 'http://example.com/group1/group2/page');
137110

138-
$group1Attributes = [
139-
GroupAttributes::PREFIX => '/group1',
140-
];
141-
142-
$group2Attributes = [
143-
GroupAttributes::PREFIX => '/group2',
144-
];
145-
146111
$router = $this->router();
147112

148-
$router->group($group1Attributes, function (Router $router) use ($group2Attributes) {
149-
$router->group($group2Attributes, function (Router $router) {
113+
$router->group(['prefix' => '/group1'], function (Router $router) {
114+
$router->group(['prefix' => '/group2'], function (Router $router) {
150115
$router->get('/page', $this->controller());
151116
});
152-
});
153-
154-
$router->dispatch();
117+
})->dispatch();
155118

156119
$this->assertEquals('OK', $this->outputOf($router));
157120
}
@@ -167,9 +130,7 @@ public function test_with_namespace()
167130

168131
$router->group(['namespace' => $namespace], function (Router $router) {
169132
$router->get('/', 'SampleController@home');
170-
});
171-
172-
$router->dispatch();
133+
})->dispatch();
173134

174135
$this->assertEquals('Home', $this->outputOf($router));
175136
}
@@ -181,68 +142,46 @@ public function test_with_domain()
181142
{
182143
$this->mockRequest(HttpMethods::GET, 'http://sub.domain.tld/');
183144

184-
$groupAttributes = [
185-
GroupAttributes::DOMAIN => 'sub.domain.tld',
186-
];
187-
188145
$router = $this->router();
189146

190-
$router->group($groupAttributes, function (Router $router) {
147+
$router->group(['domain' => 'sub.domain.tld'], function (Router $router) {
191148
$router->get('/', $this->controller());
192-
});
193-
194-
$router->dispatch();
149+
})->dispatch();
195150

196151
$this->assertEquals('OK', $this->outputOf($router));
197152
}
198153

199154
/**
200155
* @throws Throwable
201156
*/
202-
public function test_with_group_and_route_domain()
157+
public function test_with_group_and_route_domain_it_should_only_consider_route_domain()
203158
{
204159
$this->mockRequest(HttpMethods::GET, 'http://sub2.domain.com/');
205160

206-
$groupAttributes = [
207-
GroupAttributes::DOMAIN => 'sub1.domain.com',
208-
];
209-
210161
$router = $this->router();
211162

212-
$router->group($groupAttributes, function (Router $router) {
163+
$router->group(['domain' => 'sub1.domain.com'], function (Router $router) {
213164
$router->get('/', $this->controller(), [], 'sub2.domain.com');
214-
});
215-
216-
$router->dispatch();
165+
})->dispatch();
217166

218167
$this->assertEquals('OK', $this->outputOf($router));
219168
}
220169

221170
/**
222171
* @throws Throwable
223172
*/
224-
public function test_nested_groups_with_domain()
173+
public function test_nested_groups_with_domain_it_should_consider_the_inner_group_domain()
225174
{
226175
$this->mockRequest(HttpMethods::GET, 'http://sub2.domain.com/');
227176

228-
$group1Attributes = [
229-
GroupAttributes::DOMAIN => 'sub1.domain.com',
230-
];
231-
232-
$group2Attributes = [
233-
GroupAttributes::DOMAIN => 'sub2.domain.com',
234-
];
235-
236177
$router = $this->router();
237178

238-
$router->group($group1Attributes, function (Router $router) use ($group2Attributes) {
239-
$router->group($group2Attributes, function (Router $router) {
179+
$router->group(['domain' => 'sub1.domain.com'], function (Router $router) {
180+
$router->group(['domain' => 'sub2.domain.com'], function (Router $router) {
240181
$router->get('/', $this->controller());
241182

242183
});
243-
});
244-
245-
$router->dispatch();
184+
})->dispatch();
246185

247186
$this->assertEquals('OK', $this->outputOf($router));
248187
}
@@ -258,9 +197,7 @@ public function test_naming_it_should_remove_existing_name_before_the_group()
258197

259198
$router->group([], function (Router $router) {
260199
$router->get('/', $this->controller());
261-
});
262-
263-
$router->dispatch();
200+
})->dispatch();
264201

265202
$this->assertEquals('OK', $this->outputOf($router));
266203
$this->assertFalse($router->currentRoute()->getName() == 'NameForNothing');

0 commit comments

Comments
 (0)