testModelManagerImplementsLockInterface()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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 Sonata\AdminBundle\Tests\App\Model\ModelManager;
25
use Symfony\Component\EventDispatcher\EventDispatcher;
26
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
27
use Symfony\Component\Form\FormBuilder;
28
use Symfony\Component\Form\FormEvent;
29
use Symfony\Component\Form\FormEvents;
30
use Symfony\Component\Form\FormFactoryInterface;
31
use Symfony\Component\Form\FormInterface;
32
use Symfony\Component\HttpFoundation\Request;
33
34
class LockExtensionTest extends TestCase
35
{
36
    /**
37
     * @var LockExtension
38
     */
39
    private $lockExtension;
40
41
    /**
42
     * @var EventDispatcherInterface
43
     */
44
    private $eventDispatcher;
45
46
    /**
47
     * @var AdminInterface
48
     */
49
    private $admin;
50
51
    /**
52
     * @var LockInterface
53
     */
54
    private $modelManager;
55
56
    /**
57
     * @var stdClass
58
     */
59
    private $object;
60
61
    /**
62
     * @var Request
63
     */
64
    private $request;
65
66
    protected function setUp(): void
67
    {
68
        $this->modelManager = $this->prophesize(ModelManager::class);
69
        $this->admin = $this->prophesize(AbstractAdmin::class);
70
        $this->eventDispatcher = new EventDispatcher();
71
        $this->request = new Request();
72
        $this->object = new \stdClass();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \stdClass() of type object<stdClass> is incompatible with the declared type object<Sonata\AdminBundl...min\Extension\stdClass> of property $object.

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...
73
        $this->lockExtension = new LockExtension();
74
    }
75
76
    public function testModelManagerImplementsLockInterface(): void
77
    {
78
        $this->assertInstanceOf(LockInterface::class, $this->modelManager->reveal());
0 ignored issues
show
Bug introduced by
The method reveal() does not seem to exist on object<Sonata\AdminBundle\Model\LockInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
79
    }
80
81
    public function testConfigureFormFields(): void
82
    {
83
        $formMapper = $this->configureFormMapper();
84
        $form = $this->configureForm();
85
        $this->configureAdmin(null, null, $this->modelManager->reveal());
0 ignored issues
show
Bug introduced by
The method reveal() does not seem to exist on object<Sonata\AdminBundle\Model\LockInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
86
        $event = new FormEvent($form->reveal(), []);
87
88
        $this->modelManager->getLockVersion([])->willReturn(1);
0 ignored issues
show
Documentation introduced by
array() is of type array, but the function expects a object.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
89
90
        $form->add(
91
            '_lock_version',
92
            HiddenType::class,
93
            ['mapped' => false, 'data' => 1]
94
        )->shouldBeCalled();
95
96
        $this->lockExtension->configureFormFields($formMapper);
97
        $this->eventDispatcher->dispatch($event, FormEvents::PRE_SET_DATA);
98
    }
99
100
    public function testConfigureFormFieldsWhenModelManagerIsNotImplementingLockerInterface(): void
101
    {
102
        $modelManager = $this->prophesize(ModelManagerInterface::class);
103
        $formMapper = $this->configureFormMapper();
104
        $form = $this->configureForm();
105
        $this->configureAdmin(null, null, $modelManager->reveal());
106
        $event = new FormEvent($form->reveal(), []);
107
108
        $form->add()->shouldNotBeCalled();
109
110
        $this->lockExtension->configureFormFields($formMapper);
111
        $this->eventDispatcher->dispatch($event, FormEvents::PRE_SET_DATA);
112
    }
113
114
    public function testConfigureFormFieldsWhenFormEventHasNoData(): void
115
    {
116
        $formMapper = $this->configureFormMapper();
117
        $form = $this->configureForm();
118
        $event = new FormEvent($form->reveal(), null);
119
120
        $form->add()->shouldNotBeCalled();
121
122
        $this->lockExtension->configureFormFields($formMapper);
123
        $this->eventDispatcher->dispatch($event, FormEvents::PRE_SET_DATA);
124
    }
125
126
    public function testConfigureFormFieldsWhenFormHasParent(): void
127
    {
128
        $formMapper = $this->configureFormMapper();
129
        $form = $this->configureForm();
130
        $event = new FormEvent($form->reveal(), []);
131
132
        $form->getParent()->willReturn('parent');
133
        $form->add()->shouldNotBeCalled();
134
135
        $this->lockExtension->configureFormFields($formMapper);
136
        $this->eventDispatcher->dispatch($event, FormEvents::PRE_SET_DATA);
137
    }
138
139
    public function testConfigureFormFieldsWhenModelManagerHasNoLockedVersion(): void
140
    {
141
        $formMapper = $this->configureFormMapper();
142
        $form = $this->configureForm();
143
        $this->configureAdmin(null, null, $this->modelManager->reveal());
0 ignored issues
show
Bug introduced by
The method reveal() does not seem to exist on object<Sonata\AdminBundle\Model\LockInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
144
        $event = new FormEvent($form->reveal(), []);
145
146
        $this->modelManager->getLockVersion([])->willReturn(null);
0 ignored issues
show
Documentation introduced by
array() is of type array, but the function expects a object.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
147
        $form->add()->shouldNotBeCalled();
148
149
        $this->lockExtension->configureFormFields($formMapper);
150
        $this->eventDispatcher->dispatch($event, FormEvents::PRE_SET_DATA);
151
    }
152
153
    public function testPreUpdateIfAdminHasNoRequest(): void
154
    {
155
        $this->configureAdmin();
156
        $this->modelManager->lock()->shouldNotBeCalled();
0 ignored issues
show
Bug introduced by
The call to lock() misses some required arguments starting with $object.
Loading history...
157
158
        $this->lockExtension->preUpdate($this->admin->reveal(), $this->object);
159
    }
160
161
    public function testPreUpdateIfObjectIsNotVersioned(): void
162
    {
163
        $this->configureAdmin();
164
        $this->modelManager->lock()->shouldNotBeCalled();
0 ignored issues
show
Bug introduced by
The call to lock() misses some required arguments starting with $object.
Loading history...
165
166
        $this->lockExtension->preUpdate($this->admin->reveal(), $this->object);
167
    }
168
169
    public function testPreUpdateIfRequestDoesNotHaveLockVersion(): void
170
    {
171
        $uniqid = 'admin123';
172
        $this->configureAdmin($uniqid, $this->request);
173
174
        $this->modelManager->lock()->shouldNotBeCalled();
0 ignored issues
show
Bug introduced by
The call to lock() misses some required arguments starting with $object.
Loading history...
175
176
        $this->request->request->set($uniqid, ['something']);
177
        $this->lockExtension->preUpdate($this->admin->reveal(), $this->object);
178
    }
179
180
    public function testPreUpdateIfModelManagerIsNotImplementingLockerInterface(): void
181
    {
182
        $modelManager = $this->prophesize(ModelManagerInterface::class);
183
        $uniqid = 'admin123';
184
        $this->configureAdmin($uniqid, $this->request, $modelManager->reveal());
185
        $this->modelManager->lock()->shouldNotBeCalled();
0 ignored issues
show
Bug introduced by
The call to lock() misses some required arguments starting with $object.
Loading history...
186
187
        $this->request->request->set($uniqid, ['_lock_version' => 1]);
188
        $this->lockExtension->preUpdate($this->admin->reveal(), $this->object);
189
    }
190
191
    public function testPreUpdateIfObjectIsVersioned(): void
192
    {
193
        $uniqid = 'admin123';
194
        $this->configureAdmin($uniqid, $this->request, $this->modelManager->reveal());
0 ignored issues
show
Bug introduced by
The method reveal() does not seem to exist on object<Sonata\AdminBundle\Model\LockInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
195
196
        $this->modelManager->lock($this->object, 1)->shouldBeCalled();
197
198
        $this->request->request->set($uniqid, ['_lock_version' => 1]);
199
        $this->lockExtension->preUpdate($this->admin->reveal(), $this->object);
200
    }
201
202
    private function configureForm(): ObjectProphecy
203
    {
204
        $form = $this->prophesize(FormInterface::class);
205
206
        $form->getData()->willReturn($this->object);
207
        $form->getParent()->willReturn(null);
208
209
        return $form;
210
    }
211
212
    private function configureFormMapper(): FormMapper
213
    {
214
        $contractor = $this->prophesize(FormContractorInterface::class);
215
        $formFactory = $this->prophesize(FormFactoryInterface::class);
216
        $formBuilder = new FormBuilder('form', null, $this->eventDispatcher, $formFactory->reveal());
217
218
        return new FormMapper($contractor->reveal(), $formBuilder, $this->admin->reveal());
219
    }
220
221
    private function configureAdmin(
222
        ?string $uniqid = null,
223
        ?Request $request = null,
224
        ?ModelManagerInterface $modelManager = null
225
    ): void {
226
        $this->admin->getUniqid()->willReturn($uniqid);
227
        $this->admin->getRequest()->willReturn($request);
228
        $this->admin->hasRequest()->willReturn(null !== $request);
229
        $this->admin->getModelManager()->willReturn($modelManager);
230
    }
231
}
232