Completed
Push — 3.x ( b75183...b1c847 )
by Oskar
04:45
created

tests/Admin/Extension/LockExtensionTest.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\AdminBundle\Tests\Admin\Extension;
15
16
use PHPUnit\Framework\TestCase;
17
use Prophecy\Prophecy\ObjectProphecy;
18
use Sonata\AdminBundle\Admin\AbstractAdmin;
19
use Sonata\AdminBundle\Admin\Extension\LockExtension;
20
use Sonata\AdminBundle\Builder\FormContractorInterface;
21
use Sonata\AdminBundle\Form\FormMapper;
22
use Sonata\AdminBundle\Model\LockInterface;
23
use Sonata\AdminBundle\Model\ModelManagerInterface;
24
use Symfony\Component\EventDispatcher\EventDispatcher;
25
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
26
use Symfony\Component\Form\FormBuilder;
27
use Symfony\Component\Form\FormEvent;
28
use Symfony\Component\Form\FormEvents;
29
use Symfony\Component\Form\FormFactoryInterface;
30
use Symfony\Component\Form\FormInterface;
31
use Symfony\Component\HttpFoundation\Request;
32
33
class LockExtensionTest extends TestCase
34
{
35
    /**
36
     * @var LockExtension
37
     */
38
    private $lockExtension;
39
40
    /**
41
     * @var EventDispatcherInterface
42
     */
43
    private $eventDispatcher;
44
45
    /**
46
     * @var AdminInterface
47
     */
48
    private $admin;
49
50
    /**
51
     * @var LockInterface
52
     */
53
    private $modelManager;
54
55
    /**
56
     * @var stdClass
57
     */
58
    private $object;
59
60
    /**
61
     * @var Request
62
     */
63
    private $request;
64
65
    protected function setUp(): void
66
    {
67
        $this->modelManager = $this->prophesize(LockInterface::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->prophesize(\Sonat...l\LockInterface::class) of type object<Prophecy\Prophecy\ObjectProphecy> is incompatible with the declared type object<Sonata\AdminBundle\Model\LockInterface> of property $modelManager.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
68
        $this->admin = $this->prophesize(AbstractAdmin::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->prophesize(\Sonat...n\AbstractAdmin::class) of type object<Prophecy\Prophecy\ObjectProphecy> is incompatible with the declared type object<Sonata\AdminBundl...tension\AdminInterface> of property $admin.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
69
70
        $this->eventDispatcher = new EventDispatcher();
71
        $this->request = new Request();
72
        $this->object = new \stdClass();
73
        $this->lockExtension = new LockExtension();
74
    }
75
76
    public function testConfigureFormFields(): void
77
    {
78
        $formMapper = $this->configureFormMapper();
79
        $form = $this->configureForm();
80
        $this->configureAdmin(null, null, $this->modelManager->reveal());
81
        $event = new FormEvent($form->reveal(), []);
82
83
        $this->modelManager->getLockVersion([])->willReturn(1);
84
85
        $form->add(
86
            '_lock_version',
87
            HiddenType::class,
88
            ['mapped' => false, 'data' => 1]
89
        )->shouldBeCalled();
90
91
        $this->lockExtension->configureFormFields($formMapper);
92
        $this->eventDispatcher->dispatch(FormEvents::PRE_SET_DATA, $event);
93
    }
94
95
    public function testConfigureFormFieldsWhenModelManagerIsNotImplementingLockerInterface(): void
96
    {
97
        $modelManager = $this->prophesize(ModelManagerInterface::class);
98
        $formMapper = $this->configureFormMapper();
99
        $form = $this->configureForm();
100
        $this->configureAdmin(null, null, $modelManager->reveal());
101
        $event = new FormEvent($form->reveal(), []);
102
103
        $form->add()->shouldNotBeCalled();
104
105
        $this->lockExtension->configureFormFields($formMapper);
106
        $this->eventDispatcher->dispatch(FormEvents::PRE_SET_DATA, $event);
107
    }
108
109
    public function testConfigureFormFieldsWhenFormEventHasNoData(): void
110
    {
111
        $formMapper = $this->configureFormMapper();
112
        $form = $this->configureForm();
113
        $event = new FormEvent($form->reveal(), null);
114
115
        $form->add()->shouldNotBeCalled();
116
117
        $this->lockExtension->configureFormFields($formMapper);
118
        $this->eventDispatcher->dispatch(FormEvents::PRE_SET_DATA, $event);
119
    }
120
121
    public function testConfigureFormFieldsWhenFormHasParent(): void
122
    {
123
        $formMapper = $this->configureFormMapper();
124
        $form = $this->configureForm();
125
        $event = new FormEvent($form->reveal(), []);
126
127
        $form->getParent()->willReturn('parent');
128
        $form->add()->shouldNotBeCalled();
129
130
        $this->lockExtension->configureFormFields($formMapper);
131
        $this->eventDispatcher->dispatch(FormEvents::PRE_SET_DATA, $event);
132
    }
133
134
    public function testConfigureFormFieldsWhenModelManagerHasNoLockedVersion(): void
135
    {
136
        $formMapper = $this->configureFormMapper();
137
        $form = $this->configureForm();
138
        $this->configureAdmin(null, null, $this->modelManager->reveal());
139
        $event = new FormEvent($form->reveal(), []);
140
141
        $this->modelManager->getLockVersion([])->willReturn(null);
142
        $form->add()->shouldNotBeCalled();
143
144
        $this->lockExtension->configureFormFields($formMapper);
145
        $this->eventDispatcher->dispatch(FormEvents::PRE_SET_DATA, $event);
146
    }
147
148
    public function testPreUpdateIfAdminHasNoRequest(): void
149
    {
150
        $this->modelManager->lock()->shouldNotBeCalled();
151
152
        $this->lockExtension->preUpdate($this->admin->reveal(), $this->object);
153
    }
154
155
    public function testPreUpdateIfObjectIsNotVersioned(): void
156
    {
157
        $this->configureAdmin();
158
        $this->modelManager->lock()->shouldNotBeCalled();
159
160
        $this->lockExtension->preUpdate($this->admin->reveal(), $this->object);
161
    }
162
163
    public function testPreUpdateIfRequestDoesNotHaveLockVersion(): void
164
    {
165
        $uniqid = 'admin123';
166
        $this->configureAdmin($uniqid, $this->request);
167
168
        $this->modelManager->lock()->shouldNotBeCalled();
169
170
        $this->request->request->set($uniqid, ['something']);
171
        $this->lockExtension->preUpdate($this->admin->reveal(), $this->object);
172
    }
173
174
    public function testPreUpdateIfModelManagerIsNotImplementingLockerInterface(): void
175
    {
176
        $modelManager = $this->prophesize(ModelManagerInterface::class);
177
        $uniqid = 'admin123';
178
        $this->configureAdmin($uniqid, $this->request, $modelManager->reveal());
179
        $this->modelManager->lock()->shouldNotBeCalled();
180
181
        $this->request->request->set($uniqid, ['_lock_version' => 1]);
182
        $this->lockExtension->preUpdate($this->admin->reveal(), $this->object);
183
    }
184
185
    public function testPreUpdateIfObjectIsVersioned(): void
186
    {
187
        $uniqid = 'admin123';
188
        $this->configureAdmin($uniqid, $this->request, $this->modelManager->reveal());
189
190
        $this->modelManager->lock($this->object, 1)->shouldBeCalled();
191
192
        $this->request->request->set($uniqid, ['_lock_version' => 1]);
193
        $this->lockExtension->preUpdate($this->admin->reveal(), $this->object);
194
    }
195
196
    private function configureForm(): ObjectProphecy
197
    {
198
        $form = $this->prophesize(FormInterface::class);
199
200
        $form->getData()->willReturn($this->object);
201
        $form->getParent()->willReturn(null);
202
203
        return $form;
204
    }
205
206
    private function configureFormMapper(): FormMapper
207
    {
208
        $contractor = $this->prophesize(FormContractorInterface::class);
209
        $formFactory = $this->prophesize(FormFactoryInterface::class);
210
        $formBuilder = new FormBuilder('form', null, $this->eventDispatcher, $formFactory->reveal());
211
212
        return new FormMapper($contractor->reveal(), $formBuilder, $this->admin->reveal());
213
    }
214
215
    private function configureAdmin(
216
        ?string $uniqid = null,
217
        ?Request $request = null,
218
        $modelManager = null
219
    ): void {
220
        $this->admin->getUniqid()->willReturn($uniqid);
221
        $this->admin->getRequest()->willReturn($request);
222
        $this->admin->hasRequest()->willReturn(null !== $request);
223
        $this->admin->getModelManager()->willReturn($modelManager);
224
    }
225
}
226