DefaultRouteGeneratorTest.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. <?php
  2. /*
  3. * This file is part of the Sonata package.
  4. *
  5. * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Sonata\AdminBundle\Tests\Route;
  11. use Sonata\AdminBundle\Route\DefaultRouteGenerator;
  12. use Sonata\AdminBundle\Route\RouteCollection;
  13. use Sonata\AdminBundle\Route\RoutesCache;
  14. use Symfony\Component\Routing\Route;
  15. class DefaultRouteGeneratorTest extends \PHPUnit_Framework_TestCase
  16. {
  17. protected $cacheTempFolder;
  18. public function setUp()
  19. {
  20. $this->cacheTempFolder = sys_get_temp_dir().'/sonata_test_route';
  21. exec('rm -rf '.$this->cacheTempFolder);
  22. }
  23. public function testGenerate()
  24. {
  25. $router = $this->getMock('\Symfony\Component\Routing\RouterInterface');
  26. $router->expects($this->once())->method('generate')->will($this->returnValue('/foo/bar'));
  27. $cache = new RoutesCache($this->cacheTempFolder, true);
  28. $generator = new DefaultRouteGenerator($router, $cache);
  29. $this->assertEquals('/foo/bar', $generator->generate('foo_bar'));
  30. }
  31. /**
  32. * @dataProvider getGenerateUrlTests
  33. */
  34. public function testGenerateUrl($expected, $name, array $parameters)
  35. {
  36. $childCollection = new RouteCollection('base.Code.Foo|base.Code.Bar', 'admin_acme_child', '/foo/', 'BundleName:ControllerName');
  37. $childCollection->add('bar');
  38. $collection = new RouteCollection('base.Code.Foo', 'admin_acme', '/', 'BundleName:ControllerName');
  39. $collection->add('foo');
  40. $collection->addCollection($childCollection);
  41. $admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  42. $admin->expects($this->any())->method('isChild')->will($this->returnValue(false));
  43. $admin->expects($this->any())->method('getCode')->will($this->returnValue('base.Code.Foo'));
  44. $admin->expects($this->once())->method('hasParentFieldDescription')->will($this->returnValue(false));
  45. $admin->expects($this->once())->method('hasRequest')->will($this->returnValue(true));
  46. $admin->expects($this->any())->method('getUniqid')->will($this->returnValue('foo_uniqueid'));
  47. $admin->expects($this->any())->method('getCode')->will($this->returnValue('foo_code'));
  48. $admin->expects($this->once())->method('getPersistentParameters')->will($this->returnValue(array('abc'=>'a123', 'efg'=>'e456')));
  49. $admin->expects($this->any())->method('getRoutes')->will($this->returnValue($collection));
  50. $admin->expects($this->any())->method('getExtensions')->will($this->returnValue(array()));
  51. $router = $this->getMock('Symfony\Component\Routing\RouterInterface');
  52. $router->expects($this->once())
  53. ->method('generate')
  54. ->will($this->returnCallback(function($name, array $parameters = array()) {
  55. $params = '';
  56. if (!empty($parameters)) {
  57. $params .= '?'.http_build_query($parameters);
  58. }
  59. switch ($name) {
  60. case 'admin_acme_foo':
  61. return '/foo'.$params;
  62. case 'admin_acme_child_bar':
  63. return '/foo/bar'.$params;
  64. }
  65. return null;
  66. }));
  67. $cache = new RoutesCache($this->cacheTempFolder, true);
  68. $generator = new DefaultRouteGenerator($router, $cache);
  69. $this->assertEquals($expected, $generator->generateUrl($admin, $name, $parameters));
  70. }
  71. public function getGenerateUrlTests()
  72. {
  73. return array(
  74. array('/foo?abc=a123&efg=e456&default_param=default_val', 'foo', array('default_param'=>'default_val')),
  75. array('/foo/bar?abc=a123&efg=e456&default_param=default_val', 'base.Code.Bar.bar', array('default_param'=>'default_val')),
  76. );
  77. }
  78. public function testGenerateUrlWithException()
  79. {
  80. $this->setExpectedException('RuntimeException', 'unable to find the route `base.Code.Route.foo`');
  81. $admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  82. $admin->expects($this->any())->method('isChild')->will($this->returnValue(false));
  83. $admin->expects($this->any())->method('getCode')->will($this->returnValue('base.Code.Route'));
  84. $admin->expects($this->once())->method('hasParentFieldDescription')->will($this->returnValue(false));
  85. $admin->expects($this->once())->method('hasRequest')->will($this->returnValue(true));
  86. $admin->expects($this->once())->method('getPersistentParameters')->will($this->returnValue(array()));
  87. $admin->expects($this->exactly(2))->method('getRoutes')->will($this->returnValue(new RouteCollection('base.Code.Route', 'baseRouteName', 'baseRoutePattern', 'BundleName:ControllerName')));
  88. $admin->expects($this->any())->method('getExtensions')->will($this->returnValue(array()));
  89. $router = $this->getMock('Symfony\Component\Routing\RouterInterface');
  90. $cache = new RoutesCache($this->cacheTempFolder, true);
  91. $generator = new DefaultRouteGenerator($router, $cache);
  92. $generator->generateUrl($admin, 'foo', array());
  93. }
  94. /**
  95. * @dataProvider getGenerateUrlChildTests
  96. */
  97. public function testGenerateUrlChild($type, $expected, $name, array $parameters)
  98. {
  99. $childCollection = new RouteCollection('base.Code.Parent|base.Code.Child', 'admin_acme_child', '/foo/', 'BundleName:ControllerName');
  100. $childCollection->add('bar');
  101. $collection = new RouteCollection('base.Code.Parent', 'admin_acme', '/', 'BundleName:ControllerName');
  102. $collection->add('foo');
  103. $collection->addCollection($childCollection);
  104. $admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  105. $admin->expects($this->any())->method('isChild')->will($this->returnValue(true));
  106. $admin->expects($this->any())->method('getCode')->will($this->returnValue('base.Code.Child'));
  107. $admin->expects($this->any())->method('getBaseCodeRoute')->will($this->returnValue('base.Code.Parent|base.Code.Child'));
  108. $admin->expects($this->any())->method('getIdParameter')->will($this->returnValue('id'));
  109. $admin->expects($this->any())->method('hasParentFieldDescription')->will($this->returnValue(false));
  110. $admin->expects($this->any())->method('hasRequest')->will($this->returnValue(true));
  111. $admin->expects($this->any())->method('getUniqid')->will($this->returnValue('foo_uniqueid'));
  112. $admin->expects($this->any())->method('getCode')->will($this->returnValue('foo_code'));
  113. $admin->expects($this->any())->method('getPersistentParameters')->will($this->returnValue(array('abc'=>'a123', 'efg'=>'e456')));
  114. $admin->expects($this->any())->method('getRoutes')->will($this->returnValue($childCollection));
  115. $admin->expects($this->any())->method('getExtensions')->will($this->returnValue(array()));
  116. $parentAdmin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  117. $parentAdmin->expects($this->any())->method('getIdParameter')->will($this->returnValue('childId'));
  118. $parentAdmin->expects($this->any())->method('getRoutes')->will($this->returnValue($collection));
  119. $parentAdmin->expects($this->any())->method('getCode')->will($this->returnValue('base.Code.Parent'));
  120. $parentAdmin->expects($this->any())->method('getExtensions')->will($this->returnValue(array()));
  121. // no request attached in this test, so this will not be used
  122. $parentAdmin->expects($this->never())->method('getPersistentParameters')->will($this->returnValue(array('from'=>'parent')));
  123. $request = $this->getMock('Symfony\Component\HttpFoundation\Request');
  124. $request->attributes = $this->getMock('Symfony\Component\HttpFoundation\ParameterBag');
  125. $request->attributes->expects($this->any())->method('has')->will($this->returnValue(true));
  126. $request->attributes->expects($this->any())
  127. ->method('get')
  128. ->will($this->returnCallback(function ($key) {
  129. if ($key == 'childId') {
  130. return '987654';
  131. }
  132. return null;
  133. }));
  134. $admin->expects($this->any())->method('getRequest')->will($this->returnValue($request));
  135. $admin->expects($this->any())->method('getParent')->will($this->returnValue($parentAdmin));
  136. $router = $this->getMock('\Symfony\Component\Routing\RouterInterface');
  137. $router->expects($this->once())
  138. ->method('generate')
  139. ->will($this->returnCallback(function($name, array $parameters = array()) {
  140. $params = '';
  141. if (!empty($parameters)) {
  142. $params .= '?'.http_build_query($parameters);
  143. }
  144. switch ($name) {
  145. case 'admin_acme_foo':
  146. return '/foo'.$params;
  147. case 'admin_acme_child_bar':
  148. return '/foo/bar'.$params;
  149. }
  150. return null;
  151. }));
  152. $cache = new RoutesCache($this->cacheTempFolder, true);
  153. $generator = new DefaultRouteGenerator($router, $cache);
  154. $this->assertEquals($expected, $generator->generateUrl($type == 'child' ? $admin : $parentAdmin, $name, $parameters));
  155. }
  156. public function getGenerateUrlChildTests()
  157. {
  158. return array(
  159. array('parent', '/foo?id=123&default_param=default_val', 'foo', array('id'=>123, 'default_param'=>'default_val')),
  160. array('parent', '/foo/bar?id=123&default_param=default_val', 'base.Code.Child.bar', array('id'=>123, 'default_param'=>'default_val')),
  161. array('child', '/foo/bar?abc=a123&efg=e456&default_param=default_val&childId=987654', 'bar', array('id'=>123, 'default_param'=>'default_val')),
  162. );
  163. }
  164. /**
  165. * @dataProvider getGenerateUrlParentFieldDescriptionTests
  166. */
  167. public function testGenerateUrlParentFieldDescription($expected, $name, array $parameters)
  168. {
  169. $childCollection = new RouteCollection('base.Code.Parent|base.Code.Child', 'admin_acme_child', '/foo/', 'BundleName:ControllerName');
  170. $childCollection->add('bar');
  171. $collection = new RouteCollection('base.Code.Parent', 'admin_acme', '/', 'BundleName:ControllerName');
  172. $collection->add('foo');
  173. $collection->addCollection($childCollection);
  174. $admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  175. $admin->expects($this->any())->method('isChild')->will($this->returnValue(false));
  176. $admin->expects($this->any())->method('getCode')->will($this->returnValue('base.Code.Parent'));
  177. // embeded admin (not nested ...)
  178. $admin->expects($this->once())->method('hasParentFieldDescription')->will($this->returnValue(true));
  179. $admin->expects($this->once())->method('hasRequest')->will($this->returnValue(true));
  180. $admin->expects($this->any())->method('getUniqid')->will($this->returnValue('foo_uniqueid'));
  181. $admin->expects($this->any())->method('getCode')->will($this->returnValue('foo_code'));
  182. $admin->expects($this->once())->method('getPersistentParameters')->will($this->returnValue(array('abc'=>'a123', 'efg'=>'e456')));
  183. $admin->expects($this->any())->method('getExtensions')->will($this->returnValue(array()));
  184. $admin->expects($this->any())->method('getRoutes')->will($this->returnValue($collection));
  185. $router = $this->getMock('Symfony\Component\Routing\RouterInterface');
  186. $router->expects($this->once())
  187. ->method('generate')
  188. ->will($this->returnCallback(function($name, array $parameters = array()) {
  189. $params = '';
  190. if (!empty($parameters)) {
  191. $params .= '?'.http_build_query($parameters);
  192. }
  193. switch ($name) {
  194. case 'admin_acme_foo':
  195. return '/foo'.$params;
  196. case 'admin_acme_child_bar':
  197. return '/foo/bar'.$params;
  198. }
  199. return null;
  200. }));
  201. $fieldDescription = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
  202. $fieldDescription->expects($this->once())->method('getOption')->will($this->returnValue(array()));
  203. $parentAdmin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  204. $parentAdmin->expects($this->any())->method('getUniqid')->will($this->returnValue('parent_foo_uniqueid'));
  205. $parentAdmin->expects($this->any())->method('getCode')->will($this->returnValue('parent_foo_code'));
  206. $parentAdmin->expects($this->any())->method('getExtensions')->will($this->returnValue(array()));
  207. $fieldDescription->expects($this->any())->method('getAdmin')->will($this->returnValue($parentAdmin));
  208. $admin->expects($this->any())->method('getParentFieldDescription')->will($this->returnValue($fieldDescription));
  209. $cache = new RoutesCache($this->cacheTempFolder, true);
  210. $generator = new DefaultRouteGenerator($router, $cache);
  211. $this->assertEquals($expected, $generator->generateUrl($admin, $name, $parameters));
  212. }
  213. public function getGenerateUrlParentFieldDescriptionTests()
  214. {
  215. return array(
  216. array('/foo?abc=a123&efg=e456&default_param=default_val&uniqid=foo_uniqueid&code=base.Code.Parent&pcode=parent_foo_code&puniqid=parent_foo_uniqueid', 'foo', array('default_param'=>'default_val')),
  217. // this second test does not make sense as we cannot have embeded admin with nested admin....
  218. array('/foo/bar?abc=a123&efg=e456&default_param=default_val&uniqid=foo_uniqueid&code=base.Code.Parent&pcode=parent_foo_code&puniqid=parent_foo_uniqueid', 'base.Code.Child.bar', array('default_param'=>'default_val')),
  219. );
  220. }
  221. /**
  222. * @dataProvider getGenerateUrlLoadCacheTests
  223. */
  224. public function testGenerateUrlLoadCache($expected, $name, array $parameters)
  225. {
  226. $childCollection = new RouteCollection('base.Code.Parent|base.Code.Child', 'admin_acme_child', '/foo', 'BundleName:ControllerName');
  227. $childCollection->add('bar');
  228. $collection = new RouteCollection('base.Code.Parent', 'admin_acme', '/', 'BundleName:ControllerName');
  229. $collection->add('foo');
  230. $collection->addCollection($childCollection);
  231. $standaloneCollection = new RouteCollection('base.Code.Child', 'admin_acme_child_standalone', '/', 'BundleName:ControllerName');
  232. $standaloneCollection->add('bar');
  233. $admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  234. $admin->expects($this->any())->method('isChild')->will($this->returnValue(true));
  235. $admin->expects($this->any())->method('getCode')->will($this->returnValue('base.Code.Child'));
  236. $admin->expects($this->any())->method('getBaseCodeRoute')->will($this->returnValue('base.Code.Parent|base.Code.Child'));
  237. $admin->expects($this->any())->method('getIdParameter')->will($this->returnValue('id'));
  238. $admin->expects($this->any())->method('hasParentFieldDescription')->will($this->returnValue(false));
  239. $admin->expects($this->any())->method('hasRequest')->will($this->returnValue(true));
  240. $admin->expects($this->any())->method('getUniqid')->will($this->returnValue('foo_uniqueid'));
  241. $admin->expects($this->any())->method('getPersistentParameters')->will($this->returnValue(array('abc'=>'a123', 'efg'=>'e456')));
  242. $admin->expects($this->any())->method('getRoutes')->will($this->returnValue($childCollection));
  243. $admin->expects($this->any())->method('getExtensions')->will($this->returnValue(array()));
  244. $parentAdmin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  245. $parentAdmin->expects($this->any())->method('getIdParameter')->will($this->returnValue('childId'));
  246. $parentAdmin->expects($this->any())->method('getRoutes')->will($this->returnValue($collection));
  247. $parentAdmin->expects($this->any())->method('getCode')->will($this->returnValue('base.Code.Parent'));
  248. $parentAdmin->expects($this->any())->method('getExtensions')->will($this->returnValue(array()));
  249. // no request attached in this test, so this will not be used
  250. $parentAdmin->expects($this->never())->method('getPersistentParameters')->will($this->returnValue(array('from'=>'parent')));
  251. $request = $this->getMock('Symfony\Component\HttpFoundation\Request');
  252. $request->attributes = $this->getMock('Symfony\Component\HttpFoundation\ParameterBag');
  253. $request->attributes->expects($this->any())->method('has')->will($this->returnValue(true));
  254. $request->attributes->expects($this->any())
  255. ->method('get')
  256. ->will($this->returnCallback(function($key) {
  257. if ($key == 'childId') {
  258. return '987654';
  259. }
  260. return null;
  261. }));
  262. $admin->expects($this->any())->method('getRequest')->will($this->returnValue($request));
  263. $admin->expects($this->any())->method('getParent')->will($this->returnValue($parentAdmin));
  264. $standaloneAdmin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  265. $standaloneAdmin->expects($this->any())->method('isChild')->will($this->returnValue(false));
  266. $standaloneAdmin->expects($this->any())->method('getCode')->will($this->returnValue('base.Code.Child'));
  267. $standaloneAdmin->expects($this->once())->method('hasParentFieldDescription')->will($this->returnValue(false));
  268. $standaloneAdmin->expects($this->once())->method('hasRequest')->will($this->returnValue(true));
  269. $standaloneAdmin->expects($this->any())->method('getUniqid')->will($this->returnValue('foo_uniqueid'));
  270. $standaloneAdmin->expects($this->once())->method('getPersistentParameters')->will($this->returnValue(array('abc'=>'a123', 'efg'=>'e456')));
  271. $standaloneAdmin->expects($this->any())->method('getRoutes')->will($this->returnValue($standaloneCollection));
  272. $standaloneAdmin->expects($this->any())->method('getExtensions')->will($this->returnValue(array()));
  273. $router = $this->getMock('\Symfony\Component\Routing\RouterInterface');
  274. $router->expects($this->exactly(2))
  275. ->method('generate')
  276. ->will($this->returnCallback(function($name, array $parameters = array()) {
  277. $params = '';
  278. if (!empty($parameters)) {
  279. $params .= '?'.http_build_query($parameters);
  280. }
  281. switch ($name) {
  282. case 'admin_acme_child_bar':
  283. return '/foo/bar'.$params;
  284. case 'admin_acme_child_standalone_bar':
  285. return '/bar'.$params;
  286. }
  287. return null;
  288. }));
  289. $cache = new RoutesCache($this->cacheTempFolder, true);
  290. $generator = new DefaultRouteGenerator($router, $cache);
  291. // Generate once to populate cache
  292. $generator->generateUrl($admin, 'bar', $parameters);
  293. $this->assertEquals($expected, $generator->generateUrl($standaloneAdmin, $name, $parameters));
  294. }
  295. public function getGenerateUrlLoadCacheTests()
  296. {
  297. return array(
  298. array('/bar?abc=a123&efg=e456&id=123&default_param=default_val', 'bar', array('id'=>123, 'default_param'=>'default_val')),
  299. );
  300. }
  301. }