|
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 Spiral\Core\Container; |
|
15
|
|
|
use Spiral\Http\Diactoros\UriFactory; |
|
16
|
|
|
use Spiral\Http\Pipeline; |
|
17
|
|
|
use Spiral\Router\RouteGroup; |
|
18
|
|
|
use Spiral\Router\Router; |
|
19
|
|
|
use Spiral\Router\Target\AbstractTarget; |
|
20
|
|
|
use Spiral\Router\Target\Action; |
|
21
|
|
|
use Spiral\Router\UriHandler; |
|
22
|
|
|
use Spiral\Tests\Router\Stub\RoutesTestCore; |
|
23
|
|
|
use Spiral\Tests\Router\Stub\TestMiddleware; |
|
24
|
|
|
|
|
25
|
|
|
class RoutesGroupTest extends TestCase |
|
26
|
|
|
{ |
|
27
|
|
|
public function testCoreString(): void |
|
28
|
|
|
{ |
|
29
|
|
|
$router = new Router('/', new UriHandler(new UriFactory()), new Container()); |
|
30
|
|
|
$group = new RouteGroup(new Container(), $router, new Pipeline(new Container())); |
|
31
|
|
|
|
|
32
|
|
|
$group->setCore(RoutesTestCore::class); |
|
33
|
|
|
|
|
34
|
|
|
$r = $group->createRoute('/', 'controller', 'method'); |
|
35
|
|
|
$t = $this->getProperty($r, 'target'); |
|
36
|
|
|
|
|
37
|
|
|
$this->assertInstanceOf(Action::class, $t); |
|
38
|
|
|
|
|
39
|
|
|
$this->assertSame('controller', $this->getProperty($t, 'controller')); |
|
40
|
|
|
$this->assertSame('method', $this->getProperty($t, 'action')); |
|
41
|
|
|
|
|
42
|
|
|
$this->assertInstanceOf(RoutesTestCore::class, $this->getActionProperty($t, 'core')); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function testCoreObject(): void |
|
46
|
|
|
{ |
|
47
|
|
|
$router = new Router('/', new UriHandler(new UriFactory()), new Container()); |
|
48
|
|
|
$group = new RouteGroup(new Container(), $router, new Pipeline(new Container())); |
|
49
|
|
|
|
|
50
|
|
|
$group->setCore(new RoutesTestCore(new Container())); |
|
51
|
|
|
|
|
52
|
|
|
$r = $group->createRoute('/', 'controller', 'method'); |
|
53
|
|
|
$t = $this->getProperty($r, 'target'); |
|
54
|
|
|
|
|
55
|
|
|
$this->assertInstanceOf(Action::class, $t); |
|
56
|
|
|
|
|
57
|
|
|
$this->assertSame('controller', $this->getProperty($t, 'controller')); |
|
58
|
|
|
$this->assertSame('method', $this->getProperty($t, 'action')); |
|
59
|
|
|
|
|
60
|
|
|
$this->assertInstanceOf(RoutesTestCore::class, $this->getActionProperty($t, 'core')); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function testMiddleware(): void |
|
64
|
|
|
{ |
|
65
|
|
|
$router = new Router('/', new UriHandler(new UriFactory()), new Container()); |
|
66
|
|
|
$group = new RouteGroup(new Container(), $router, new Pipeline(new Container())); |
|
67
|
|
|
$group->addMiddleware(TestMiddleware::class); |
|
68
|
|
|
|
|
69
|
|
|
$r = $group->createRoute('/', 'controller', 'method'); |
|
70
|
|
|
|
|
71
|
|
|
$rl = new \ReflectionObject($r); |
|
72
|
|
|
$m = $rl->getMethod('makePipeline'); |
|
73
|
|
|
$m->setAccessible(true); |
|
74
|
|
|
|
|
75
|
|
|
$p = $m->invoke($r); |
|
76
|
|
|
$m = $this->getProperty($p, 'middleware'); |
|
77
|
|
|
|
|
78
|
|
|
$this->assertCount(1, $m); |
|
79
|
|
|
$this->assertInstanceOf(TestMiddleware::class, $m[0]); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @param object $object |
|
84
|
|
|
* @param string $property |
|
85
|
|
|
* @return mixed |
|
86
|
|
|
* @throws \ReflectionException |
|
87
|
|
|
*/ |
|
88
|
|
|
private function getProperty(object $object, string $property) |
|
89
|
|
|
{ |
|
90
|
|
|
$r = new \ReflectionObject($object); |
|
91
|
|
|
$p = $r->getProperty($property); |
|
92
|
|
|
$p->setAccessible(true); |
|
93
|
|
|
|
|
94
|
|
|
return $p->getValue($object); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @param object $object |
|
99
|
|
|
* @param string $property |
|
100
|
|
|
* @return mixed |
|
101
|
|
|
* @throws \ReflectionException |
|
102
|
|
|
*/ |
|
103
|
|
|
private function getActionProperty(object $object, string $property) |
|
104
|
|
|
{ |
|
105
|
|
|
$r = new \ReflectionClass(AbstractTarget::class); |
|
106
|
|
|
$p = $r->getProperty($property); |
|
107
|
|
|
$p->setAccessible(true); |
|
108
|
|
|
|
|
109
|
|
|
return $p->getValue($object); |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|