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