Completed
Push — master ( 1af7e7...796d56 )
by Jeroen
16s
created

AbstractDoctrineORMAdminListConfiguratorTest.php (1 issue)

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
30
     */
31
    public function getBundleName()
32
    {
33
        return 'SomeBundle';
34
    }
35
36
    /**
37
     * @return mixed
38
     */
39
    public function getEntityName()
40
    {
41
        return 'SomeEntity';
42
    }
43
44
    /**
45
     * @return mixed
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
class AbstractDoctrineORMAdminListConfiguratorTest extends PHPUnit_Framework_TestCase
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

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.

Loading history...
62
{
63
    /** @var ORM $config */
64
    private $config;
65
66
    /** @var \PHPUnit_Framework_MockObject_MockObject $em */
67
    private $em;
68
69
    /**
70
     * @throws \ReflectionException
71
     */
72
    public function setUp()
73
    {
74
        $em = $this->createMock(EntityManager::class);
75
        $this->em = $em;
76
        $this->config = new ORM($em);
77
78
        $mirror = new ReflectionClass(AbstractDoctrineORMAdminListConfigurator::class);
79
        $property = $mirror->getProperty('orderBy');
80
        $property->setAccessible(true);
81
        $property->setValue($this->config, 'somefield');
82
    }
83
84
    public function testGetSetEntityManager()
85
    {
86
        $config = $this->config;
87
        $em = $this->createMock(EntityManager::class);
88
        $config->setEntityManager($em);
89
        $this->assertInstanceOf(EntityManager::class, $config->getEntityManager());
90
    }
91
92
    public function testGetSetPermissionDefinition()
93
    {
94
        $config = $this->config;
95
        $this->assertNull($config->getPermissionDefinition());
96
        $config->setPermissionDefinition(new PermissionDefinition(['something']));
97
        $this->assertInstanceOf(PermissionDefinition::class, $config->getPermissionDefinition());
98
    }
99
100
    public function testGetQuery()
101
    {
102
        $config = $this->config;
103
        $em = $this->em;
104
        $filterBuilder = $this->createMock(FilterBuilder::class);
105
        $filterBuilder->expects($this->once())->method('getCurrentFilters')->willReturn([new Filter('whatever', ['type' => new StringFilterType('whatever')], uniqid())]);
106
        $config->setFilterBuilder($filterBuilder);
107
        $cacheConfig = $this->createMock(CacheConfiguration::class);
108
        $cacheConfig->expects($this->any())->method('getCacheLogger')->willReturn('whatever');
109
        $fakeConfig = $this->createMock(Configuration::class);
110
        $fakeConfig->expects($this->any())->method('getDefaultQueryHints')->willReturn($fakeConfig);
111
        $fakeConfig->expects($this->any())->method('isSecondLevelCacheEnabled')->willReturn($fakeConfig);
112
        $fakeConfig->expects($this->any())->method('getSecondLevelCacheConfiguration')->willReturn($cacheConfig);
113
        $qb = $this->createMock(QueryBuilder::class);
114
        $qb->expects($this->any())->method('setParameters')->willReturn($qb);
115
        $qb->expects($this->any())->method('setMaxResults')->willReturn($qb);
116
        $em->expects($this->any())->method('createQuery')->willReturn($qb);
117
        $em->expects($this->any())->method('getRepository')->willReturn($em);
118
        $em->expects($this->any())->method('createQueryBuilder')->willReturn($qb);
119
        $em->expects($this->any())->method('getConfiguration')->willReturn($fakeConfig);
120
        $query = new Query($em);
121
        $qb->expects($this->any())->method('getQuery')->willReturn($query);
122
        $query = $config->getQuery();
123
        $this->assertInstanceOf(Query::class, $query);
124
    }
125
126 View Code Duplication
    public function testEditUrlFor()
127
    {
128
        $config = $this->config;
129
        $item = new Popup();
130
        $item->setId(666);
131
        $url = $config->getEditUrlFor($item);
132
        $this->assertCount(2, $url);
133
        $this->assertArrayHasKey('path', $url);
134
        $this->assertArrayHasKey('params', $url);
135
        $this->assertArrayHasKey('id', $url['params']);
136
        $this->assertEquals('somebundle_admin_someentity_edit', $url['path']);
137
        $this->assertCount(1, $url['params']);
138
        $this->assertEquals(666, $url['params']['id']);
139
    }
140
141 View Code Duplication
    public function testDeleteUrlFor()
142
    {
143
        $config = $this->config;
144
        $item = new Popup();
145
        $item->setId(666);
146
        $url = $config->getDeleteUrlFor($item);
147
        $this->assertCount(2, $url);
148
        $this->assertArrayHasKey('path', $url);
149
        $this->assertArrayHasKey('params', $url);
150
        $this->assertArrayHasKey('id', $url['params']);
151
        $this->assertEquals('somebundle_admin_someentity_delete', $url['path']);
152
        $this->assertCount(1, $url['params']);
153
        $this->assertEquals(666, $url['params']['id']);
154
    }
155
156
    /**
157
     * @throws \ReflectionException
158
     */
159
    public function testGetPagerFanta()
160
    {
161
        $config = $this->config;
162
        $em = $this->em;
163
164
        $cacheConfig = $this->createMock(CacheConfiguration::class);
165
        $cacheConfig->expects($this->any())->method('getCacheLogger')->willReturn('whatever');
166
167
        $fakeConfig = $this->createMock(Configuration::class);
168
        $fakeConfig->expects($this->any())->method('getDefaultQueryHints')->willReturn(['doctrine_paginator.distinct' => 'blah']);
169
        $fakeConfig->expects($this->any())->method('isSecondLevelCacheEnabled')->willReturn($fakeConfig);
170
        $fakeConfig->expects($this->any())->method('getSecondLevelCacheConfiguration')->willReturn($cacheConfig);
171
172
        $platform = $this->createMock(MySQL57Platform::class);
173
        $platform->expects($this->any())->method('getSQLResultCasing')->willReturn('string');
174
175
        $connection = $this->createMock(Connection::class);
176
        $connection->expects($this->any())->method('getDatabasePlatform')->willReturn($platform);
177
178
        $qb = $this->createMock(QueryBuilder::class);
179
        $qb->expects($this->any())->method('setParameters')->willReturn($qb);
180
        $qb->expects($this->any())->method('setMaxResults')->willReturn($qb);
181
        $em->expects($this->any())->method('createQuery')->willReturn($qb);
182
        $em->expects($this->any())->method('getRepository')->willReturn($em);
183
        $em->expects($this->any())->method('createQueryBuilder')->willReturn($qb);
184
        $em->expects($this->any())->method('getConfiguration')->willReturn($fakeConfig);
185
        $em->expects($this->any())->method('getConnection')->willReturn($connection);
186
        $qb->expects($this->any())->method('getQuery')->willReturn(new Query($em));
187
188
        $pager = $config->getPagerfanta();
189
        $this->assertInstanceOf(Pagerfanta::class, $pager);
190
191
        $pager = $this->createMock(Pagerfanta::class);
192
        $pager->expects($this->once())->method('getNbResults')->willReturn(5);
193
        $pager->expects($this->once())->method('getCurrentPageResults')->willReturn([1, 2, 3, 4, 5]);
194
195
        $mirror = new ReflectionClass(AbstractDoctrineORMAdminListConfigurator::class);
196
        $property = $mirror->getProperty('pagerfanta');
197
        $property->setAccessible(true);
198
        $property->setValue($config, $pager);
199
200
        $count = $config->getCount();
201
        $this->assertEquals(5, $count);
202
203
        $items = $config->getItems();
204
        $this->assertCount(5, $items);
205
    }
206
207
    /**
208
     * @throws \ReflectionException
209
     */
210 View Code Duplication
    public function testGetIterator()
211
    {
212
        $config = $this->config;
213
        $query = $this->createMock(AbstractQuery::class);
214
        $query->expects($this->once())->method('iterate')->willReturn(new ArrayIterator());
215
216
        $mirror = new ReflectionClass(AbstractDoctrineORMAdminListConfigurator::class);
217
        $property = $mirror->getProperty('query');
218
        $property->setAccessible(true);
219
        $property->setValue($config, $query);
220
221
        $it = $config->getIterator();
222
        $this->assertInstanceOf(ArrayIterator::class, $it);
223
    }
224
225
    /**
226
     * @throws \ReflectionException
227
     */
228
    public function testGetQueryWithAclEnabled()
229
    {
230
        $config = $this->config;
231
        $em = $this->em;
232
233
        $cacheConfig = $this->createMock(CacheConfiguration::class);
234
        $cacheConfig->expects($this->any())->method('getCacheLogger')->willReturn('whatever');
235
        $fakeConfig = $this->createMock(Configuration::class);
236
        $fakeConfig->expects($this->any())->method('getDefaultQueryHints')->willReturn($fakeConfig);
237
        $fakeConfig->expects($this->any())->method('isSecondLevelCacheEnabled')->willReturn($fakeConfig);
238
        $fakeConfig->expects($this->any())->method('getSecondLevelCacheConfiguration')->willReturn($cacheConfig);
239
        $qb = $this->createMock(QueryBuilder::class);
240
        $qb->expects($this->any())->method('setParameters')->willReturn($qb);
241
        $qb->expects($this->any())->method('setMaxResults')->willReturn($qb);
242
        $em->expects($this->any())->method('createQuery')->willReturn($qb);
243
        $em->expects($this->any())->method('getRepository')->willReturn($em);
244
        $em->expects($this->any())->method('createQueryBuilder')->willReturn($qb);
245
        $em->expects($this->any())->method('getConfiguration')->willReturn($fakeConfig);
246
247
        $query = new Query($em);
248
        $aclHelper = $this->createMock(AclHelper::class);
249
        $aclHelper->expects($this->once())->method('apply')->willReturn($query);
250
        $mirror = new ReflectionClass(AbstractDoctrineORMAdminListConfigurator::class);
251
        $property = $mirror->getProperty('aclHelper');
252
        $property->setAccessible(true);
253
        $property->setValue($config, $aclHelper);
254
        $property = $mirror->getProperty('permissionDef');
255
        $property->setAccessible(true);
256
        $property->setValue($config, new PermissionDefinition(['admin']));
257
258
        $qb->expects($this->any())->method('getQuery')->willReturn($query);
259
        $query = $config->getQuery();
260
        $this->assertInstanceOf(Query::class, $query);
261
    }
262
}
263