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\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\PersistenceEvent; |
||
25 | use Sonata\AdminBundle\Form\FormMapper; |
||
26 | use Sonata\AdminBundle\Show\ShowMapper; |
||
27 | use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
||
28 | |||
29 | class AdminEventExtensionTest extends TestCase |
||
30 | { |
||
31 | public function getExtension(array $args): AdminExtensionInterface |
||
32 | { |
||
33 | $eventDispatcher = $this->createMock(EventDispatcherInterface::class); |
||
34 | $stub = $eventDispatcher->expects($this->once())->method('dispatch'); |
||
35 | $stub->with(...$args); |
||
36 | |||
37 | return new AdminEventExtension($eventDispatcher); |
||
0 ignored issues
–
show
|
|||
38 | } |
||
39 | |||
40 | public function getMapper($class) |
||
41 | { |
||
42 | $mapper = $this->getMockBuilder($class)->disableOriginalConstructor()->getMock(); |
||
43 | $mapper->expects($this->once())->method('getAdmin')->willReturn($this->createMock(AdminInterface::class)); |
||
44 | |||
45 | return $mapper; |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * @param $type |
||
50 | * |
||
51 | * @return callable |
||
52 | */ |
||
53 | public function getConfigureEventClosure($type) |
||
54 | { |
||
55 | return static function ($event) use ($type) { |
||
56 | if (!$event instanceof ConfigureEvent) { |
||
57 | return false; |
||
58 | } |
||
59 | |||
60 | if ($event->getType() !== $type) { |
||
61 | return false; |
||
62 | } |
||
63 | |||
64 | return true; |
||
65 | }; |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * @param $type |
||
70 | * |
||
71 | * @return callable |
||
72 | */ |
||
73 | public function getConfigurePersistenceClosure($type) |
||
74 | { |
||
75 | return static function ($event) use ($type) { |
||
76 | if (!$event instanceof PersistenceEvent) { |
||
77 | return false; |
||
78 | } |
||
79 | |||
80 | if ($event->getType() !== $type) { |
||
81 | return false; |
||
82 | } |
||
83 | |||
84 | return true; |
||
85 | }; |
||
86 | } |
||
87 | |||
88 | public function testConfigureFormFields(): void |
||
89 | { |
||
90 | $this |
||
91 | ->getExtension([ |
||
92 | $this->equalTo('sonata.admin.event.configure.form'), |
||
93 | $this->callback($this->getConfigureEventClosure(ConfigureEvent::TYPE_FORM)), |
||
94 | ]) |
||
95 | ->configureFormFields($this->getMapper(FormMapper::class)); |
||
96 | } |
||
97 | |||
98 | public function testConfigureListFields(): void |
||
99 | { |
||
100 | $this |
||
101 | ->getExtension([ |
||
102 | $this->equalTo('sonata.admin.event.configure.list'), |
||
103 | $this->callback($this->getConfigureEventClosure(ConfigureEvent::TYPE_LIST)), |
||
104 | ]) |
||
105 | ->configureListFields($this->getMapper(ListMapper::class)); |
||
106 | } |
||
107 | |||
108 | public function testConfigureDatagridFields(): void |
||
109 | { |
||
110 | $this |
||
111 | ->getExtension([ |
||
112 | $this->equalTo('sonata.admin.event.configure.datagrid'), |
||
113 | $this->callback($this->getConfigureEventClosure(ConfigureEvent::TYPE_DATAGRID)), |
||
114 | ]) |
||
115 | ->configureDatagridFilters($this->getMapper(DatagridMapper::class)); |
||
116 | } |
||
117 | |||
118 | public function testConfigureShowFields(): void |
||
119 | { |
||
120 | $this |
||
121 | ->getExtension([ |
||
122 | $this->equalTo('sonata.admin.event.configure.show'), |
||
123 | $this->callback($this->getConfigureEventClosure(ConfigureEvent::TYPE_SHOW)), |
||
124 | ]) |
||
125 | ->configureShowFields($this->getMapper(ShowMapper::class)); |
||
126 | } |
||
127 | |||
128 | public function testPreUpdate(): void |
||
129 | { |
||
130 | $this->getExtension([ |
||
131 | $this->equalTo('sonata.admin.event.persistence.pre_update'), |
||
132 | $this->callback($this->getConfigurePersistenceClosure(PersistenceEvent::TYPE_PRE_UPDATE)), |
||
133 | ])->preUpdate($this->createMock(AdminInterface::class), new \stdClass()); |
||
134 | } |
||
135 | |||
136 | public function testConfigureQuery(): void |
||
137 | { |
||
138 | $this->getExtension([ |
||
139 | $this->equalTo('sonata.admin.event.configure.query'), |
||
140 | ])->configureQuery($this->createMock(AdminInterface::class), $this->createMock(ProxyQueryInterface::class)); |
||
141 | } |
||
142 | |||
143 | public function testPostUpdate(): void |
||
144 | { |
||
145 | $this->getExtension([ |
||
146 | $this->equalTo('sonata.admin.event.persistence.post_update'), |
||
147 | $this->callback($this->getConfigurePersistenceClosure(PersistenceEvent::TYPE_POST_UPDATE)), |
||
148 | ])->postUpdate($this->createMock(AdminInterface::class), new \stdClass()); |
||
149 | } |
||
150 | |||
151 | public function testPrePersist(): void |
||
152 | { |
||
153 | $this->getExtension([ |
||
154 | $this->equalTo('sonata.admin.event.persistence.pre_persist'), |
||
155 | $this->callback($this->getConfigurePersistenceClosure(PersistenceEvent::TYPE_PRE_PERSIST)), |
||
156 | ])->prePersist($this->createMock(AdminInterface::class), new \stdClass()); |
||
157 | } |
||
158 | |||
159 | public function testPostPersist(): void |
||
160 | { |
||
161 | $this->getExtension([ |
||
162 | $this->equalTo('sonata.admin.event.persistence.post_persist'), |
||
163 | $this->callback($this->getConfigurePersistenceClosure(PersistenceEvent::TYPE_POST_PERSIST)), |
||
164 | ])->postPersist($this->createMock(AdminInterface::class), new \stdClass()); |
||
165 | } |
||
166 | |||
167 | public function testPreRemove(): void |
||
168 | { |
||
169 | $this->getExtension([ |
||
170 | $this->equalTo('sonata.admin.event.persistence.pre_remove'), |
||
171 | $this->callback($this->getConfigurePersistenceClosure(PersistenceEvent::TYPE_PRE_REMOVE)), |
||
172 | ])->preRemove($this->createMock(AdminInterface::class), new \stdClass()); |
||
173 | } |
||
174 | |||
175 | public function testPostRemove(): void |
||
176 | { |
||
177 | $this->getExtension([ |
||
178 | $this->equalTo('sonata.admin.event.persistence.post_remove'), |
||
179 | $this->callback($this->getConfigurePersistenceClosure(PersistenceEvent::TYPE_POST_REMOVE)), |
||
180 | ])->postRemove($this->createMock(AdminInterface::class), new \stdClass()); |
||
181 | } |
||
182 | } |
||
183 |
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.