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