Completed
Pull Request — master (#2101)
by Kevin
18:19
created

AbstractDoctrineDBALAdminListConfiguratorTest.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\QueryBuilder;
12
use Kunstmaan\AdminListBundle\AdminList\Configurator\AbstractDoctrineDBALAdminListConfigurator;
13
use Kunstmaan\AdminListBundle\AdminList\Filter;
14
use Kunstmaan\AdminListBundle\AdminList\FilterBuilder;
15
use Kunstmaan\AdminListBundle\AdminList\FilterType\DBAL\StringFilterType;
16
use Kunstmaan\AdminListBundle\AdminList\SortableInterface;
17
use Pagerfanta\Pagerfanta;
18
use PHPUnit_Framework_TestCase;
19
use ReflectionClass;
20
21
class DBAL extends AbstractDoctrineDBALAdminListConfigurator implements SortableInterface
22
{
23
    /**
24
     * @return mixed
25
     */
26
    public function getBundleName()
27
    {
28
        return 'SomeBundle';
29
    }
30
31
    /**
32
     * @return mixed
33
     */
34
    public function getEntityName()
35
    {
36
        return 'SomeEntity';
37
    }
38
39
    /**
40
     * @return mixed
41
     */
42
    public function buildFields()
43
    {
44
        return true;
45
    }
46
47
    /**
48
     * @return string
49
     */
50
    public function getSortableField()
51
    {
52
        return 'sortfield';
53
    }
54
55
56
}
57
58
class AbstractDoctrineDBALAdminListConfiguratorTest 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...
59
{
60
    /** @var DBAL $config */
61
    private $config;
62
63
    /** @var \PHPUnit_Framework_MockObject_MockObject $connection */
64
    private $connection;
65
66
    /**
67
     * @throws \ReflectionException
68
     */
69
    public function setUp()
70
    {
71
        $connection = $this->createMock(Connection::class);
72
        $this->connection = $connection;
73
        $this->config = new DBAL($connection);
74
75
        $mirror = new ReflectionClass(AbstractDoctrineDBALAdminListConfigurator::class);
76
        $property = $mirror->getProperty('orderBy');
77
        $property->setAccessible(true);
78
        $property->setValue($this->config, 'somefield');
79
        $this->config->setUseDistinctCount(true);
80
81
        $filterBuilder = $this->createMock(FilterBuilder::class);
82
        $filterBuilder->expects($this->any())->method('getCurrentFilters')->willReturn([new Filter('whatever', ['type' => new StringFilterType('whatever')], uniqid())]);
83
        $this->config->setFilterBuilder($filterBuilder);
84
    }
85
86 View Code Duplication
    public function testEditUrlFor()
87
    {
88
        $config = $this->config;
89
        $item = ['id' => 666];
90
        $url = $config->getEditUrlFor($item);
91
        $this->assertCount(2, $url);
92
        $this->assertArrayHasKey('path', $url);
93
        $this->assertArrayHasKey('params', $url);
94
        $this->assertArrayHasKey('id', $url['params']);
95
        $this->assertEquals('somebundle_admin_someentity_edit', $url['path']);
96
        $this->assertCount(1, $url['params']);
97
        $this->assertEquals(666, $url['params']['id']);
98
    }
99
100 View Code Duplication
    public function testDeleteUrlFor()
101
    {
102
        $config = $this->config;
103
        $item = ['id' => 666];
104
        $url = $config->getDeleteUrlFor($item);
105
        $this->assertCount(2, $url);
106
        $this->assertArrayHasKey('path', $url);
107
        $this->assertArrayHasKey('params', $url);
108
        $this->assertArrayHasKey('id', $url['params']);
109
        $this->assertEquals('somebundle_admin_someentity_delete', $url['path']);
110
        $this->assertCount(1, $url['params']);
111
        $this->assertEquals(666, $url['params']['id']);
112
    }
113
114
    public function testGetCountField()
115
    {
116
        $config = $this->config;
117
        $config->setCountField('table.column');
118
        $this->assertEquals('table.column', $config->getCountField());
119
    }
120
121
    /**
122
     * @throws \ReflectionException
123
     */
124
    public function testGetPagerFanta()
125
    {
126
        $config = $this->config;
127
128
        $cacheConfig = $this->createMock(CacheConfiguration::class);
129
        $cacheConfig->expects($this->any())->method('getCacheLogger')->willReturn('whatever');
130
131
        $fakeConfig = $this->createMock(Configuration::class);
132
        $fakeConfig->expects($this->any())->method('getDefaultQueryHints')->willReturn(['doctrine_paginator.distinct' => 'blah']);
133
        $fakeConfig->expects($this->any())->method('isSecondLevelCacheEnabled')->willReturn($fakeConfig);
134
        $fakeConfig->expects($this->any())->method('getSecondLevelCacheConfiguration')->willReturn($cacheConfig);
135
136
        $platform = $this->createMock(MySQL57Platform::class);
137
        $platform->expects($this->any())->method('getSQLResultCasing')->willReturn('string');
138
139
        $connection = $this->createMock(Connection::class);
140
        $connection->expects($this->any())->method('getDatabasePlatform')->willReturn($platform);
141
142
        $qb = $this->createMock(QueryBuilder::class);
143
        $qb->expects($this->any())->method('setParameters')->willReturn($qb);
144
        $qb->expects($this->any())->method('setMaxResults')->willReturn($qb);
145
        $connection->expects($this->any())->method('createQueryBuilder')->willReturn($qb);
146
        $connection->expects($this->any())->method('getConfiguration')->willReturn($fakeConfig);
147
148
        $pager = $config->getPagerfanta();
149
        $this->assertInstanceOf(Pagerfanta::class, $pager);
150
151
        $pager = $this->createMock(Pagerfanta::class);
152
        $pager->expects($this->once())->method('getNbResults')->willReturn(5);
153
        $pager->expects($this->once())->method('getCurrentPageResults')->willReturn([1,2,3,4,5]);
154
155
        $mirror = new ReflectionClass(AbstractDoctrineDBALAdminListConfigurator::class);
156
        $property = $mirror->getProperty('pagerfanta');
157
        $property->setAccessible(true);
158
        $property->setValue($config, $pager);
159
160
        $count = $config->getCount();
161
        $this->assertEquals(5, $count);
162
163
        $items = $config->getItems();
164
        $this->assertCount(5, $items);
165
    }
166
167
    /**
168
     * @throws \ReflectionException
169
     */
170 View Code Duplication
    public function testGetIterator()
171
    {
172
        $config = $this->config;
173
        $queryBuilder = $this->createMock(AbstractQuery::class);
174
        $queryBuilder->expects($this->once())->method('execute')->willReturn(new ArrayIterator());
175
176
        $mirror = new ReflectionClass(AbstractDoctrineDBALAdminListConfigurator::class);
177
        $property = $mirror->getProperty('queryBuilder');
178
        $property->setAccessible(true);
179
        $property->setValue($config, $queryBuilder);
180
181
        $it = $config->getIterator();
182
        $this->assertInstanceOf(ArrayIterator::class, $it);
183
    }
184
}