1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Sonata Project package. |
7
|
|
|
* |
8
|
|
|
* (c) Thomas Rabaix <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Sonata\AdminBundle\Tests\Admin; |
15
|
|
|
|
16
|
|
|
use PHPUnit\Framework\TestCase; |
17
|
|
|
use Sonata\AdminBundle\Admin\FieldDescriptionCollection; |
18
|
|
|
use Sonata\AdminBundle\Admin\FieldDescriptionInterface; |
19
|
|
|
|
20
|
|
|
class FieldDescriptionCollectionTest extends TestCase |
21
|
|
|
{ |
22
|
|
|
public function testMethods(): void |
23
|
|
|
{ |
24
|
|
|
$collection = new FieldDescriptionCollection(); |
25
|
|
|
|
26
|
|
|
$fieldDescription = $this->createMock(FieldDescriptionInterface::class); |
27
|
|
|
$fieldDescription->expects($this->once())->method('getName')->will($this->returnValue('title')); |
28
|
|
|
$collection->add($fieldDescription); |
|
|
|
|
29
|
|
|
|
30
|
|
|
$fieldDescription = $this->createMock(FieldDescriptionInterface::class); |
31
|
|
|
$fieldDescription->expects($this->once())->method('getName')->will($this->returnValue('position')); |
32
|
|
|
$collection->add($fieldDescription); |
|
|
|
|
33
|
|
|
|
34
|
|
|
$this->assertFalse($collection->has('foo')); |
35
|
|
|
$this->assertFalse(isset($collection['foo'])); |
36
|
|
|
$this->assertTrue($collection->has('title')); |
37
|
|
|
$this->assertTrue(isset($collection['title'])); |
38
|
|
|
|
39
|
|
|
$this->assertCount(2, $collection->getElements()); |
|
|
|
|
40
|
|
|
$this->assertCount(2, $collection); |
41
|
|
|
|
42
|
|
|
$this->isInstanceOf(FieldDescriptionInterface::class, $collection['title']); |
|
|
|
|
43
|
|
|
$this->isInstanceOf(FieldDescriptionInterface::class, $collection->get('title')); |
|
|
|
|
44
|
|
|
|
45
|
|
|
$collection->remove('title'); |
46
|
|
|
$this->assertFalse($collection->has('title')); |
47
|
|
|
|
48
|
|
|
unset($collection['position']); |
49
|
|
|
|
50
|
|
|
$this->assertCount(0, $collection->getElements()); |
|
|
|
|
51
|
|
|
$this->assertCount(0, $collection); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function testNonExistentField(): void |
55
|
|
|
{ |
56
|
|
|
$this->expectException(\InvalidArgumentException::class); |
57
|
|
|
$this->expectExceptionMessage('Element "foo" does not exist.'); |
58
|
|
|
|
59
|
|
|
$collection = new FieldDescriptionCollection(); |
60
|
|
|
$collection->get('foo'); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function testArrayAccessSetField(): void |
64
|
|
|
{ |
65
|
|
|
$this->expectException(\RuntimeException::class); |
66
|
|
|
$this->expectExceptionMessage('Cannot set value, use add'); |
67
|
|
|
|
68
|
|
|
$collection = new FieldDescriptionCollection(); |
69
|
|
|
|
70
|
|
|
$collection['foo'] = null; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function testReorderListWithoutBatchField(): void |
74
|
|
|
{ |
75
|
|
|
$collection = new FieldDescriptionCollection(); |
76
|
|
|
|
77
|
|
|
$fieldDescription = $this->createMock(FieldDescriptionInterface::class); |
78
|
|
|
$fieldDescription->expects($this->once())->method('getName')->will($this->returnValue('title')); |
79
|
|
|
$collection->add($fieldDescription); |
|
|
|
|
80
|
|
|
|
81
|
|
|
$fieldDescription = $this->createMock(FieldDescriptionInterface::class); |
82
|
|
|
$fieldDescription->expects($this->once())->method('getName')->will($this->returnValue('position')); |
83
|
|
|
$collection->add($fieldDescription); |
|
|
|
|
84
|
|
|
|
85
|
|
|
$newOrder = ['position', 'title']; |
86
|
|
|
$collection->reorder($newOrder); |
87
|
|
|
|
88
|
|
|
$actualElements = array_keys($collection->getElements()); |
89
|
|
|
$this->assertSame($newOrder, $actualElements, 'the order is wrong'); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function testReorderListWithBatchField(): void |
93
|
|
|
{ |
94
|
|
|
$collection = new FieldDescriptionCollection(); |
95
|
|
|
|
96
|
|
|
$fieldDescription = $this->createMock(FieldDescriptionInterface::class); |
97
|
|
|
$fieldDescription->expects($this->once())->method('getName')->will($this->returnValue('title')); |
98
|
|
|
$collection->add($fieldDescription); |
|
|
|
|
99
|
|
|
|
100
|
|
|
$fieldDescription = $this->createMock(FieldDescriptionInterface::class); |
101
|
|
|
$fieldDescription->expects($this->once())->method('getName')->will($this->returnValue('position')); |
102
|
|
|
$collection->add($fieldDescription); |
|
|
|
|
103
|
|
|
|
104
|
|
|
$fieldDescription = $this->createMock(FieldDescriptionInterface::class); |
105
|
|
|
$fieldDescription->expects($this->once())->method('getName')->will($this->returnValue('batch')); |
106
|
|
|
$collection->add($fieldDescription); |
|
|
|
|
107
|
|
|
|
108
|
|
|
$newOrder = ['position', 'title']; |
109
|
|
|
$collection->reorder($newOrder); |
110
|
|
|
array_unshift($newOrder, 'batch'); |
111
|
|
|
|
112
|
|
|
$actualElements = array_keys($collection->getElements()); |
113
|
|
|
$this->assertSame($newOrder, $actualElements, 'the order is wrong'); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: