AdminEventExtensionTest.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 testConfigureQuery()
  108. {
  109. $this->getExtension(array(
  110. $this->equalTo('sonata.admin.event.configure.query'),
  111. ))->configureQuery($this->getMock('Sonata\AdminBundle\Admin\AdminInterface'), $this->getMock('Sonata\AdminBundle\Datagrid\ProxyQueryInterface'));
  112. }
  113. public function testPostUpdate()
  114. {
  115. $this->getExtension(array(
  116. $this->equalTo('sonata.admin.event.persistence.post_update'),
  117. $this->callback($this->getConfigurePersistenceClosure(PersistenceEvent::TYPE_POST_UPDATE))
  118. ))->postUpdate($this->getMock('Sonata\AdminBundle\Admin\AdminInterface'), new \stdClass);
  119. }
  120. public function testPrePersist()
  121. {
  122. $this->getExtension(array(
  123. $this->equalTo('sonata.admin.event.persistence.pre_persist'),
  124. $this->callback($this->getConfigurePersistenceClosure(PersistenceEvent::TYPE_PRE_PERSIST))
  125. ))->prePersist($this->getMock('Sonata\AdminBundle\Admin\AdminInterface'), new \stdClass);
  126. }
  127. public function testPostPersist()
  128. {
  129. $this->getExtension(array(
  130. $this->equalTo('sonata.admin.event.persistence.post_persist'),
  131. $this->callback($this->getConfigurePersistenceClosure(PersistenceEvent::TYPE_POST_PERSIST))
  132. ))->postPersist($this->getMock('Sonata\AdminBundle\Admin\AdminInterface'), new \stdClass);
  133. }
  134. public function testPreRemove()
  135. {
  136. $this->getExtension(array(
  137. $this->equalTo('sonata.admin.event.persistence.pre_remove'),
  138. $this->callback($this->getConfigurePersistenceClosure(PersistenceEvent::TYPE_PRE_REMOVE))
  139. ))->preRemove($this->getMock('Sonata\AdminBundle\Admin\AdminInterface'), new \stdClass);
  140. }
  141. public function testPostRemove()
  142. {
  143. $this->getExtension(array(
  144. $this->equalTo('sonata.admin.event.persistence.post_remove'),
  145. $this->callback($this->getConfigurePersistenceClosure(PersistenceEvent::TYPE_POST_REMOVE))
  146. ))->postRemove($this->getMock('Sonata\AdminBundle\Admin\AdminInterface'), new \stdClass);
  147. }
  148. }