AdminVoterTest.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. /*
  3. * This file is part of the Sonata Project 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\Menu\Matcher\Voter;
  11. use Sonata\AdminBundle\Menu\Matcher\Voter\AdminVoter;
  12. use Symfony\Component\HttpFoundation\Request;
  13. class AdminVoterTest extends AbstractVoterTest
  14. {
  15. /**
  16. * {@inheritdoc}
  17. */
  18. public function provideData()
  19. {
  20. return array(
  21. 'no data' => array(null, null, null, null),
  22. 'no route and granted' => array($this->getAdmin('_sonata_admin'), '_sonata_admin', null, null),
  23. 'no granted' => array($this->getAdmin('_sonata_admin', true, false), '_sonata_admin', null, null),
  24. 'no code' => array($this->getAdmin('_sonata_admin_code', true, true), '_sonata_admin', null, null),
  25. 'no code request' => array($this->getAdmin('_sonata_admin', true, true), '_sonata_admin_unexpected', null, null),
  26. 'no route' => array($this->getAdmin('_sonata_admin', false, true), '_sonata_admin', null, null),
  27. 'has admin' => array($this->getAdmin('_sonata_admin', true, true), '_sonata_admin', null, true),
  28. 'direct link' => array('admin_post', null, 'admin_post', true),
  29. 'no direct link' => array('admin_post', null, 'admin_blog', null),
  30. );
  31. }
  32. /**
  33. * {@inheritdoc}
  34. */
  35. protected function createVoter($dataVoter, $route)
  36. {
  37. $voter = new AdminVoter();
  38. $request = new Request();
  39. $request->request->set('_sonata_admin', $dataVoter);
  40. $request->request->set('_route', $route);
  41. $voter->setRequest($request);
  42. return $voter;
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. protected function createItem($data)
  48. {
  49. $item = $this->getMock('Knp\Menu\ItemInterface');
  50. $item->expects($this->any())
  51. ->method('getExtra')
  52. ->with($this->logicalOr(
  53. $this->equalTo('admin'),
  54. $this->equalTo('route')
  55. ))
  56. ->will($this->returnValue($data))
  57. ;
  58. return $item;
  59. }
  60. /**
  61. * {@inheritdoc}
  62. */
  63. private function getAdmin($code, $list = false, $granted = false)
  64. {
  65. $admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  66. $admin
  67. ->expects($this->any())
  68. ->method('hasRoute')
  69. ->with('list')
  70. ->will($this->returnValue($list))
  71. ;
  72. $admin
  73. ->expects($this->any())
  74. ->method('isGranted')
  75. ->with('LIST')
  76. ->will($this->returnValue($granted))
  77. ;
  78. $admin
  79. ->expects($this->any())
  80. ->method('getCode')
  81. ->will($this->returnValue($code))
  82. ;
  83. return $admin;
  84. }
  85. }