LockExtensionTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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\EventDispatcher\EventDispatcherInterface;
  15. use Symfony\Component\Form\FormBuilder;
  16. use Symfony\Component\Form\FormEvent;
  17. use Symfony\Component\Form\FormEvents;
  18. use Symfony\Component\HttpFoundation\Request;
  19. class LockExtensionTest extends \PHPUnit_Framework_TestCase
  20. {
  21. /**
  22. * @var LockExtension
  23. */
  24. private $lockExtension;
  25. /**
  26. * @var EventDispatcherInterface
  27. */
  28. private $eventDispatcher;
  29. private $admin;
  30. private $modelManager;
  31. private $form;
  32. private $object;
  33. private $request;
  34. public function setUp()
  35. {
  36. $contractor = $this->getMock('Sonata\AdminBundle\Builder\FormContractorInterface');
  37. $formFactory = $this->getMock('Symfony\Component\Form\FormFactoryInterface');
  38. $this->eventDispatcher = new EventDispatcher();
  39. $formBuilder = new FormBuilder('form', null, $this->eventDispatcher, $formFactory);
  40. $this->modelManager = $this->getMock('Sonata\AdminBundle\Model\LockInterface');
  41. $this->admin = $this->getMockBuilder('Sonata\AdminBundle\Admin\Admin')
  42. ->disableOriginalConstructor()
  43. ->getMock();
  44. $this->admin->expects($this->any())
  45. ->method('getModelManager')
  46. ->will($this->returnValue($this->modelManager));
  47. $this->request = new Request();
  48. $this->admin->expects($this->any())
  49. ->method('getRequest')
  50. ->will($this->returnValue($this->request));
  51. $this->admin->expects($this->any())
  52. ->method('hasRequest')
  53. ->will($this->returnValue(true));
  54. $formMapper = new FormMapper(
  55. $contractor,
  56. $formBuilder,
  57. $this->admin
  58. );
  59. $this->object = new \StdClass();
  60. $this->form = $this->getMock('Symfony\Component\Form\FormInterface');
  61. $this->form->expects($this->any())
  62. ->method('getData')
  63. ->will($this->returnValue($this->object));
  64. $this->lockExtension = new LockExtension();
  65. $this->lockExtension->configureFormFields($formMapper);
  66. }
  67. public function testConfigureFormFields()
  68. {
  69. $this->modelManager->expects($this->any())
  70. ->method('getLockVersion')
  71. ->will($this->returnValue(1));
  72. $this->form->expects($this->once())
  73. ->method('add')
  74. ->with(
  75. $this->equalTo('_lock_version'),
  76. $this->equalTo('hidden'),
  77. $this->equalTo(array(
  78. 'mapped' => false,
  79. 'data' => 1,
  80. ))
  81. );
  82. $event = new FormEvent($this->form, array());
  83. $this->eventDispatcher->dispatch(FormEvents::PRE_SET_DATA, $event);
  84. }
  85. public function testPreUpdateIfObjectIsNotVersioned()
  86. {
  87. $this->modelManager->expects($this->never())
  88. ->method('lock');
  89. $this->lockExtension->preUpdate($this->admin, $this->object);
  90. }
  91. public function testPreUpdateIfObjectIsVersioned()
  92. {
  93. $uniqid = 'admin123';
  94. $this->modelManager->expects($this->once())
  95. ->method('lock')
  96. ->with($this->object, 1);
  97. $this->request->request->set($uniqid, array('_lock_version' => 1));
  98. $this->admin->expects($this->any())
  99. ->method('getUniqId')
  100. ->will($this->returnValue($uniqid));
  101. $this->lockExtension->preUpdate($this->admin, $this->object);
  102. }
  103. }