/
ExposedRoutesExtractorTest.php
171 lines (138 loc) · 6.15 KB
/
ExposedRoutesExtractorTest.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
165
166
167
168
169
170
171
<?php
/*
* This file is part of the FOSJsRoutingBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace FOS\JsRoutingBundle\Tests\Extractor;
use FOS\JsRoutingBundle\Extractor\ExposedRoutesExtractor;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
/**
* ExposedRoutesExtractorTest class.
*
* @author William DURAND <william.durand1@gmail.com>
*/
class ExposedRoutesExtractorTest extends TestCase
{
private $cacheDir;
public function setUp()
{
if (!class_exists('Symfony\\Component\\Routing\\Route')) {
$this->markTestSkipped('The Routing component is not available.');
}
$this->cacheDir = sys_get_temp_dir();
}
public function testGetRoutes()
{
$expected = new RouteCollection();
$expected->add('literal', new Route('/literal'));
$expected->add('blog_post', new Route('/blog-post/{slug}'));
$expected->add('list', new Route('/literal'));
$router = $this->getRouter($expected);
$extractor = new ExposedRoutesExtractor($router, array('.*'), $this->cacheDir, array());
$expected->addOptions(array('expose' => 'default'));
$this->assertEquals($expected, $extractor->getRoutes());
}
public function testGetRoutesWithPatterns()
{
$expected = new RouteCollection();
$expected->add('hello_you', new Route('/foo', array('_controller' => '')));
$expected->add('hello_123', new Route('/foo', array('_controller' => '')));
$expected->add('hello_world', new Route('/foo', array('_controller' => '')));
$router = $this->getRouter($expected);
$extractor = new ExposedRoutesExtractor($router, array('hello_.*'), $this->cacheDir, array());
$this->assertCount(3, $extractor->getRoutes(), '3 routes match the pattern: "hello_.*"');
$extractor = new ExposedRoutesExtractor($router, array('hello_[0-9]{3}'), $this->cacheDir, array());
$this->assertCount(1, $extractor->getRoutes(), '1 routes match the pattern: "hello_[0-9]{3}"');
$extractor = new ExposedRoutesExtractor($router, array('hello_[0-9]{4}'), $this->cacheDir, array());
$this->assertCount(0, $extractor->getRoutes(), '1 routes match the pattern: "hello_[0-9]{4}"');
$extractor = new ExposedRoutesExtractor($router, array('hello_.+o.+'), $this->cacheDir, array());
$this->assertCount(2, $extractor->getRoutes(), '2 routes match the pattern: "hello_.+o.+"');
$extractor = new ExposedRoutesExtractor($router, array('hello_.+o.+', 'hello_123'), $this->cacheDir, array());
$this->assertCount(3, $extractor->getRoutes(), '3 routes match patterns: "hello_.+o.+" and "hello_123"');
$extractor = new ExposedRoutesExtractor($router, array('hello_.+o.+', 'hello_$'), $this->cacheDir, array());
$this->assertCount(2, $extractor->getRoutes(), '2 routes match patterns: "hello_.+o.+" and "hello_"');
$extractor = new ExposedRoutesExtractor($router, array(), $this->cacheDir, array());
$this->assertCount(0, $extractor->getRoutes(), 'No patterns so no matched routes');
}
public function testGetCachePath()
{
$router = $this->getMockBuilder('Symfony\\Component\\Routing\\Router')
->disableOriginalConstructor()
->getMock();
$extractor = new ExposedRoutesExtractor($router, array(), $this->cacheDir, array());
$this->assertEquals($this->cacheDir . DIRECTORY_SEPARATOR . 'fosJsRouting' . DIRECTORY_SEPARATOR . 'data.json', $extractor->getCachePath(''));
}
/**
* @dataProvider provideTestGetHostOverHttp
*/
public function testGetHostOverHttp($host, $httpPort, $expected)
{
$requestContext = new RequestContext('/app_dev.php', 'GET', $host, 'http', $httpPort);
$router = $this->getMockBuilder('Symfony\\Component\\Routing\\Router')
->disableOriginalConstructor()
->getMock();
$router->expects($this->atLeastOnce())
->method('getContext')
->will($this->returnValue($requestContext));
$extractor = new ExposedRoutesExtractor($router, array(), $this->cacheDir, array());
$this->assertEquals($expected, $extractor->getHost());
}
/**
* @return array
*/
public function provideTestGetHostOverHttp()
{
return array(
'HTTP Standard' => array('127.0.0.1', 80, '127.0.0.1'),
'HTTP Non-Standard' => array('127.0.0.1', 8888, '127.0.0.1:8888'),
);
}
/**
* @dataProvider provideTestGetHostOverHttps
*/
public function testGetHostOverHttps($host, $httpsPort, $expected)
{
$requestContext = new RequestContext('/app_dev.php', 'GET', $host, 'https', 80, $httpsPort);
$router = $this->getMockBuilder('Symfony\\Component\\Routing\\Router')
->disableOriginalConstructor()
->getMock();
$router->expects($this->atLeastOnce())
->method('getContext')
->will($this->returnValue($requestContext));
$extractor = new ExposedRoutesExtractor($router, array(), $this->cacheDir, array());
$this->assertEquals($expected, $extractor->getHost());
}
/**
* @return array
*/
public function provideTestGetHostOverHttps()
{
return array(
'HTTPS Standard' => array('127.0.0.1', 443, '127.0.0.1'),
'HTTPS Non-Standard' => array('127.0.0.1', 9876, '127.0.0.1:9876'),
);
}
/**
* Get a mock object which represents a Router
*
* @return \Symfony\Component\Routing\Router
*/
private function getRouter(RouteCollection $routes)
{
$router = $this->getMockBuilder('Symfony\\Component\\Routing\\Router')
->disableOriginalConstructor()
->getMock();
$router
->expects($this->atLeastOnce())
->method('getRouteCollection')
->will($this->returnValue($routes));
return $router;
}
}