DefaultRouteGeneratorTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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 Symfony\Component\Routing\Route;
  13. class DefaultRouteGeneratorTest extends \PHPUnit_Framework_TestCase
  14. {
  15. public function testGenerate()
  16. {
  17. $router = $this->getMock('\Symfony\Component\Routing\RouterInterface');
  18. $router->expects($this->once())->method('generate')->will($this->returnValue('/foo/bar'));
  19. $generator = new DefaultRouteGenerator($router);
  20. $this->assertEquals('/foo/bar', $generator->generate('foo_bar'));
  21. }
  22. /**
  23. * @dataProvider getGenerateUrlTests
  24. */
  25. public function testGenerateUrl($expected, $name, array $parameters)
  26. {
  27. $admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  28. $admin->expects($this->once())->method('isChild')->will($this->returnValue(false));
  29. $admin->expects($this->any())->method('getCode')->will($this->returnValue('base.Code.Route'));
  30. $admin->expects($this->once())->method('hasParentFieldDescription')->will($this->returnValue(false));
  31. $admin->expects($this->once())->method('hasRequest')->will($this->returnValue(true));
  32. $admin->expects($this->any())->method('getUniqid')->will($this->returnValue('foo_uniqueid'));
  33. $admin->expects($this->any())->method('getCode')->will($this->returnValue('foo_code'));
  34. $admin->expects($this->once())->method('getPersistentParameters')->will($this->returnValue(array('abc'=>'a123', 'efg'=>'e456')));
  35. $route1 = new Route('/foo');
  36. $route1->setDefault('_sonata_name', 'admin_acme_foo');
  37. $route2 = new Route('/foo/bar');
  38. $route2->setDefault('_sonata_name', 'admin_acme_bar');
  39. $admin->expects($this->once())
  40. ->method('getRoute')
  41. ->will($this->returnCallback(function($name) use ($route1, $route2) {
  42. switch ($name) {
  43. case 'base.Code.Route.foo':
  44. return $route1;
  45. case 'base.Code.Route|foo.bar':
  46. return $route2;
  47. }
  48. return false;
  49. }));
  50. $router = $this->getMock('\Symfony\Component\Routing\RouterInterface');
  51. $router->expects($this->once())
  52. ->method('generate')
  53. ->will($this->returnCallback(function($name, array $parameters = array()) {
  54. $params = '';
  55. if (!empty($parameters)) {
  56. $params .= '?'.http_build_query($parameters);
  57. }
  58. switch ($name) {
  59. case 'admin_acme_foo':
  60. return '/foo'.$params;
  61. case 'admin_acme_bar':
  62. return '/foo/bar'.$params;
  63. }
  64. return null;
  65. }));
  66. $generator = new DefaultRouteGenerator($router);
  67. $this->assertEquals($expected, $generator->generateUrl($admin, $name, $parameters));
  68. }
  69. public function getGenerateUrlTests()
  70. {
  71. return array(
  72. array('/foo?abc=a123&efg=e456&default_param=default_val', 'foo', array('default_param'=>'default_val')),
  73. array('/foo/bar?abc=a123&efg=e456&default_param=default_val', 'foo.bar', array('default_param'=>'default_val')),
  74. );
  75. }
  76. public function testGenerateUrlWithException()
  77. {
  78. $this->setExpectedException('RuntimeException', 'unable to find the route `base.Code.Route.foo`');
  79. $admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  80. $admin->expects($this->once())->method('isChild')->will($this->returnValue(false));
  81. $admin->expects($this->any())->method('getCode')->will($this->returnValue('base.Code.Route'));
  82. $admin->expects($this->once())->method('hasParentFieldDescription')->will($this->returnValue(false));
  83. $admin->expects($this->once())->method('hasRequest')->will($this->returnValue(true));
  84. $admin->expects($this->once())->method('getPersistentParameters')->will($this->returnValue(array()));
  85. $admin->expects($this->once())->method('getRoute')->will($this->returnValue(false));
  86. $router = $this->getMock('\Symfony\Component\Routing\RouterInterface');
  87. $generator = new DefaultRouteGenerator($router);
  88. $generator->generateUrl($admin, 'foo', array());
  89. }
  90. /**
  91. * @dataProvider getGenerateUrlChildTests
  92. */
  93. public function testGenerateUrlChild($expected, $name, array $parameters)
  94. {
  95. $admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  96. $admin->expects($this->once())->method('isChild')->will($this->returnValue(true));
  97. $admin->expects($this->any())->method('getCode')->will($this->returnValue('base.Code.Route'));
  98. $admin->expects($this->any())->method('getBaseCodeRoute')->will($this->returnValue('baseChild.Code.Route'));
  99. $admin->expects($this->any())->method('getIdParameter')->will($this->returnValue('id'));
  100. $admin->expects($this->once())->method('hasParentFieldDescription')->will($this->returnValue(false));
  101. $admin->expects($this->once())->method('hasRequest')->will($this->returnValue(true));
  102. $admin->expects($this->any())->method('getUniqid')->will($this->returnValue('foo_uniqueid'));
  103. $admin->expects($this->any())->method('getCode')->will($this->returnValue('foo_code'));
  104. $admin->expects($this->once())->method('getPersistentParameters')->will($this->returnValue(array('abc'=>'a123', 'efg'=>'e456')));
  105. $parentAdmin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  106. $parentAdmin->expects($this->any())->method('getIdParameter')->will($this->returnValue('childId'));
  107. $request = $this->getMock('Symfony\Component\HttpFoundation\Request');
  108. $request->expects($this->once())
  109. ->method('get')
  110. ->will($this->returnCallback(function($key) {
  111. if ($key == 'childId') {
  112. return '987654';
  113. }
  114. return null;
  115. }));
  116. $admin->expects($this->any())->method('getRequest')->will($this->returnValue($request));
  117. $admin->expects($this->any())->method('getParent')->will($this->returnValue($parentAdmin));
  118. $route1 = new Route('/foo');
  119. $route1->setDefault('_sonata_name', 'admin_acme_foo');
  120. $route2 = new Route('/foo/bar');
  121. $route2->setDefault('_sonata_name', 'admin_acme_bar');
  122. $admin->expects($this->once())
  123. ->method('getRoute')
  124. ->will($this->returnCallback(function($name) use ($route1, $route2) {
  125. switch ($name) {
  126. case 'baseChild.Code.Route.foo':
  127. return $route1;
  128. case 'baseChild.Code.Route.foo.bar':
  129. return $route2;
  130. }
  131. return false;
  132. }));
  133. $router = $this->getMock('\Symfony\Component\Routing\RouterInterface');
  134. $router->expects($this->once())
  135. ->method('generate')
  136. ->will($this->returnCallback(function($name, array $parameters = array()) {
  137. $params = '';
  138. if (!empty($parameters)) {
  139. $params .= '?'.http_build_query($parameters);
  140. }
  141. switch ($name) {
  142. case 'admin_acme_foo':
  143. return '/foo'.$params;
  144. case 'admin_acme_bar':
  145. return '/foo/bar'.$params;
  146. }
  147. return null;
  148. }));
  149. $generator = new DefaultRouteGenerator($router);
  150. $this->assertEquals($expected, $generator->generateUrl($admin, $name, $parameters));
  151. }
  152. public function getGenerateUrlChildTests()
  153. {
  154. return array(
  155. array('/foo?abc=a123&efg=e456&default_param=default_val&childId=987654', 'foo', array('id'=>123, 'default_param'=>'default_val')),
  156. array('/foo/bar?abc=a123&efg=e456&default_param=default_val&childId=987654', 'foo.bar', array('id'=>123, 'default_param'=>'default_val')),
  157. );
  158. }
  159. /**
  160. * @dataProvider getGenerateUrlParentFieldDescriptionTests
  161. */
  162. public function testGenerateUrlParentFieldDescription($expected, $name, array $parameters)
  163. {
  164. $admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  165. $admin->expects($this->once())->method('isChild')->will($this->returnValue(false));
  166. $admin->expects($this->any())->method('getCode')->will($this->returnValue('base.Code.Route'));
  167. $admin->expects($this->once())->method('hasParentFieldDescription')->will($this->returnValue(true));
  168. $admin->expects($this->once())->method('hasRequest')->will($this->returnValue(true));
  169. $admin->expects($this->any())->method('getUniqid')->will($this->returnValue('foo_uniqueid'));
  170. $admin->expects($this->any())->method('getCode')->will($this->returnValue('foo_code'));
  171. $admin->expects($this->once())->method('getPersistentParameters')->will($this->returnValue(array('abc'=>'a123', 'efg'=>'e456')));
  172. $route1 = new Route('/foo');
  173. $route1->setDefault('_sonata_name', 'admin_acme_foo');
  174. $route2 = new Route('/foo/bar');
  175. $route2->setDefault('_sonata_name', 'admin_acme_bar');
  176. $admin->expects($this->once())
  177. ->method('getRoute')
  178. ->will($this->returnCallback(function($name) use ($route1, $route2) {
  179. switch ($name) {
  180. case 'base.Code.Route.foo':
  181. return $route1;
  182. case 'base.Code.Route|foo.bar':
  183. return $route2;
  184. }
  185. return false;
  186. }));
  187. $router = $this->getMock('\Symfony\Component\Routing\RouterInterface');
  188. $router->expects($this->once())
  189. ->method('generate')
  190. ->will($this->returnCallback(function($name, array $parameters = array()) {
  191. $params = '';
  192. if (!empty($parameters)) {
  193. $params .= '?'.http_build_query($parameters);
  194. }
  195. switch ($name) {
  196. case 'admin_acme_foo':
  197. return '/foo'.$params;
  198. case 'admin_acme_bar':
  199. return '/foo/bar'.$params;
  200. }
  201. return null;
  202. }));
  203. $fieldDescription = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
  204. $fieldDescription->expects($this->once())->method('getOption')->will($this->returnValue(array()));
  205. $parentAdmin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  206. $parentAdmin->expects($this->any())->method('getUniqid')->will($this->returnValue('parent_foo_uniqueid'));
  207. $parentAdmin->expects($this->any())->method('getCode')->will($this->returnValue('parent_foo_code'));
  208. $fieldDescription->expects($this->any())->method('getAdmin')->will($this->returnValue($parentAdmin));
  209. $admin->expects($this->any())->method('getParentFieldDescription')->will($this->returnValue($fieldDescription));
  210. $generator = new DefaultRouteGenerator($router);
  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.Route&pcode=parent_foo_code&puniqid=parent_foo_uniqueid', 'foo', array('default_param'=>'default_val')),
  217. array('/foo/bar?abc=a123&efg=e456&default_param=default_val&uniqid=foo_uniqueid&code=base.Code.Route&pcode=parent_foo_code&puniqid=parent_foo_uniqueid', 'foo.bar', array('default_param'=>'default_val')),
  218. );
  219. }
  220. }