Skip to content

Commit 1e395be

Browse files
committed
add tests for current route
1 parent de05664 commit 1e395be

File tree

4 files changed

+66
-7
lines changed

4 files changed

+66
-7
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,8 @@ $router->dispatch();
394394

395395
The group attributes will be explained later in this documentation.
396396

397+
You can use [Attributes](src/Routing/Attributes.php) enum, as well.
398+
397399
### Middleware
398400

399401
PhpRouter supports middleware.

src/Routing/Attributes.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace MiladRahimi\PhpRouter\Routing;
4+
5+
class Attributes
6+
{
7+
const MIDDLEWARE = 'middleware';
8+
const PREFIX = 'prefix';
9+
const DOMAIN = 'domain';
10+
}

src/Routing/State.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@
44

55
class State
66
{
7-
const MIDDLEWARE = 'middleware';
8-
const PREFIX = 'prefix';
9-
const DOMAIN = 'domain';
10-
117
/**
128
* @var string
139
*/
@@ -44,9 +40,9 @@ public function __construct(string $prefix = '', array $middleware = [], ?string
4440
*/
4541
public function append(array $attributes): void
4642
{
47-
$this->domain = $attributes[State::DOMAIN] ?? null;
48-
$this->prefix .= $attributes[State::PREFIX] ?? '';
49-
$this->middleware = array_merge($this->middleware, $attributes[State::MIDDLEWARE] ?? []);
43+
$this->domain = $attributes[Attributes::DOMAIN] ?? null;
44+
$this->prefix .= $attributes[Attributes::PREFIX] ?? '';
45+
$this->middleware = array_merge($this->middleware, $attributes[Attributes::MIDDLEWARE] ?? []);
5046
}
5147

5248
/**

tests/RouteTest.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace MiladRahimi\PhpRouter\Tests;
4+
5+
use MiladRahimi\PhpRouter\Router;
6+
use MiladRahimi\PhpRouter\Routing\Attributes;
7+
use MiladRahimi\PhpRouter\Routing\Route;
8+
use MiladRahimi\PhpRouter\Tests\Common\SampleMiddleware;
9+
use Throwable;
10+
11+
class RouteTest extends TestCase
12+
{
13+
/**
14+
* @throws Throwable
15+
*/
16+
public function test_current_route_for_a_route_with_all_attributes()
17+
{
18+
$this->mockRequest('POST', 'http://shop.com/admin/profile/666');
19+
20+
$router = $this->router();
21+
22+
$attributes = [
23+
Attributes::DOMAIN => 'shop.com',
24+
Attributes::MIDDLEWARE => [SampleMiddleware::class],
25+
Attributes::PREFIX => '/admin',
26+
];
27+
28+
$router->group($attributes, function (Router $router) {
29+
$router->post('/profile/{id}', function (Route $route) {
30+
return $route->__toString();
31+
}, 'admin.profile');
32+
});
33+
34+
$router->dispatch();
35+
36+
$expected = [
37+
'method' => 'POST',
38+
'path' => '/admin/profile/{id}',
39+
'controller' => function () {
40+
return 'Closure';
41+
},
42+
'name' => 'admin.profile',
43+
'middleware' => [SampleMiddleware::class],
44+
'domain' => 'shop.com',
45+
'uri' => '/admin/profile/666',
46+
'parameters' => ['id' => '666'],
47+
];
48+
49+
$this->assertEquals(json_encode($expected), $this->output($router));
50+
}
51+
}

0 commit comments

Comments
 (0)