LockExtensionTest.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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\Admin\Extension;
  11. use Sonata\AdminBundle\Admin\Extension\LockExtension;
  12. use Sonata\AdminBundle\Form\FormMapper;
  13. use Symfony\Component\EventDispatcher\EventDispatcher;
  14. use Symfony\Component\Form\FormBuilder;
  15. use Symfony\Component\Form\FormEvent;
  16. use Symfony\Component\Form\FormEvents;
  17. use Symfony\Component\HttpFoundation\Request;
  18. class LockExtensionTest extends \PHPUnit_Framework_TestCase
  19. {
  20. /**
  21. * @var LockExtension
  22. */
  23. private $lockExtension;
  24. /**
  25. * @var EventDispatcherInterface
  26. */
  27. private $eventDispatcher;
  28. /**
  29. * @var AdminInterface
  30. */
  31. private $admin;
  32. /**
  33. * @var LockInterface
  34. */
  35. private $modelManager;
  36. /**
  37. * @var stdClass
  38. */
  39. private $object;
  40. /**
  41. * @var Request
  42. */
  43. private $request;
  44. protected function setUp()
  45. {
  46. $this->modelManager = $this->prophesize('Sonata\AdminBundle\Model\LockInterface');
  47. $this->admin = $this->prophesize('Sonata\AdminBundle\Admin\AbstractAdmin');
  48. $this->eventDispatcher = new EventDispatcher();
  49. $this->request = new Request();
  50. $this->object = new \stdClass();
  51. $this->lockExtension = new LockExtension();
  52. }
  53. public function testConfigureFormFields()
  54. {
  55. $formMapper = $this->configureFormMapper();
  56. $form = $this->configureForm();
  57. $this->configureAdmin(null, null, $this->modelManager->reveal());
  58. $event = new FormEvent($form->reveal(), array());
  59. $this->modelManager->getLockVersion(array())->willReturn(1);
  60. $form->add('_lock_version', 'hidden', array('mapped' => false, 'data' => 1))->shouldBeCalled();
  61. $this->lockExtension->configureFormFields($formMapper);
  62. $this->eventDispatcher->dispatch(FormEvents::PRE_SET_DATA, $event);
  63. }
  64. public function testConfigureFormFieldsWhenModelManagerIsNotImplementingLockerInterface()
  65. {
  66. $modelManager = $this->prophesize('Sonata\AdminBundle\Model\ModelManagerInterface');
  67. $formMapper = $this->configureFormMapper();
  68. $form = $this->configureForm();
  69. $this->configureAdmin(null, null, $modelManager->reveal());
  70. $event = new FormEvent($form->reveal(), array());
  71. $form->add()->shouldNotBeCalled();
  72. $this->lockExtension->configureFormFields($formMapper);
  73. $this->eventDispatcher->dispatch(FormEvents::PRE_SET_DATA, $event);
  74. }
  75. public function testConfigureFormFieldsWhenFormEventHasNoData()
  76. {
  77. $formMapper = $this->configureFormMapper();
  78. $form = $this->configureForm();
  79. $event = new FormEvent($form->reveal(), null);
  80. $form->add()->shouldNotBeCalled();
  81. $this->lockExtension->configureFormFields($formMapper);
  82. $this->eventDispatcher->dispatch(FormEvents::PRE_SET_DATA, $event);
  83. }
  84. public function testConfigureFormFieldsWhenFormHasParent()
  85. {
  86. $formMapper = $this->configureFormMapper();
  87. $form = $this->configureForm();
  88. $event = new FormEvent($form->reveal(), array());
  89. $form->getParent()->willReturn('parent');
  90. $form->add()->shouldNotBeCalled();
  91. $this->lockExtension->configureFormFields($formMapper);
  92. $this->eventDispatcher->dispatch(FormEvents::PRE_SET_DATA, $event);
  93. }
  94. public function testConfigureFormFieldsWhenModelManagerHasNoLockedVersion()
  95. {
  96. $formMapper = $this->configureFormMapper();
  97. $form = $this->configureForm();
  98. $this->configureAdmin(null, null, $this->modelManager->reveal());
  99. $event = new FormEvent($form->reveal(), array());
  100. $this->modelManager->getLockVersion(array())->willReturn(null);
  101. $form->add()->shouldNotBeCalled();
  102. $this->lockExtension->configureFormFields($formMapper);
  103. $this->eventDispatcher->dispatch(FormEvents::PRE_SET_DATA, $event);
  104. }
  105. public function testPreUpdateIfAdminHasNoRequest()
  106. {
  107. $this->modelManager->lock()->shouldNotBeCalled();
  108. $this->lockExtension->preUpdate($this->admin->reveal(), $this->object);
  109. }
  110. public function testPreUpdateIfObjectIsNotVersioned()
  111. {
  112. $this->configureAdmin();
  113. $this->modelManager->lock()->shouldNotBeCalled();
  114. $this->lockExtension->preUpdate($this->admin->reveal(), $this->object);
  115. }
  116. public function testPreUpdateIfRequestDoesNotHaveLockVersion()
  117. {
  118. $uniqid = 'admin123';
  119. $this->configureAdmin($uniqid, $this->request);
  120. $this->modelManager->lock()->shouldNotBeCalled();
  121. $this->request->request->set($uniqid, array('something'));
  122. $this->lockExtension->preUpdate($this->admin->reveal(), $this->object);
  123. }
  124. public function testPreUpdateIfModelManagerIsNotImplementingLockerInterface()
  125. {
  126. $modelManager = $this->prophesize('Sonata\AdminBundle\Model\ModelManagerInterface');
  127. $uniqid = 'admin123';
  128. $this->configureAdmin($uniqid, $this->request, $modelManager->reveal());
  129. $this->request->request->set($uniqid, array('_lock_version' => 1));
  130. $this->lockExtension->preUpdate($this->admin->reveal(), $this->object);
  131. }
  132. public function testPreUpdateIfObjectIsVersioned()
  133. {
  134. $uniqid = 'admin123';
  135. $this->configureAdmin($uniqid, $this->request, $this->modelManager->reveal());
  136. $this->modelManager->lock($this->object, 1)->shouldBeCalled();
  137. $this->request->request->set($uniqid, array('_lock_version' => 1));
  138. $this->lockExtension->preUpdate($this->admin->reveal(), $this->object);
  139. }
  140. private function configureForm()
  141. {
  142. $form = $this->prophesize('Symfony\Component\Form\FormInterface');
  143. $form->getData()->willReturn($this->object);
  144. $form->getParent()->willReturn(null);
  145. return $form;
  146. }
  147. private function configureFormMapper()
  148. {
  149. $contractor = $this->prophesize('Sonata\AdminBundle\Builder\FormContractorInterface');
  150. $formFactory = $this->prophesize('Symfony\Component\Form\FormFactoryInterface');
  151. $formBuilder = new FormBuilder('form', null, $this->eventDispatcher, $formFactory->reveal());
  152. return new FormMapper($contractor->reveal(), $formBuilder, $this->admin->reveal());
  153. }
  154. private function configureAdmin($uniqid = null, $request = null, $modelManager = null)
  155. {
  156. $this->admin->getUniqid()->willReturn($uniqid);
  157. $this->admin->getRequest()->willReturn($request);
  158. $this->admin->hasRequest()->willReturn($request !== null);
  159. $this->admin->getModelManager()->willReturn($modelManager);
  160. }
  161. }