QueryStringBuilderTest.php 4.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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\QueryStringBuilder;
  12. use Sonata\AdminBundle\Route\RouteCollection;
  13. class QueryStringBuilderTest extends \PHPUnit_Framework_TestCase
  14. {
  15. /**
  16. * @dataProvider getBuildTests
  17. */
  18. public function testBuild(array $expectedRoutes, $hasReader, $aclEnabled, $getParent)
  19. {
  20. $audit = $this->getMock('Sonata\AdminBundle\Model\AuditManagerInterface');
  21. $audit->expects($this->once())->method('hasReader')->will($this->returnValue($hasReader));
  22. $admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  23. $admin->expects($this->once())->method('getParent')->will($this->returnValue($getParent));
  24. $admin->expects($this->any())->method('getChildren')->will($this->returnValue(array()));
  25. $admin->expects($this->once())->method('isAclEnabled')->will($this->returnValue($aclEnabled));
  26. $routeCollection = new RouteCollection('base.Code.Route', 'baseRouteName', 'baseRoutePattern', 'baseControllerName');
  27. $pathBuilder = new QueryStringBuilder($audit);
  28. $pathBuilder->build($admin, $routeCollection);
  29. $this->assertCount(count($expectedRoutes), $routeCollection->getElements());
  30. foreach ($expectedRoutes as $expectedRoute) {
  31. $this->assertTrue($routeCollection->has($expectedRoute), sprintf('Expected route: "%s" doesn`t exist.', $expectedRoute));
  32. }
  33. }
  34. public function getBuildTests()
  35. {
  36. return array(
  37. array(array('list', 'create', 'batch', 'edit', 'delete', 'show', 'export', 'history', 'history_view_revision', 'acl'), true, true, null),
  38. array(array('list', 'create', 'batch', 'edit', 'delete', 'show', 'export', 'acl'), false, true, null),
  39. array(array('list', 'create', 'batch', 'edit', 'delete', 'show', 'export', 'history', 'history_view_revision'), true, false, null),
  40. array(array('list', 'create', 'batch', 'edit', 'delete', 'show', 'export', 'history', 'history_view_revision', 'acl'), true, true, $this->getMock('Sonata\AdminBundle\Admin\AdminInterface')),
  41. );
  42. }
  43. public function testBuildWithChildren()
  44. {
  45. $audit = $this->getMock('Sonata\AdminBundle\Model\AuditManagerInterface');
  46. $audit->expects($this->once())->method('hasReader')->will($this->returnValue(true));
  47. $childRouteCollection1 = new RouteCollection('child1.Code.Route', 'child1RouteName', 'child1RoutePattern', 'child1ControllerName');
  48. $childRouteCollection1->add('foo');
  49. $childRouteCollection1->add('bar');
  50. $childRouteCollection2 = new RouteCollection('child2.Code.Route', 'child2RouteName', 'child2RoutePattern', 'child2ControllerName');
  51. $childRouteCollection2->add('baz');
  52. $child1 = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  53. $child1->expects($this->once())->method('getRoutes')->will($this->returnValue($childRouteCollection1));
  54. $child2 = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  55. $child2->expects($this->once())->method('getRoutes')->will($this->returnValue($childRouteCollection2));
  56. $admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  57. $admin->expects($this->once())->method('getParent')->will($this->returnValue(null));
  58. $admin->expects($this->once())->method('getChildren')->will($this->returnValue(array($child1, $child2)));
  59. $admin->expects($this->once())->method('isAclEnabled')->will($this->returnValue(true));
  60. $routeCollection = new RouteCollection('base.Code.Route', 'baseRouteName', 'baseRoutePattern', 'baseControllerName');
  61. $pathBuilder = new QueryStringBuilder($audit);
  62. $pathBuilder->build($admin, $routeCollection);
  63. $expectedRoutes = array('list', 'create', 'batch', 'edit', 'delete', 'show', 'export', 'history', 'history_view_revision', 'acl', 'child1.Code.Route.foo', 'child1.Code.Route.bar', 'child2.Code.Route.baz');
  64. $this->assertCount(count($expectedRoutes), $routeCollection->getElements());
  65. foreach ($expectedRoutes as $expectedRoute) {
  66. $this->assertTrue($routeCollection->has($expectedRoute), sprintf('Expected route: "%s" doesn`t exist.', $expectedRoute));
  67. }
  68. }
  69. }