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

Tests/unit/AdminList/AdminListTest.php (2 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;
4
5
use ArrayIterator;
6
use DateTime;
7
use Doctrine\Common\Collections\ArrayCollection;
8
use Doctrine\ORM\EntityManager;
9
use Doctrine\ORM\Mapping\ClassMetadata;
10
use Doctrine\ORM\PersistentCollection;
11
use Kunstmaan\AdminListBundle\AdminList\AdminList;
12
use Kunstmaan\AdminListBundle\AdminList\FilterBuilder;
13
use Kunstmaan\AdminListBundle\Tests\unit\Model\ConcreteConfigurator;
14
use Kunstmaan\MenuBundle\Entity\MenuItem;
15
use PHPUnit_Framework_TestCase;
16
use stdClass;
17
use Symfony\Component\HttpFoundation\Request;
18
use Symfony\Component\HttpFoundation\Session\Session;
19
20
class PrivateObject
21
{
22
    protected $name = 'delboy1978uk';
23
}
24
25
class PublicObject extends PrivateObject
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...
26
{
27
    /**
28
     * @return string
29
     */
30
    public function getName()
31
    {
32
        return $this->name;
33
    }
34
}
35
36
/**
37
 * Class AdminListTest
38
 */
39
class AdminListTest 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...
40
{
41
    public function testStuff()
42
    {
43
        $item = new MenuItem();
44
        $item->setId(666);
45
46
        $config = new ConcreteConfigurator();
47
        $adminList = new AdminList($config);
48
49
        $this->assertInstanceOf(ConcreteConfigurator::class, $adminList->getConfigurator());
50
        $this->assertInstanceOf(FilterBuilder::class, $adminList->getFilterBuilder());
51
        $this->assertCount(0, $adminList->getColumns());
52
        $this->assertCount(0, $adminList->getExportColumns());
53
        $this->assertEquals(0, $adminList->getCount());
54
        $this->assertEquals('fake pagerfanta', $adminList->getPagerfanta());
55
        $this->assertInstanceOf(ArrayIterator::class, $adminList->getIterator());
56
        $this->assertTrue($adminList->canAdd());
57
        $this->assertTrue($adminList->canEdit($item));
58
        $this->assertTrue($adminList->canDelete($item));
59
        $this->assertFalse($adminList->canExport());
60
        $this->assertFalse($adminList->canView($item));
61
        $this->assertFalse($adminList->hasSort());
62
        $this->assertFalse($adminList->hasSort('non-exitant-var-name'));
63
64
        $url = $adminList->getIndexUrl();
65
        $this->assertArrayHasKey('path', $url);
66
        $this->assertArrayHasKey('params', $url);
67
        $this->assertEquals('xyz_admin_xyz', $url['path']);
68
        $this->assertEmpty($url['params']);
69
70
        $url = $adminList->getExportUrl();
71
        $this->assertArrayHasKey('path', $url);
72
        $this->assertArrayHasKey('params', $url);
73
        $this->assertArrayHasKey('_format', $url['params']);
74
        $this->assertCount(1, $url['params']);
75
        $this->assertEquals('xyz_admin_xyz_export', $url['path']);
76
        $this->assertEquals('csv', $url['params']['_format']);
77
78
        $url = $adminList->getAddUrlFor([]);
79
        $this->assertArrayHasKey('Xyz', $url);
80
        $this->assertCount(2, $url['Xyz']);
81
        $this->assertEquals('xyz_admin_xyz_add', $url['Xyz']['path']);
82
        $this->assertCount(0, $url['Xyz']['params']);
83
84
        $url = $adminList->getEditUrlFor($item);
85
        $this->assertArrayHasKey('Xyz', $url);
86
        $this->assertCount(2, $url['Xyz']);
87
        $this->assertEquals('xyz_admin_xyz_edit', $url['Xyz']['path']);
88
        $this->assertCount(0, $url['Xyz']['params']);
89
90
        $url = $adminList->getDeleteUrlFor($item);
91
        $this->assertArrayHasKey('Xyz', $url);
92
        $this->assertCount(2, $url['Xyz']);
93
        $this->assertEquals('xyz_admin_xyz_delete', $url['Xyz']['path']);
94
        $this->assertCount(0, $url['Xyz']['params']);
95
96
        $url = $adminList->getViewUrlFor($item);
97
        $this->assertCount(2, $url);
98
        $this->assertArrayHasKey('path', $url);
99
        $this->assertArrayHasKey('params', $url);
100
        $this->assertEquals('xyz_admin_xyz_view', $url['path']);
101
        $this->assertCount(1, $url['params']);
102
        $this->assertArrayHasKey('id', $url['params']);
103
        $this->assertEquals(666, $url['params']['id']);
104
105
        $items = $adminList->getItems();
106
        $this->assertArrayHasKey('some', $items);
107
108
        $hasBulkActions = $adminList->hasBulkActions();
109
        $bulkActions = $adminList->getBulkActions();
110
        $this->assertFalse($hasBulkActions);
111
        $this->assertEmpty($bulkActions);
112
113
        $hasListActions = $adminList->hasListActions();
114
        $listActions = $adminList->getListActions();
115
        $this->assertFalse($hasListActions);
116
        $this->assertEmpty($listActions);
117
118
        $hasItemActions = $adminList->hasItemActions();
119
        $itemActions = $adminList->getItemActions();
120
        $this->assertFalse($hasItemActions);
121
        $this->assertEmpty($itemActions);
122
    }
123
124
    public function testGetValue()
125
    {
126
        $config = new ConcreteConfigurator();
127
        $adminList = new AdminList($config);
128
129
        $em = $this->createMock(EntityManager::class);
130
        $meta = $this->createMock(ClassMetadata::class);
131
132
        $collection = new PersistentCollection($em, $meta, new ArrayCollection([
133
            new PublicObject(),
134
        ]));
135
136
        $object = new stdClass();
137
        $object->name = 'delboy1978uk';
138
        $value = $adminList->getValue($object, 'name');
139
        $this->assertEquals('delboy1978uk', $value);
140
141
        $array = [
142
            'name' => 'delboy1978uk',
143
        ];
144
        $value = $adminList->getValue($array, 'name');
145
        $this->assertEquals('delboy1978uk', $value);
146
        $this->assertEquals('', $adminList->getValue($array, 'missing'));
147
148
        $private = new PrivateObject();
149
        $this->assertEquals('undefined function [get/is/has]name()', $adminList->getValue($private, 'name'));
150
151
        $date = new DateTime('2014-09-18 22:00:00');
152
        $array = [
153
            'key' => $date,
154
            'array' => [
155
                'random',
156
                'strings',
157
            ],
158
            'bool' => true,
159
            'string' => 'strings!',
160
            'persistent' => $collection,
161
        ];
162
        $value = $adminList->getStringValue($array, 'key');
163
        $this->assertEquals('2014-09-18 22:00:00', $value);
164
        $this->assertEquals('random, strings', $adminList->getStringValue($array, 'array'));
165
        $this->assertEquals('true', $adminList->getStringValue($array, 'bool'));
166
        $this->assertEquals('strings!', $adminList->getStringValue($array, 'string'));
167
        $this->assertEquals('delboy1978uk', $adminList->getStringValue($array, 'persistent'));
168
169
        $collection = new PersistentCollection($em, $meta, new ArrayCollection([]));
170
171
        $array = [
172
            'persistent' => $collection,
173
        ];
174
        $this->assertEquals('', $adminList->getStringValue($array, 'persistent'));
175
    }
176
177
    public function testBindRequest()
178
    {
179
        $session = $this->createMock(Session::class);
180
        $session->expects($this->any())->method('has')->willReturn(true);
181
        $session->expects($this->any())->method('get')->willReturn([
182
            'page' => 1,
183
            'orderBy' => 'id',
184
            'orderDirection' => 'ASC',
185
        ]);
186
        $request = new Request();
187
        $request->setSession($session);
188
        $request->query->add([
189
            '_route' => 'some-route',
190
        ]);
191
        $config = new ConcreteConfigurator();
192
        $adminList = new AdminList($config);
193
        $adminList->bindRequest($request);
194
195
        $this->assertEquals('id', $adminList->getOrderBy());
196
        $this->assertEquals('ASC', $adminList->getOrderDirection());
197
        $this->assertEquals(1, $adminList->getConfigurator()->getPage());
198
    }
199
}
200