These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Kunstmaan\AdminListBundle\Tests\AdminList\Configurator; |
||
4 | |||
5 | use ArrayIterator; |
||
6 | use Codeception\Stub; |
||
7 | use Doctrine\Common\Persistence\Mapping\ClassMetadata; |
||
8 | use Doctrine\DBAL\Connection; |
||
9 | use Doctrine\DBAL\Platforms\MySQL57Platform; |
||
10 | use Doctrine\ORM\AbstractQuery; |
||
11 | use Doctrine\ORM\Cache\CacheConfiguration; |
||
12 | use Doctrine\ORM\Configuration; |
||
13 | use Doctrine\ORM\EntityManager; |
||
14 | use Doctrine\ORM\Query; |
||
15 | use Doctrine\ORM\QueryBuilder; |
||
16 | use Kunstmaan\AdminBundle\Helper\Security\Acl\AclHelper; |
||
17 | use Kunstmaan\AdminBundle\Helper\Security\Acl\Permission\PermissionDefinition; |
||
18 | use Kunstmaan\AdminListBundle\AdminList\Configurator\AbstractDoctrineORMAdminListConfigurator; |
||
19 | use Kunstmaan\AdminListBundle\AdminList\Filter; |
||
20 | use Kunstmaan\AdminListBundle\AdminList\FilterBuilder; |
||
21 | use Kunstmaan\AdminListBundle\AdminList\FilterType\ORM\StringFilterType; |
||
22 | use Kunstmaan\AdminListBundle\AdminList\SortableInterface; |
||
23 | use Pagerfanta\Pagerfanta; |
||
24 | use PHPUnit\Framework\TestCase; |
||
25 | use ReflectionClass; |
||
26 | use Kunstmaan\LeadGenerationBundle\Tests\unit\Entity\Popup\Popup; |
||
27 | |||
28 | class ORM extends AbstractDoctrineORMAdminListConfigurator implements SortableInterface |
||
29 | { |
||
30 | /** |
||
31 | * @return mixed |
||
32 | */ |
||
33 | public function getBundleName() |
||
34 | { |
||
35 | return 'SomeBundle'; |
||
36 | } |
||
37 | |||
38 | /** |
||
39 | * @return mixed |
||
40 | */ |
||
41 | public function getEntityName() |
||
42 | { |
||
43 | return 'SomeEntity'; |
||
44 | } |
||
45 | |||
46 | /** |
||
47 | * @return mixed |
||
48 | */ |
||
49 | public function buildFields() |
||
50 | { |
||
51 | return true; |
||
52 | } |
||
53 | |||
54 | /** |
||
55 | * @return string |
||
56 | */ |
||
57 | public function getSortableField() |
||
58 | { |
||
59 | return 'sortfield'; |
||
60 | } |
||
61 | } |
||
62 | |||
63 | class AbstractDoctrineORMAdminListConfiguratorTest extends TestCase |
||
0 ignored issues
–
show
|
|||
64 | { |
||
65 | /** @var ORM $config */ |
||
66 | private $config; |
||
67 | |||
68 | /** @var \PHPUnit_Framework_MockObject_MockObject $em */ |
||
69 | private $em; |
||
70 | |||
71 | /** |
||
72 | * @throws \ReflectionException |
||
73 | */ |
||
74 | public function setUp() |
||
75 | { |
||
76 | $em = $this->createMock(EntityManager::class); |
||
77 | $this->em = $em; |
||
78 | $this->config = new ORM($em); |
||
79 | |||
80 | $mirror = new ReflectionClass(AbstractDoctrineORMAdminListConfigurator::class); |
||
81 | $property = $mirror->getProperty('orderBy'); |
||
82 | $property->setAccessible(true); |
||
83 | $property->setValue($this->config, 'somefield'); |
||
84 | } |
||
85 | |||
86 | public function testGetSetEntityManager() |
||
87 | { |
||
88 | $config = $this->config; |
||
89 | $em = $this->createMock(EntityManager::class); |
||
90 | $config->setEntityManager($em); |
||
91 | $this->assertInstanceOf(EntityManager::class, $config->getEntityManager()); |
||
92 | } |
||
93 | |||
94 | public function testGetSetPermissionDefinition() |
||
95 | { |
||
96 | $config = $this->config; |
||
97 | $this->assertNull($config->getPermissionDefinition()); |
||
98 | $config->setPermissionDefinition(new PermissionDefinition(['something'])); |
||
99 | $this->assertInstanceOf(PermissionDefinition::class, $config->getPermissionDefinition()); |
||
100 | } |
||
101 | |||
102 | public function testGetQuery() |
||
103 | { |
||
104 | $config = $this->config; |
||
105 | $em = $this->em; |
||
106 | $filterBuilder = $this->createMock(FilterBuilder::class); |
||
107 | $filterBuilder->expects($this->once())->method('getCurrentFilters')->willReturn([new Filter('whatever', ['type' => new StringFilterType('whatever')], uniqid())]); |
||
108 | $config->setFilterBuilder($filterBuilder); |
||
109 | $cacheConfig = $this->createMock(CacheConfiguration::class); |
||
110 | $cacheConfig->expects($this->any())->method('getCacheLogger')->willReturn('whatever'); |
||
111 | $fakeConfig = $this->createMock(Configuration::class); |
||
112 | $fakeConfig->expects($this->any())->method('getDefaultQueryHints')->willReturn($fakeConfig); |
||
113 | $fakeConfig->expects($this->any())->method('isSecondLevelCacheEnabled')->willReturn($fakeConfig); |
||
114 | $fakeConfig->expects($this->any())->method('getSecondLevelCacheConfiguration')->willReturn($cacheConfig); |
||
115 | $fakeClassMetaData = Stub::makeEmpty(ClassMetadata::class, ['hasAssociation' => false]); |
||
116 | $qb = $this->createMock(QueryBuilder::class); |
||
117 | $qb->expects($this->any())->method('setParameters')->willReturn($qb); |
||
118 | $qb->expects($this->any())->method('setMaxResults')->willReturn($qb); |
||
119 | $em->expects($this->any())->method('createQuery')->willReturn($qb); |
||
120 | $em->expects($this->any())->method('getRepository')->willReturn($em); |
||
121 | $em->expects($this->any())->method('createQueryBuilder')->willReturn($qb); |
||
122 | $em->expects($this->any())->method('getConfiguration')->willReturn($fakeConfig); |
||
123 | $em->expects($this->any())->method('getClassMetadata')->willReturn($fakeClassMetaData); |
||
124 | $query = new Query($em); |
||
125 | $qb->expects($this->any())->method('getQuery')->willReturn($query); |
||
126 | $query = $config->getQuery(); |
||
127 | $this->assertInstanceOf(Query::class, $query); |
||
128 | } |
||
129 | |||
130 | View Code Duplication | public function testEditUrlFor() |
|
131 | { |
||
132 | $config = $this->config; |
||
133 | $item = new Popup(); |
||
134 | $item->setId(666); |
||
135 | $url = $config->getEditUrlFor($item); |
||
136 | $this->assertCount(2, $url); |
||
137 | $this->assertArrayHasKey('path', $url); |
||
138 | $this->assertArrayHasKey('params', $url); |
||
139 | $this->assertArrayHasKey('id', $url['params']); |
||
140 | $this->assertEquals('somebundle_admin_someentity_edit', $url['path']); |
||
141 | $this->assertCount(1, $url['params']); |
||
142 | $this->assertEquals(666, $url['params']['id']); |
||
143 | } |
||
144 | |||
145 | View Code Duplication | public function testDeleteUrlFor() |
|
146 | { |
||
147 | $config = $this->config; |
||
148 | $item = new Popup(); |
||
149 | $item->setId(666); |
||
150 | $url = $config->getDeleteUrlFor($item); |
||
151 | $this->assertCount(2, $url); |
||
152 | $this->assertArrayHasKey('path', $url); |
||
153 | $this->assertArrayHasKey('params', $url); |
||
154 | $this->assertArrayHasKey('id', $url['params']); |
||
155 | $this->assertEquals('somebundle_admin_someentity_delete', $url['path']); |
||
156 | $this->assertCount(1, $url['params']); |
||
157 | $this->assertEquals(666, $url['params']['id']); |
||
158 | } |
||
159 | |||
160 | /** |
||
161 | * @throws \ReflectionException |
||
162 | */ |
||
163 | public function testGetPagerFanta() |
||
164 | { |
||
165 | $config = $this->config; |
||
166 | $em = $this->em; |
||
167 | |||
168 | $cacheConfig = $this->createMock(CacheConfiguration::class); |
||
169 | $cacheConfig->expects($this->any())->method('getCacheLogger')->willReturn('whatever'); |
||
170 | |||
171 | $fakeConfig = $this->createMock(Configuration::class); |
||
172 | $fakeConfig->expects($this->any())->method('getDefaultQueryHints')->willReturn(['doctrine_paginator.distinct' => 'blah']); |
||
173 | $fakeConfig->expects($this->any())->method('isSecondLevelCacheEnabled')->willReturn($fakeConfig); |
||
174 | $fakeConfig->expects($this->any())->method('getSecondLevelCacheConfiguration')->willReturn($cacheConfig); |
||
175 | |||
176 | $platform = $this->createMock(MySQL57Platform::class); |
||
177 | $platform->expects($this->any())->method('getSQLResultCasing')->willReturn('string'); |
||
178 | |||
179 | $fakeClassMetaData = Stub::makeEmpty(ClassMetadata::class, ['hasAssociation' => false]); |
||
180 | $connection = $this->createMock(Connection::class); |
||
181 | $connection->expects($this->any())->method('getDatabasePlatform')->willReturn($platform); |
||
182 | |||
183 | $qb = $this->createMock(QueryBuilder::class); |
||
184 | $qb->expects($this->any())->method('setParameters')->willReturn($qb); |
||
185 | $qb->expects($this->any())->method('setMaxResults')->willReturn($qb); |
||
186 | $em->expects($this->any())->method('createQuery')->willReturn($qb); |
||
187 | $em->expects($this->any())->method('getRepository')->willReturn($em); |
||
188 | $em->expects($this->any())->method('createQueryBuilder')->willReturn($qb); |
||
189 | $em->expects($this->any())->method('getConfiguration')->willReturn($fakeConfig); |
||
190 | $em->expects($this->any())->method('getConnection')->willReturn($connection); |
||
191 | $em->expects($this->any())->method('getClassMetadata')->willReturn($fakeClassMetaData); |
||
192 | $qb->expects($this->any())->method('getQuery')->willReturn(new Query($em)); |
||
193 | |||
194 | $pager = $config->getPagerfanta(); |
||
195 | $this->assertInstanceOf(Pagerfanta::class, $pager); |
||
196 | |||
197 | $pager = $this->createMock(Pagerfanta::class); |
||
198 | $pager->expects($this->once())->method('getNbResults')->willReturn(5); |
||
199 | $pager->expects($this->once())->method('getCurrentPageResults')->willReturn([1, 2, 3, 4, 5]); |
||
200 | |||
201 | $mirror = new ReflectionClass(AbstractDoctrineORMAdminListConfigurator::class); |
||
202 | $property = $mirror->getProperty('pagerfanta'); |
||
203 | $property->setAccessible(true); |
||
204 | $property->setValue($config, $pager); |
||
205 | |||
206 | $count = $config->getCount(); |
||
207 | $this->assertEquals(5, $count); |
||
208 | |||
209 | $items = $config->getItems(); |
||
210 | $this->assertCount(5, $items); |
||
211 | } |
||
212 | |||
213 | /** |
||
214 | * @throws \ReflectionException |
||
215 | */ |
||
216 | View Code Duplication | public function testGetIterator() |
|
217 | { |
||
218 | $config = $this->config; |
||
219 | $query = $this->createMock(AbstractQuery::class); |
||
220 | $query->expects($this->once())->method('iterate')->willReturn(new ArrayIterator()); |
||
221 | |||
222 | $mirror = new ReflectionClass(AbstractDoctrineORMAdminListConfigurator::class); |
||
223 | $property = $mirror->getProperty('query'); |
||
224 | $property->setAccessible(true); |
||
225 | $property->setValue($config, $query); |
||
226 | |||
227 | $it = $config->getIterator(); |
||
228 | $this->assertInstanceOf(ArrayIterator::class, $it); |
||
229 | } |
||
230 | |||
231 | /** |
||
232 | * @throws \ReflectionException |
||
233 | * @throws \Exception |
||
234 | */ |
||
235 | public function testGetQueryWithAclEnabled() |
||
236 | { |
||
237 | $config = $this->config; |
||
238 | $em = $this->em; |
||
239 | |||
240 | $cacheConfig = $this->createMock(CacheConfiguration::class); |
||
241 | $cacheConfig->expects($this->any())->method('getCacheLogger')->willReturn('whatever'); |
||
242 | $fakeConfig = $this->createMock(Configuration::class); |
||
243 | $fakeConfig->expects($this->any())->method('getDefaultQueryHints')->willReturn($fakeConfig); |
||
244 | $fakeConfig->expects($this->any())->method('isSecondLevelCacheEnabled')->willReturn($fakeConfig); |
||
245 | $fakeConfig->expects($this->any())->method('getSecondLevelCacheConfiguration')->willReturn($cacheConfig); |
||
246 | $qb = $this->createMock(QueryBuilder::class); |
||
247 | $fakeClassMetaData = Stub::makeEmpty(ClassMetadata::class, ['hasAssociation' => false]); |
||
248 | $qb->expects($this->any())->method('setParameters')->willReturn($qb); |
||
249 | $qb->expects($this->any())->method('setMaxResults')->willReturn($qb); |
||
250 | $em->expects($this->any())->method('createQuery')->willReturn($qb); |
||
251 | $em->expects($this->any())->method('getRepository')->willReturn($em); |
||
252 | $em->expects($this->any())->method('createQueryBuilder')->willReturn($qb); |
||
253 | $em->expects($this->any())->method('getConfiguration')->willReturn($fakeConfig); |
||
254 | $em->expects($this->any())->method('getClassMetadata')->willReturn($fakeClassMetaData); |
||
255 | |||
256 | $query = new Query($em); |
||
257 | $aclHelper = $this->createMock(AclHelper::class); |
||
258 | $aclHelper->expects($this->once())->method('apply')->willReturn($query); |
||
259 | $mirror = new ReflectionClass(AbstractDoctrineORMAdminListConfigurator::class); |
||
260 | $property = $mirror->getProperty('aclHelper'); |
||
261 | $property->setAccessible(true); |
||
262 | $property->setValue($config, $aclHelper); |
||
263 | $property = $mirror->getProperty('permissionDef'); |
||
264 | $property->setAccessible(true); |
||
265 | $property->setValue($config, new PermissionDefinition(['admin'])); |
||
266 | |||
267 | $qb->expects($this->any())->method('getQuery')->willReturn($query); |
||
268 | $query = $config->getQuery(); |
||
269 | $this->assertInstanceOf(Query::class, $query); |
||
270 | } |
||
271 | } |
||
272 |
Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.