AdminVoterTest.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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),
  22. 'no route and granted' => array($this->getAdmin('_sonata_admin'), '_sonata_admin', null),
  23. 'no granted' => array($this->getAdmin('_sonata_admin', true, false), '_sonata_admin', null),
  24. 'no code' => array($this->getAdmin('_sonata_admin_code', true, true), '_sonata_admin', null),
  25. 'no code request' => array($this->getAdmin('_sonata_admin', true, true), '_sonata_admin_unexpected', null),
  26. 'no route' => array($this->getAdmin('_sonata_admin', false, true), '_sonata_admin', null),
  27. 'has admin' => array($this->getAdmin('_sonata_admin', true, true), '_sonata_admin', true),
  28. );
  29. }
  30. /**
  31. * {@inheritdoc}
  32. */
  33. protected function createVoter($dataVoter)
  34. {
  35. $voter = new AdminVoter();
  36. $request = new Request();
  37. $request->request->set('_sonata_admin', $dataVoter);
  38. $voter->setRequest($request);
  39. return $voter;
  40. }
  41. /**
  42. * {@inheritdoc}
  43. */
  44. protected function createItem($data)
  45. {
  46. $item = $this->getMock('Knp\Menu\ItemInterface');
  47. $item->expects($this->any())
  48. ->method('getExtra')
  49. ->with('admin')
  50. ->will($this->returnValue($data))
  51. ;
  52. return $item;
  53. }
  54. /**
  55. * {@inheritdoc}
  56. */
  57. private function getAdmin($code, $list = false, $granted = false)
  58. {
  59. $admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  60. $admin
  61. ->expects($this->any())
  62. ->method('hasRoute')
  63. ->with('list')
  64. ->will($this->returnValue($list))
  65. ;
  66. $admin
  67. ->expects($this->any())
  68. ->method('isGranted')
  69. ->with('LIST')
  70. ->will($this->returnValue($granted))
  71. ;
  72. $admin
  73. ->expects($this->any())
  74. ->method('getCode')
  75. ->will($this->returnValue($code))
  76. ;
  77. return $admin;
  78. }
  79. }