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\Manipulator; |
15
|
|
|
|
16
|
|
|
use PHPUnit\Framework\TestCase; |
17
|
|
|
use Sonata\AdminBundle\Admin\FieldDescriptionInterface; |
18
|
|
|
use Sonata\AdminBundle\Manipulator\ObjectManipulator; |
19
|
|
|
|
20
|
|
|
class ObjectManipulatorTest extends TestCase |
21
|
|
|
{ |
22
|
|
|
public function testAddInstance(): void |
23
|
|
|
{ |
24
|
|
|
$fieldDescription = $this->createMock(FieldDescriptionInterface::class); |
25
|
|
|
$fieldDescription->expects($this->once())->method('getAssociationMapping')->willReturn(['fieldName' => 'fooBar']); |
26
|
|
|
$fieldDescription->expects($this->once())->method('getParentAssociationMappings')->willReturn([]); |
27
|
|
|
|
28
|
|
|
$instance = new \stdClass(); |
29
|
|
|
$object = $this->getMockBuilder(\stdClass::class)->setMethods(['addFooBar'])->getMock(); |
30
|
|
|
$object->expects($this->once())->method('addFooBar')->with($instance); |
31
|
|
|
|
32
|
|
|
ObjectManipulator::addInstance($object, $instance, $fieldDescription); |
|
|
|
|
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function testAddInstanceWithParentAssociation(): void |
36
|
|
|
{ |
37
|
|
|
$fieldDescription = $this->createMock(FieldDescriptionInterface::class); |
38
|
|
|
$fieldDescription->expects($this->once())->method('getAssociationMapping')->willReturn(['fieldName' => 'fooBar']); |
39
|
|
|
$fieldDescription->expects($this->once())->method('getParentAssociationMappings')->willReturn([['fieldName' => 'parent']]); |
40
|
|
|
|
41
|
|
|
$instance = new \stdClass(); |
42
|
|
|
|
43
|
|
|
$object2 = $this->getMockBuilder(\stdClass::class)->setMethods(['addFooBar'])->getMock(); |
44
|
|
|
$object2->expects($this->once())->method('addFooBar')->with($instance); |
45
|
|
|
|
46
|
|
|
$object1 = $this->getMockBuilder(\stdClass::class)->setMethods(['getParent'])->getMock(); |
47
|
|
|
$object1->expects($this->once())->method('getParent')->willReturn($object2); |
48
|
|
|
|
49
|
|
|
ObjectManipulator::addInstance($object1, $instance, $fieldDescription); |
|
|
|
|
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function testAddInstancePlural(): void |
53
|
|
|
{ |
54
|
|
|
$fieldDescription = $this->createMock(FieldDescriptionInterface::class); |
55
|
|
|
$fieldDescription->expects($this->once())->method('getAssociationMapping')->willReturn(['fieldName' => 'fooBars']); |
56
|
|
|
$fieldDescription->expects($this->once())->method('getParentAssociationMappings')->willReturn([]); |
57
|
|
|
|
58
|
|
|
$instance = new \stdClass(); |
59
|
|
|
$object = $this->getMockBuilder(\stdClass::class)->setMethods(['addFooBar'])->getMock(); |
60
|
|
|
$object->expects($this->once())->method('addFooBar')->with($instance); |
61
|
|
|
|
62
|
|
|
ObjectManipulator::addInstance($object, $instance, $fieldDescription); |
|
|
|
|
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function testAddInstanceInflector(): void |
66
|
|
|
{ |
67
|
|
|
$fieldDescription = $this->createMock(FieldDescriptionInterface::class); |
68
|
|
|
$fieldDescription->expects($this->once())->method('getAssociationMapping')->willReturn(['fieldName' => 'entries']); |
69
|
|
|
$fieldDescription->expects($this->once())->method('getParentAssociationMappings')->willReturn([]); |
70
|
|
|
|
71
|
|
|
$instance = new \stdClass(); |
72
|
|
|
$object = $this->getMockBuilder(\stdClass::class)->setMethods(['addEntry'])->getMock(); |
73
|
|
|
$object->expects($this->once())->method('addEntry')->with($instance); |
74
|
|
|
|
75
|
|
|
ObjectManipulator::addInstance($object, $instance, $fieldDescription); |
|
|
|
|
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function testSetObject(): void |
79
|
|
|
{ |
80
|
|
|
$fieldDescription = $this->createMock(FieldDescriptionInterface::class); |
81
|
|
|
$fieldDescription->expects($this->once())->method('getAssociationMapping')->willReturn(['mappedBy' => 'parent']); |
82
|
|
|
$fieldDescription->expects($this->once())->method('getParentAssociationMappings')->willReturn([]); |
83
|
|
|
|
84
|
|
|
$object = new \stdClass(); |
85
|
|
|
$instance = $this->getMockBuilder(\stdClass::class)->setMethods(['setParent'])->getMock(); |
86
|
|
|
$instance->expects($this->once())->method('setParent')->with($object); |
87
|
|
|
|
88
|
|
|
ObjectManipulator::setObject($instance, $object, $fieldDescription); |
|
|
|
|
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function testSetObjectWithoutMappedBy(): void |
92
|
|
|
{ |
93
|
|
|
$fieldDescription = $this->createMock(FieldDescriptionInterface::class); |
94
|
|
|
$fieldDescription->expects($this->once())->method('getAssociationMapping')->willReturn(['mappedBy' => null]); |
95
|
|
|
|
96
|
|
|
ObjectManipulator::setObject(new \stdClass(), new \stdClass(), $fieldDescription); |
|
|
|
|
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function testSetObjectWithParentAssociation(): void |
100
|
|
|
{ |
101
|
|
|
$fieldDescription = $this->createMock(FieldDescriptionInterface::class); |
102
|
|
|
$fieldDescription->expects($this->once())->method('getAssociationMapping')->willReturn(['mappedBy' => 'fooBar']); |
103
|
|
|
$fieldDescription->expects($this->once())->method('getParentAssociationMappings')->willReturn([['fieldName' => 'parent']]); |
104
|
|
|
|
105
|
|
|
$object2 = new \stdClass(); |
106
|
|
|
|
107
|
|
|
$instance = $this->getMockBuilder(\stdClass::class)->setMethods(['setFooBar'])->getMock(); |
108
|
|
|
$instance->expects($this->once())->method('setFooBar')->with($object2); |
109
|
|
|
|
110
|
|
|
$object1 = $this->getMockBuilder(\stdClass::class)->setMethods(['getParent'])->getMock(); |
111
|
|
|
$object1->expects($this->once())->method('getParent')->willReturn($object2); |
112
|
|
|
|
113
|
|
|
ObjectManipulator::setObject($instance, $object1, $fieldDescription); |
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: