|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Mouf\Mvc\Splash\Services; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\Common\Annotations\AnnotationReader; |
|
6
|
|
|
use Doctrine\Common\Annotations\AnnotationRegistry; |
|
7
|
|
|
use Mouf\Mvc\Splash\Fixtures\TestController; |
|
8
|
|
|
use Mouf\Mvc\Splash\Fixtures\TestController2; |
|
9
|
|
|
use Mouf\Picotainer\Picotainer; |
|
10
|
|
|
|
|
11
|
|
|
class ControllerRegistryTest extends \PHPUnit_Framework_TestCase |
|
12
|
|
|
{ |
|
13
|
|
|
protected function setUp() |
|
14
|
|
|
{ |
|
15
|
|
|
$loader = require __DIR__.'../../../../../../vendor/autoload.php'; |
|
16
|
|
|
AnnotationRegistry::registerLoader(array($loader, 'loadClass')); |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
public function testControllerRegistry() |
|
20
|
|
|
{ |
|
21
|
|
|
$container = new Picotainer([ |
|
22
|
|
|
'controller' => function () { |
|
23
|
|
|
return new TestController(); |
|
24
|
|
|
}, |
|
25
|
|
|
]); |
|
26
|
|
|
|
|
27
|
|
|
$parameterFetcherRegistry = ParameterFetcherRegistry::buildDefaultControllerRegistry(); |
|
28
|
|
|
$controllerAnalyzer = new ControllerAnalyzer($container, $parameterFetcherRegistry, new AnnotationReader()); |
|
29
|
|
|
$controllerRegistry = new ControllerRegistry($controllerAnalyzer); |
|
30
|
|
|
$controllerRegistry->addController('controller'); |
|
31
|
|
|
|
|
32
|
|
|
$urlsList = $controllerRegistry->getUrlsList('foo'); |
|
33
|
|
|
|
|
34
|
|
|
$this->assertCount(1, $urlsList); |
|
35
|
|
|
$this->assertInstanceOf(SplashRoute::class, $urlsList[0]); |
|
36
|
|
|
$this->assertEquals('myurl', $urlsList[0]->getUrl()); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function testControllerRegistryThisParam() |
|
40
|
|
|
{ |
|
41
|
|
|
$container = new Picotainer([ |
|
42
|
|
|
'controller' => function () { |
|
43
|
|
|
return new TestController2(); |
|
44
|
|
|
}, |
|
45
|
|
|
]); |
|
46
|
|
|
|
|
47
|
|
|
$parameterFetcherRegistry = ParameterFetcherRegistry::buildDefaultControllerRegistry(); |
|
48
|
|
|
$controllerAnalyzer = new ControllerAnalyzer($container, $parameterFetcherRegistry, new AnnotationReader()); |
|
49
|
|
|
$controllerRegistry = new ControllerRegistry($controllerAnalyzer, ['controller']); |
|
50
|
|
|
|
|
51
|
|
|
$urlsList = $controllerRegistry->getUrlsList('foo'); |
|
52
|
|
|
|
|
53
|
|
|
$this->assertCount(5, $urlsList); |
|
54
|
|
|
$this->assertInstanceOf(SplashRoute::class, $urlsList[0]); |
|
55
|
|
|
$this->assertEquals('url/42/foo/52', $urlsList[0]->getUrl()); |
|
56
|
|
|
|
|
57
|
|
|
$this->assertInstanceOf(SplashRoute::class, $urlsList[1]); |
|
58
|
|
|
$this->assertEquals('controller/actionAnnotation', $urlsList[1]->getUrl()); |
|
59
|
|
|
|
|
60
|
|
|
$this->assertInstanceOf(SplashRoute::class, $urlsList[2]); |
|
61
|
|
|
$this->assertEquals('controller/', $urlsList[2]->getUrl()); |
|
62
|
|
|
$this->assertInstanceOf(SplashRoute::class, $urlsList[2]); |
|
63
|
|
|
$this->assertEquals('Main page', $urlsList[2]->getTitle()); |
|
64
|
|
|
$this->assertContains('GET', $urlsList[2]->getHttpMethods()); |
|
65
|
|
|
$this->assertContains('POST', $urlsList[2]->getHttpMethods()); |
|
66
|
|
|
$this->assertContains('PUT', $urlsList[2]->getHttpMethods()); |
|
67
|
|
|
$this->assertContains('DELETE', $urlsList[2]->getHttpMethods()); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function testControllerRegistryWithDetector() |
|
71
|
|
|
{ |
|
72
|
|
|
$container = new Picotainer([ |
|
73
|
|
|
'controller' => function () { |
|
74
|
|
|
return new TestController(); |
|
75
|
|
|
}, |
|
76
|
|
|
]); |
|
77
|
|
|
|
|
78
|
|
|
$parameterFetcherRegistry = ParameterFetcherRegistry::buildDefaultControllerRegistry(); |
|
79
|
|
|
|
|
80
|
|
|
$detector = new class implements ControllerDetector |
|
81
|
|
|
{ |
|
82
|
|
|
public function getControllerIdentifiers(ControllerAnalyzer $controllerAnalyzer) : array |
|
83
|
|
|
{ |
|
84
|
|
|
return ['controller']; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public function getExpirationTag() : string |
|
88
|
|
|
{ |
|
89
|
|
|
return ''; |
|
90
|
|
|
} |
|
91
|
|
|
}; |
|
92
|
|
|
|
|
93
|
|
|
$controllerAnalyzer = new ControllerAnalyzer($container, $parameterFetcherRegistry, new AnnotationReader()); |
|
94
|
|
|
$controllerRegistry = new ControllerRegistry($controllerAnalyzer, [], $detector); |
|
95
|
|
|
|
|
96
|
|
|
$urlsList = $controllerRegistry->getUrlsList('foo'); |
|
97
|
|
|
|
|
98
|
|
|
$this->assertCount(1, $urlsList); |
|
99
|
|
|
$this->assertInstanceOf(SplashRoute::class, $urlsList[0]); |
|
100
|
|
|
$this->assertEquals('myurl', $urlsList[0]->getUrl()); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|