Completed
Push — master ( 1af7e7...796d56 )
by Jeroen
16s
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
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...
57
{
58
    /** @var DBAL $config */
59
    private $config;
60
61
    /** @var \PHPUnit_Framework_MockObject_MockObject $connection */
62
    private $connection;
63
64
    /**
65
     * @throws \ReflectionException
66
     */
67
    public function setUp()
68
    {
69
        $connection = $this->createMock(Connection::class);
70
        $this->connection = $connection;
71
        $this->config = new DBAL($connection);
72
73
        $mirror = new ReflectionClass(AbstractDoctrineDBALAdminListConfigurator::class);
74
        $property = $mirror->getProperty('orderBy');
75
        $property->setAccessible(true);
76
        $property->setValue($this->config, 'somefield');
77
        $this->config->setUseDistinctCount(true);
78
79
        $filterBuilder = $this->createMock(FilterBuilder::class);
80
        $filterBuilder->expects($this->any())->method('getCurrentFilters')->willReturn([new Filter('whatever', ['type' => new StringFilterType('whatever')], uniqid())]);
81
        $this->config->setFilterBuilder($filterBuilder);
82
    }
83
84 View Code Duplication
    public function testEditUrlFor()
85
    {
86
        $config = $this->config;
87
        $item = ['id' => 666];
88
        $url = $config->getEditUrlFor($item);
89
        $this->assertCount(2, $url);
90
        $this->assertArrayHasKey('path', $url);
91
        $this->assertArrayHasKey('params', $url);
92
        $this->assertArrayHasKey('id', $url['params']);
93
        $this->assertEquals('somebundle_admin_someentity_edit', $url['path']);
94
        $this->assertCount(1, $url['params']);
95
        $this->assertEquals(666, $url['params']['id']);
96
    }
97
98 View Code Duplication
    public function testDeleteUrlFor()
99
    {
100
        $config = $this->config;
101
        $item = ['id' => 666];
102
        $url = $config->getDeleteUrlFor($item);
103
        $this->assertCount(2, $url);
104
        $this->assertArrayHasKey('path', $url);
105
        $this->assertArrayHasKey('params', $url);
106
        $this->assertArrayHasKey('id', $url['params']);
107
        $this->assertEquals('somebundle_admin_someentity_delete', $url['path']);
108
        $this->assertCount(1, $url['params']);
109
        $this->assertEquals(666, $url['params']['id']);
110
    }
111
112
    public function testGetCountField()
113
    {
114
        $config = $this->config;
115
        $config->setCountField('table.column');
116
        $this->assertEquals('table.column', $config->getCountField());
117
    }
118
119
    /**
120
     * @throws \ReflectionException
121
     */
122
    public function testGetPagerFanta()
123
    {
124
        $config = $this->config;
125
126
        $cacheConfig = $this->createMock(CacheConfiguration::class);
127
        $cacheConfig->expects($this->any())->method('getCacheLogger')->willReturn('whatever');
128
129
        $fakeConfig = $this->createMock(Configuration::class);
130
        $fakeConfig->expects($this->any())->method('getDefaultQueryHints')->willReturn(['doctrine_paginator.distinct' => 'blah']);
131
        $fakeConfig->expects($this->any())->method('isSecondLevelCacheEnabled')->willReturn($fakeConfig);
132
        $fakeConfig->expects($this->any())->method('getSecondLevelCacheConfiguration')->willReturn($cacheConfig);
133
134
        $platform = $this->createMock(MySQL57Platform::class);
135
        $platform->expects($this->any())->method('getSQLResultCasing')->willReturn('string');
136
137
        $connection = $this->createMock(Connection::class);
138
        $connection->expects($this->any())->method('getDatabasePlatform')->willReturn($platform);
139
140
        $qb = $this->createMock(QueryBuilder::class);
141
        $qb->expects($this->any())->method('setParameters')->willReturn($qb);
142
        $qb->expects($this->any())->method('setMaxResults')->willReturn($qb);
143
        $connection->expects($this->any())->method('createQueryBuilder')->willReturn($qb);
144
        $connection->expects($this->any())->method('getConfiguration')->willReturn($fakeConfig);
145
146
        $pager = $config->getPagerfanta();
147
        $this->assertInstanceOf(Pagerfanta::class, $pager);
148
149
        $pager = $this->createMock(Pagerfanta::class);
150
        $pager->expects($this->once())->method('getNbResults')->willReturn(5);
151
        $pager->expects($this->once())->method('getCurrentPageResults')->willReturn([1, 2, 3, 4, 5]);
152
153
        $mirror = new ReflectionClass(AbstractDoctrineDBALAdminListConfigurator::class);
154
        $property = $mirror->getProperty('pagerfanta');
155
        $property->setAccessible(true);
156
        $property->setValue($config, $pager);
157
158
        $count = $config->getCount();
159
        $this->assertEquals(5, $count);
160
161
        $items = $config->getItems();
162
        $this->assertCount(5, $items);
163
    }
164
165
    /**
166
     * @throws \ReflectionException
167
     */
168 View Code Duplication
    public function testGetIterator()
169
    {
170
        $config = $this->config;
171
        $queryBuilder = $this->createMock(AbstractQuery::class);
172
        $queryBuilder->expects($this->once())->method('execute')->willReturn(new ArrayIterator());
173
174
        $mirror = new ReflectionClass(AbstractDoctrineDBALAdminListConfigurator::class);
175
        $property = $mirror->getProperty('queryBuilder');
176
        $property->setAccessible(true);
177
        $property->setValue($config, $queryBuilder);
178
179
        $it = $config->getIterator();
180
        $this->assertInstanceOf(ArrayIterator::class, $it);
181
    }
182
}
183