Completed
Push — master ( 91fdab...75a7b9 )
by
unknown
13:37
created

AbstractDoctrineORMAdminListConfiguratorTest.php (3 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
namespace Kunstmaan\AdminListBundle\Tests\AdminList\Configurator;
4
5
use ArrayIterator;
6
use Doctrine\DBAL\Connection;
7
use Doctrine\DBAL\Platforms\MySQL57Platform;
8
use Doctrine\ORM\AbstractQuery;
9
use Doctrine\ORM\Cache\CacheConfiguration;
10
use Doctrine\ORM\Configuration;
11
use Doctrine\ORM\EntityManager;
12
use Doctrine\ORM\Query;
13
use Doctrine\ORM\QueryBuilder;
14
use Kunstmaan\AdminBundle\Helper\Security\Acl\AclHelper;
15
use Kunstmaan\AdminBundle\Helper\Security\Acl\Permission\PermissionDefinition;
16
use Kunstmaan\AdminListBundle\AdminList\Configurator\AbstractDoctrineORMAdminListConfigurator;
17
use Kunstmaan\AdminListBundle\AdminList\Filter;
18
use Kunstmaan\AdminListBundle\AdminList\FilterBuilder;
19
use Kunstmaan\AdminListBundle\AdminList\FilterType\ORM\StringFilterType;
20
use Kunstmaan\AdminListBundle\AdminList\SortableInterface;
21
use Pagerfanta\Pagerfanta;
22
use PHPUnit_Framework_TestCase;
23
use ReflectionClass;
24
use Kunstmaan\LeadGenerationBundle\Tests\unit\Entity\Popup\Popup;
25
26
class ORM extends AbstractDoctrineORMAdminListConfigurator implements SortableInterface
27
{
28
    /**
29
     * @return mixed
0 ignored issues
show
Consider making the return type a bit more specific; maybe use string.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
30
     */
31
    public function getBundleName()
32
    {
33
        return 'SomeBundle';
34
    }
35
36
    /**
37
     * @return mixed
0 ignored issues
show
Consider making the return type a bit more specific; maybe use string.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
38
     */
39
    public function getEntityName()
40
    {
41
        return 'SomeEntity';
42
    }
43
44
    /**
45
     * @return mixed
0 ignored issues
show
Consider making the return type a bit more specific; maybe use boolean.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
46
     */
47
    public function buildFields()
48
    {
49
        return true;
50
    }
51
52
    /**
53
     * @return string
54
     */
55
    public function getSortableField()
56
    {
57
        return 'sortfield';
58
    }
59
60
61
}
62
63
class AbstractDoctrineORMAdminListConfiguratorTest extends PHPUnit_Framework_TestCase
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
        $qb = $this->createMock(QueryBuilder::class);
116
        $qb->expects($this->any())->method('setParameters')->willReturn($qb);
117
        $qb->expects($this->any())->method('setMaxResults')->willReturn($qb);
118
        $em->expects($this->any())->method('createQuery')->willReturn($qb);
119
        $em->expects($this->any())->method('getRepository')->willReturn($em);
120
        $em->expects($this->any())->method('createQueryBuilder')->willReturn($qb);
121
        $em->expects($this->any())->method('getConfiguration')->willReturn($fakeConfig);
122
        $query = new Query($em);
123
        $qb->expects($this->any())->method('getQuery')->willReturn($query);
124
        $query = $config->getQuery();
125
        $this->assertInstanceOf(Query::class, $query);
126
    }
127
128 View Code Duplication
    public function testEditUrlFor()
129
    {
130
        $config = $this->config;
131
        $item = new Popup();
132
        $item->setId(666);
133
        $url = $config->getEditUrlFor($item);
134
        $this->assertCount(2, $url);
135
        $this->assertArrayHasKey('path', $url);
136
        $this->assertArrayHasKey('params', $url);
137
        $this->assertArrayHasKey('id', $url['params']);
138
        $this->assertEquals('somebundle_admin_someentity_edit', $url['path']);
139
        $this->assertCount(1, $url['params']);
140
        $this->assertEquals(666, $url['params']['id']);
141
    }
142
143 View Code Duplication
    public function testDeleteUrlFor()
144
    {
145
        $config = $this->config;
146
        $item = new Popup();
147
        $item->setId(666);
148
        $url = $config->getDeleteUrlFor($item);
149
        $this->assertCount(2, $url);
150
        $this->assertArrayHasKey('path', $url);
151
        $this->assertArrayHasKey('params', $url);
152
        $this->assertArrayHasKey('id', $url['params']);
153
        $this->assertEquals('somebundle_admin_someentity_delete', $url['path']);
154
        $this->assertCount(1, $url['params']);
155
        $this->assertEquals(666, $url['params']['id']);
156
    }
157
158
    /**
159
     * @throws \ReflectionException
160
     */
161
    public function testGetPagerFanta()
162
    {
163
        $config = $this->config;
164
        $em = $this->em;
165
166
        $cacheConfig = $this->createMock(CacheConfiguration::class);
167
        $cacheConfig->expects($this->any())->method('getCacheLogger')->willReturn('whatever');
168
169
        $fakeConfig = $this->createMock(Configuration::class);
170
        $fakeConfig->expects($this->any())->method('getDefaultQueryHints')->willReturn(['doctrine_paginator.distinct' => 'blah']);
171
        $fakeConfig->expects($this->any())->method('isSecondLevelCacheEnabled')->willReturn($fakeConfig);
172
        $fakeConfig->expects($this->any())->method('getSecondLevelCacheConfiguration')->willReturn($cacheConfig);
173
174
        $platform = $this->createMock(MySQL57Platform::class);
175
        $platform->expects($this->any())->method('getSQLResultCasing')->willReturn('string');
176
177
        $connection = $this->createMock(Connection::class);
178
        $connection->expects($this->any())->method('getDatabasePlatform')->willReturn($platform);
179
180
        $qb = $this->createMock(QueryBuilder::class);
181
        $qb->expects($this->any())->method('setParameters')->willReturn($qb);
182
        $qb->expects($this->any())->method('setMaxResults')->willReturn($qb);
183
        $em->expects($this->any())->method('createQuery')->willReturn($qb);
184
        $em->expects($this->any())->method('getRepository')->willReturn($em);
185
        $em->expects($this->any())->method('createQueryBuilder')->willReturn($qb);
186
        $em->expects($this->any())->method('getConfiguration')->willReturn($fakeConfig);
187
        $em->expects($this->any())->method('getConnection')->willReturn($connection);
188
        $qb->expects($this->any())->method('getQuery')->willReturn(new Query($em));
189
190
        $pager = $config->getPagerfanta();
191
        $this->assertInstanceOf(Pagerfanta::class, $pager);
192
193
        $pager = $this->createMock(Pagerfanta::class);
194
        $pager->expects($this->once())->method('getNbResults')->willReturn(5);
195
        $pager->expects($this->once())->method('getCurrentPageResults')->willReturn([1,2,3,4,5]);
196
197
        $mirror = new ReflectionClass(AbstractDoctrineORMAdminListConfigurator::class);
198
        $property = $mirror->getProperty('pagerfanta');
199
        $property->setAccessible(true);
200
        $property->setValue($config, $pager);
201
202
        $count = $config->getCount();
203
        $this->assertEquals(5, $count);
204
205
        $items = $config->getItems();
206
        $this->assertCount(5, $items);
207
    }
208
209
    /**
210
     * @throws \ReflectionException
211
     */
212 View Code Duplication
    public function testGetIterator()
213
    {
214
        $config = $this->config;
215
        $query = $this->createMock(AbstractQuery::class);
216
        $query->expects($this->once())->method('iterate')->willReturn(new ArrayIterator());
217
218
        $mirror = new ReflectionClass(AbstractDoctrineORMAdminListConfigurator::class);
219
        $property = $mirror->getProperty('query');
220
        $property->setAccessible(true);
221
        $property->setValue($config, $query);
222
223
        $it = $config->getIterator();
224
        $this->assertInstanceOf(ArrayIterator::class, $it);
225
    }
226
227
    /**
228
     * @throws \ReflectionException
229
     */
230
    public function testGetQueryWithAclEnabled()
231
    {
232
        $config = $this->config;
233
        $em = $this->em;
234
235
        $cacheConfig = $this->createMock(CacheConfiguration::class);
236
        $cacheConfig->expects($this->any())->method('getCacheLogger')->willReturn('whatever');
237
        $fakeConfig = $this->createMock(Configuration::class);
238
        $fakeConfig->expects($this->any())->method('getDefaultQueryHints')->willReturn($fakeConfig);
239
        $fakeConfig->expects($this->any())->method('isSecondLevelCacheEnabled')->willReturn($fakeConfig);
240
        $fakeConfig->expects($this->any())->method('getSecondLevelCacheConfiguration')->willReturn($cacheConfig);
241
        $qb = $this->createMock(QueryBuilder::class);
242
        $qb->expects($this->any())->method('setParameters')->willReturn($qb);
243
        $qb->expects($this->any())->method('setMaxResults')->willReturn($qb);
244
        $em->expects($this->any())->method('createQuery')->willReturn($qb);
245
        $em->expects($this->any())->method('getRepository')->willReturn($em);
246
        $em->expects($this->any())->method('createQueryBuilder')->willReturn($qb);
247
        $em->expects($this->any())->method('getConfiguration')->willReturn($fakeConfig);
248
249
        $query = new Query($em);
250
        $aclHelper = $this->createMock(AclHelper::class);
251
        $aclHelper->expects($this->once())->method('apply')->willReturn($query);
252
        $mirror = new ReflectionClass(AbstractDoctrineORMAdminListConfigurator::class);
253
        $property = $mirror->getProperty('aclHelper');
254
        $property->setAccessible(true);
255
        $property->setValue($config, $aclHelper);
256
        $property = $mirror->getProperty('permissionDef');
257
        $property->setAccessible(true);
258
        $property->setValue($config, new PermissionDefinition(['admin']));
259
260
261
        $qb->expects($this->any())->method('getQuery')->willReturn($query);
262
        $query = $config->getQuery();
263
        $this->assertInstanceOf(Query::class, $query);
264
    }
265
}
266