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\Event; |
15
|
|
|
|
16
|
|
|
use PHPUnit\Framework\TestCase; |
17
|
|
|
use Sonata\AdminBundle\Admin\AdminExtensionInterface; |
18
|
|
|
use Sonata\AdminBundle\Admin\AdminInterface; |
19
|
|
|
use Sonata\AdminBundle\Datagrid\DatagridMapper; |
20
|
|
|
use Sonata\AdminBundle\Datagrid\ListMapper; |
21
|
|
|
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface; |
22
|
|
|
use Sonata\AdminBundle\Event\AdminEventExtension; |
23
|
|
|
use Sonata\AdminBundle\Event\ConfigureEvent; |
24
|
|
|
use Sonata\AdminBundle\Event\ConfigureQueryEvent; |
25
|
|
|
use Sonata\AdminBundle\Event\PersistenceEvent; |
26
|
|
|
use Sonata\AdminBundle\Form\FormMapper; |
27
|
|
|
use Sonata\AdminBundle\Show\ShowMapper; |
28
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
29
|
|
|
|
30
|
|
|
class AdminEventExtensionTest extends TestCase |
31
|
|
|
{ |
32
|
|
|
public function getExtension(array $args): AdminExtensionInterface |
33
|
|
|
{ |
34
|
|
|
$eventDispatcher = $this->createMock(EventDispatcherInterface::class); |
35
|
|
|
$stub = $eventDispatcher->expects($this->once())->method('dispatch'); |
36
|
|
|
$stub->with(...$args); |
37
|
|
|
|
38
|
|
|
return new AdminEventExtension($eventDispatcher); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function getMapper($class) |
42
|
|
|
{ |
43
|
|
|
$mapper = $this->getMockBuilder($class)->disableOriginalConstructor()->getMock(); |
44
|
|
|
$mapper->expects($this->once())->method('getAdmin')->willReturn($this->createMock(AdminInterface::class)); |
45
|
|
|
|
46
|
|
|
return $mapper; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function getConfigureEventClosure(string $type): callable |
50
|
|
|
{ |
51
|
|
|
return static function ($event) use ($type): bool { |
52
|
|
|
if (!$event instanceof ConfigureEvent) { |
53
|
|
|
return false; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
if ($event->getType() !== $type) { |
57
|
|
|
return false; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return true; |
61
|
|
|
}; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function getConfigurePersistenceClosure(string $type): callable |
65
|
|
|
{ |
66
|
|
|
return static function ($event) use ($type): bool { |
67
|
|
|
if (!$event instanceof PersistenceEvent) { |
68
|
|
|
return false; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
if ($event->getType() !== $type) { |
72
|
|
|
return false; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return true; |
76
|
|
|
}; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function testConfigureFormFields(): void |
80
|
|
|
{ |
81
|
|
|
$this |
82
|
|
|
->getExtension([ |
83
|
|
|
$this->callback($this->getConfigureEventClosure(ConfigureEvent::TYPE_FORM)), |
84
|
|
|
$this->equalTo('sonata.admin.event.configure.form'), |
85
|
|
|
]) |
86
|
|
|
->configureFormFields($this->getMapper(FormMapper::class)); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function testConfigureListFields(): void |
90
|
|
|
{ |
91
|
|
|
$this |
92
|
|
|
->getExtension([ |
93
|
|
|
$this->callback($this->getConfigureEventClosure(ConfigureEvent::TYPE_LIST)), |
94
|
|
|
$this->equalTo('sonata.admin.event.configure.list'), |
95
|
|
|
]) |
96
|
|
|
->configureListFields($this->getMapper(ListMapper::class)); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function testConfigureDatagridFields(): void |
100
|
|
|
{ |
101
|
|
|
$this |
102
|
|
|
->getExtension([ |
103
|
|
|
$this->callback($this->getConfigureEventClosure(ConfigureEvent::TYPE_DATAGRID)), |
104
|
|
|
$this->equalTo('sonata.admin.event.configure.datagrid'), |
105
|
|
|
]) |
106
|
|
|
->configureDatagridFilters($this->getMapper(DatagridMapper::class)); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function testConfigureShowFields(): void |
110
|
|
|
{ |
111
|
|
|
$this |
112
|
|
|
->getExtension([ |
113
|
|
|
$this->callback($this->getConfigureEventClosure(ConfigureEvent::TYPE_SHOW)), |
114
|
|
|
$this->equalTo('sonata.admin.event.configure.show'), |
115
|
|
|
]) |
116
|
|
|
->configureShowFields($this->getMapper(ShowMapper::class)); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function testPreUpdate(): void |
120
|
|
|
{ |
121
|
|
|
$this->getExtension([ |
122
|
|
|
$this->callback($this->getConfigurePersistenceClosure(PersistenceEvent::TYPE_PRE_UPDATE)), |
123
|
|
|
$this->equalTo('sonata.admin.event.persistence.pre_update'), |
124
|
|
|
])->preUpdate($this->createMock(AdminInterface::class), new \stdClass()); |
|
|
|
|
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function testConfigureQuery(): void |
128
|
|
|
{ |
129
|
|
|
$this->getExtension([ |
130
|
|
|
$this->isInstanceOf(ConfigureQueryEvent::class), |
131
|
|
|
$this->equalTo('sonata.admin.event.configure.query'), |
132
|
|
|
])->configureQuery($this->createMock(AdminInterface::class), $this->createMock(ProxyQueryInterface::class)); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function testPostUpdate(): void |
136
|
|
|
{ |
137
|
|
|
$this->getExtension([ |
138
|
|
|
$this->callback($this->getConfigurePersistenceClosure(PersistenceEvent::TYPE_POST_UPDATE)), |
139
|
|
|
$this->equalTo('sonata.admin.event.persistence.post_update'), |
140
|
|
|
])->postUpdate($this->createMock(AdminInterface::class), new \stdClass()); |
|
|
|
|
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
public function testPrePersist(): void |
144
|
|
|
{ |
145
|
|
|
$this->getExtension([ |
146
|
|
|
$this->callback($this->getConfigurePersistenceClosure(PersistenceEvent::TYPE_PRE_PERSIST)), |
147
|
|
|
$this->equalTo('sonata.admin.event.persistence.pre_persist'), |
148
|
|
|
])->prePersist($this->createMock(AdminInterface::class), new \stdClass()); |
|
|
|
|
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
public function testPostPersist(): void |
152
|
|
|
{ |
153
|
|
|
$this->getExtension([ |
154
|
|
|
$this->callback($this->getConfigurePersistenceClosure(PersistenceEvent::TYPE_POST_PERSIST)), |
155
|
|
|
$this->equalTo('sonata.admin.event.persistence.post_persist'), |
156
|
|
|
])->postPersist($this->createMock(AdminInterface::class), new \stdClass()); |
|
|
|
|
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
public function testPreRemove(): void |
160
|
|
|
{ |
161
|
|
|
$this->getExtension([ |
162
|
|
|
$this->callback($this->getConfigurePersistenceClosure(PersistenceEvent::TYPE_PRE_REMOVE)), |
163
|
|
|
$this->equalTo('sonata.admin.event.persistence.pre_remove'), |
164
|
|
|
])->preRemove($this->createMock(AdminInterface::class), new \stdClass()); |
|
|
|
|
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
public function testPostRemove(): void |
168
|
|
|
{ |
169
|
|
|
$this->getExtension([ |
170
|
|
|
$this->callback($this->getConfigurePersistenceClosure(PersistenceEvent::TYPE_POST_REMOVE)), |
171
|
|
|
$this->equalTo('sonata.admin.event.persistence.post_remove'), |
172
|
|
|
])->postRemove($this->createMock(AdminInterface::class), new \stdClass()); |
|
|
|
|
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
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: