AdminEventExtensionTest.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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\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. *
  35. * @return callable
  36. */
  37. public function getConfigureEventClosure($type)
  38. {
  39. return function ($event) use ($type) {
  40. if (!$event instanceof ConfigureEvent) {
  41. return false;
  42. }
  43. if ($event->getType() != $type) {
  44. return false;
  45. }
  46. return true;
  47. };
  48. }
  49. /**
  50. * @param $type
  51. *
  52. * @return callable
  53. */
  54. public function getConfigurePersistenceClosure($type)
  55. {
  56. return function ($event) use ($type) {
  57. if (!$event instanceof PersistenceEvent) {
  58. return false;
  59. }
  60. if ($event->getType() != $type) {
  61. return false;
  62. }
  63. return true;
  64. };
  65. }
  66. public function testConfigureFormFields()
  67. {
  68. $this
  69. ->getExtension(array(
  70. $this->equalTo('sonata.admin.event.configure.form'),
  71. $this->callback($this->getConfigureEventClosure(ConfigureEvent::TYPE_FORM)),
  72. ))
  73. ->configureFormFields($this->getMapper('Sonata\AdminBundle\Form\FormMapper'));
  74. }
  75. public function testConfigureListFields()
  76. {
  77. $this
  78. ->getExtension(array(
  79. $this->equalTo('sonata.admin.event.configure.list'),
  80. $this->callback($this->getConfigureEventClosure(ConfigureEvent::TYPE_LIST)),
  81. ))
  82. ->configureListFields($this->getMapper('Sonata\AdminBundle\Datagrid\ListMapper'));
  83. }
  84. public function testConfigureDatagridFields()
  85. {
  86. $this
  87. ->getExtension(array(
  88. $this->equalTo('sonata.admin.event.configure.datagrid'),
  89. $this->callback($this->getConfigureEventClosure(ConfigureEvent::TYPE_DATAGRID)),
  90. ))
  91. ->configureDatagridFilters($this->getMapper('Sonata\AdminBundle\Datagrid\DatagridMapper'));
  92. }
  93. public function testConfigureShowFields()
  94. {
  95. $this
  96. ->getExtension(array(
  97. $this->equalTo('sonata.admin.event.configure.show'),
  98. $this->callback($this->getConfigureEventClosure(ConfigureEvent::TYPE_SHOW)),
  99. ))
  100. ->configureShowFields($this->getMapper('Sonata\AdminBundle\Show\ShowMapper'));
  101. }
  102. public function testPreUpdate()
  103. {
  104. $this->getExtension(array(
  105. $this->equalTo('sonata.admin.event.persistence.pre_update'),
  106. $this->callback($this->getConfigurePersistenceClosure(PersistenceEvent::TYPE_PRE_UPDATE)),
  107. ))->preUpdate($this->getMock('Sonata\AdminBundle\Admin\AdminInterface'), new \stdClass());
  108. }
  109. public function testConfigureQuery()
  110. {
  111. $this->getExtension(array(
  112. $this->equalTo('sonata.admin.event.configure.query'),
  113. ))->configureQuery($this->getMock('Sonata\AdminBundle\Admin\AdminInterface'), $this->getMock('Sonata\AdminBundle\Datagrid\ProxyQueryInterface'));
  114. }
  115. public function testPostUpdate()
  116. {
  117. $this->getExtension(array(
  118. $this->equalTo('sonata.admin.event.persistence.post_update'),
  119. $this->callback($this->getConfigurePersistenceClosure(PersistenceEvent::TYPE_POST_UPDATE)),
  120. ))->postUpdate($this->getMock('Sonata\AdminBundle\Admin\AdminInterface'), new \stdClass());
  121. }
  122. public function testPrePersist()
  123. {
  124. $this->getExtension(array(
  125. $this->equalTo('sonata.admin.event.persistence.pre_persist'),
  126. $this->callback($this->getConfigurePersistenceClosure(PersistenceEvent::TYPE_PRE_PERSIST)),
  127. ))->prePersist($this->getMock('Sonata\AdminBundle\Admin\AdminInterface'), new \stdClass());
  128. }
  129. public function testPostPersist()
  130. {
  131. $this->getExtension(array(
  132. $this->equalTo('sonata.admin.event.persistence.post_persist'),
  133. $this->callback($this->getConfigurePersistenceClosure(PersistenceEvent::TYPE_POST_PERSIST)),
  134. ))->postPersist($this->getMock('Sonata\AdminBundle\Admin\AdminInterface'), new \stdClass());
  135. }
  136. public function testPreRemove()
  137. {
  138. $this->getExtension(array(
  139. $this->equalTo('sonata.admin.event.persistence.pre_remove'),
  140. $this->callback($this->getConfigurePersistenceClosure(PersistenceEvent::TYPE_PRE_REMOVE)),
  141. ))->preRemove($this->getMock('Sonata\AdminBundle\Admin\AdminInterface'), new \stdClass());
  142. }
  143. public function testPostRemove()
  144. {
  145. $this->getExtension(array(
  146. $this->equalTo('sonata.admin.event.persistence.post_remove'),
  147. $this->callback($this->getConfigurePersistenceClosure(PersistenceEvent::TYPE_POST_REMOVE)),
  148. ))->postRemove($this->getMock('Sonata\AdminBundle\Admin\AdminInterface'), new \stdClass());
  149. }
  150. }