AdminEventExtensionTest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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\Event;
  11. use Sonata\AdminBundle\Event\AdminEventExtension;
  12. use Sonata\AdminBundle\Event\ConfigureEvent;
  13. use Sonata\AdminBundle\Event\PersistenceEvent;
  14. class AdminEventExtensionTest extends \PHPUnit_Framework_TestCase
  15. {
  16. /**
  17. * @return AdminEventExtension
  18. */
  19. public function getExtension($args)
  20. {
  21. $eventDispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
  22. $stub = $eventDispatcher->expects($this->once())->method('dispatch');
  23. call_user_func_array(array($stub, 'with'), $args);
  24. return new AdminEventExtension($eventDispatcher);
  25. }
  26. public function getMapper($class)
  27. {
  28. $mapper = $this->getMockBuilder($class)->disableOriginalConstructor()->getMock();
  29. $mapper->expects($this->once())->method('getAdmin')->will($this->returnValue($this->getMock('Sonata\AdminBundle\Admin\AdminInterface')));
  30. return $mapper;
  31. }
  32. /**
  33. * @param $type
  34. * @return callable
  35. */
  36. public function getConfigureEventClosure($type)
  37. {
  38. return function ($event) use ($type) {
  39. if (!$event instanceof ConfigureEvent) {
  40. return false;
  41. }
  42. if ($event->getType() != $type) {
  43. return false;
  44. }
  45. return true;
  46. };
  47. }
  48. /**
  49. * @param $type
  50. * @return callable
  51. */
  52. public function getConfigurePersistenceClosure($type)
  53. {
  54. return function ($event) use ($type) {
  55. if (!$event instanceof PersistenceEvent) {
  56. return false;
  57. }
  58. if ($event->getType() != $type) {
  59. return false;
  60. }
  61. return true;
  62. };
  63. }
  64. public function testConfigureFormFields()
  65. {
  66. $this
  67. ->getExtension(array(
  68. $this->equalTo('sonata.admin.event.configure.form'),
  69. $this->callback($this->getConfigureEventClosure(ConfigureEvent::TYPE_FORM))
  70. ))
  71. ->configureFormFields($this->getMapper('Sonata\AdminBundle\Form\FormMapper'));
  72. }
  73. public function testConfigureListFields()
  74. {
  75. $this
  76. ->getExtension(array(
  77. $this->equalTo('sonata.admin.event.configure.list'),
  78. $this->callback($this->getConfigureEventClosure(ConfigureEvent::TYPE_LIST))
  79. ))
  80. ->configureListFields($this->getMapper('Sonata\AdminBundle\Datagrid\ListMapper'));
  81. }
  82. public function testConfigureDatagridFields()
  83. {
  84. $this
  85. ->getExtension(array(
  86. $this->equalTo('sonata.admin.event.configure.datagrid'),
  87. $this->callback($this->getConfigureEventClosure(ConfigureEvent::TYPE_DATAGRID))
  88. ))
  89. ->configureDatagridFilters($this->getMapper('Sonata\AdminBundle\Datagrid\DatagridMapper'));
  90. }
  91. public function testConfigureShowFields()
  92. {
  93. $this
  94. ->getExtension(array(
  95. $this->equalTo('sonata.admin.event.configure.show'),
  96. $this->callback($this->getConfigureEventClosure(ConfigureEvent::TYPE_SHOW))
  97. ))
  98. ->configureShowFields($this->getMapper('Sonata\AdminBundle\Show\ShowMapper'));
  99. }
  100. public function testPreUpdate()
  101. {
  102. $this->getExtension(array(
  103. $this->equalTo('sonata.admin.event.persistence.pre_update'),
  104. $this->callback($this->getConfigurePersistenceClosure(PersistenceEvent::TYPE_PRE_UPDATE))
  105. ))->preUpdate($this->getMock('Sonata\AdminBundle\Admin\AdminInterface'), new \stdClass);
  106. }
  107. public function testPostUpdate()
  108. {
  109. $this->getExtension(array(
  110. $this->equalTo('sonata.admin.event.persistence.post_update'),
  111. $this->callback($this->getConfigurePersistenceClosure(PersistenceEvent::TYPE_POST_UPDATE))
  112. ))->postUpdate($this->getMock('Sonata\AdminBundle\Admin\AdminInterface'), new \stdClass);
  113. }
  114. public function testPrePersist()
  115. {
  116. $this->getExtension(array(
  117. $this->equalTo('sonata.admin.event.persistence.pre_persist'),
  118. $this->callback($this->getConfigurePersistenceClosure(PersistenceEvent::TYPE_PRE_PERSIST))
  119. ))->prePersist($this->getMock('Sonata\AdminBundle\Admin\AdminInterface'), new \stdClass);
  120. }
  121. public function testPostPersist()
  122. {
  123. $this->getExtension(array(
  124. $this->equalTo('sonata.admin.event.persistence.post_persist'),
  125. $this->callback($this->getConfigurePersistenceClosure(PersistenceEvent::TYPE_POST_PERSIST))
  126. ))->postPersist($this->getMock('Sonata\AdminBundle\Admin\AdminInterface'), new \stdClass);
  127. }
  128. public function testPreRemove()
  129. {
  130. $this->getExtension(array(
  131. $this->equalTo('sonata.admin.event.persistence.pre_remove'),
  132. $this->callback($this->getConfigurePersistenceClosure(PersistenceEvent::TYPE_PRE_REMOVE))
  133. ))->preRemove($this->getMock('Sonata\AdminBundle\Admin\AdminInterface'), new \stdClass);
  134. }
  135. public function testPostRemove()
  136. {
  137. $this->getExtension(array(
  138. $this->equalTo('sonata.admin.event.persistence.post_remove'),
  139. $this->callback($this->getConfigurePersistenceClosure(PersistenceEvent::TYPE_POST_REMOVE))
  140. ))->postRemove($this->getMock('Sonata\AdminBundle\Admin\AdminInterface'), new \stdClass);
  141. }
  142. }