1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Psi\Bridge\ContentType\Tests\Unit\Doctrine\PhpcrOdm; |
4
|
|
|
|
5
|
|
|
use Doctrine\ODM\PHPCR\DocumentManagerInterface; |
6
|
|
|
use Doctrine\ODM\PHPCR\Mapping\ClassMetadata as OdmMetadata; |
7
|
|
|
use Doctrine\ODM\PHPCR\Mapping\ClassMetadataFactory; |
8
|
|
|
use Metadata\MetadataFactoryInterface; |
9
|
|
|
use Psi\Bridge\ContentType\Doctrine\PhpcrOdm\CollectionIdentifierUpdater; |
10
|
|
|
use Psi\Bridge\ContentType\Doctrine\PhpcrOdm\PropertyEncoder; |
11
|
|
|
use Psi\Component\ContentType\Metadata\ClassMetadata; |
12
|
|
|
|
13
|
|
|
class CollectionIdentifierUpdaterTest extends \PHPUnit_Framework_TestCase |
14
|
|
|
{ |
15
|
|
|
public function setUp() |
16
|
|
|
{ |
17
|
|
|
$this->metadataFactory = $this->prophesize(MetadataFactoryInterface::class); |
|
|
|
|
18
|
|
|
$this->encoder = $this->prophesize(PropertyEncoder::class); |
|
|
|
|
19
|
|
|
|
20
|
|
|
$this->updater = new CollectionIdentifierUpdater( |
|
|
|
|
21
|
|
|
$this->metadataFactory->reveal(), |
22
|
|
|
$this->encoder->reveal() |
23
|
|
|
); |
24
|
|
|
|
25
|
|
|
$this->documentManager = $this->prophesize(DocumentManagerInterface::class); |
|
|
|
|
26
|
|
|
$this->document = new \stdClass(); |
|
|
|
|
27
|
|
|
$this->odmMetadataFactory = $this->prophesize(ClassMetadataFactory::class); |
|
|
|
|
28
|
|
|
$this->odmMetadata = $this->prophesize(OdmMetadata::class); |
|
|
|
|
29
|
|
|
$this->ctMetadata = $this->prophesize(ClassMetadata::class); |
|
|
|
|
30
|
|
|
|
31
|
|
|
$this->documentManager->getMetadataFactory()->willReturn( |
32
|
|
|
$this->odmMetadataFactory->reveal() |
|
|
|
|
33
|
|
|
); |
34
|
|
|
|
35
|
|
|
$this->document = new \stdClass(); |
|
|
|
|
36
|
|
|
$this->child1 = new \stdClass(); |
|
|
|
|
37
|
|
|
$this->child2 = new \stdClass(); |
|
|
|
|
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* It should return early if there is CT system has no metadata. |
42
|
|
|
*/ |
43
|
|
|
public function testReturnEarlyNoCtMetadata() |
44
|
|
|
{ |
45
|
|
|
$this->odmMetadataFactory->getMetadataFor(\stdClass::class)->willReturn( |
|
|
|
|
46
|
|
|
$this->odmMetadata->reveal() |
47
|
|
|
); |
48
|
|
|
$this->metadataFactory->getMetadataForClass(\stdClass::class)->willReturn(null); |
49
|
|
|
$this->updater->update($this->documentManager->reveal(), $this->document); |
|
|
|
|
50
|
|
|
$this->odmMetadata->getIdentifierValue($this->document)->shouldNotHaveBeenCalled(); |
|
|
|
|
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* It should ignore children ODM mappings that have no corresponding CT mapping. |
55
|
|
|
*/ |
56
|
|
|
public function testIgnoreNonCtMappings() |
57
|
|
|
{ |
58
|
|
|
$identifier = '/path/to/document'; |
59
|
|
|
$this->odmMetadataFactory->getMetadataFor(\stdClass::class)->willReturn( |
|
|
|
|
60
|
|
|
$this->odmMetadata->reveal() |
61
|
|
|
); |
62
|
|
|
$this->metadataFactory->getMetadataForClass(\stdClass::class)->willReturn($this->ctMetadata->reveal()); |
63
|
|
|
|
64
|
|
|
$this->odmMetadata->getIdentifierValue($this->document)->willReturn($identifier); |
|
|
|
|
65
|
|
|
$this->odmMetadata->childrenMappings = [ |
66
|
|
|
'some_collection', |
67
|
|
|
]; |
68
|
|
|
|
69
|
|
|
$this->odmMetadata->getFieldValue($this->document, 'some_collection')->shouldNotBeCalled(); |
|
|
|
|
70
|
|
|
$this->ctMetadata->propertyMetadata = []; |
71
|
|
|
|
72
|
|
|
$this->updater->update($this->documentManager->reveal(), $this->document); |
|
|
|
|
73
|
|
|
$this->odmMetadata->getFieldValue($this->document, 'some_collection')->shouldNotHaveBeenCalled(); |
|
|
|
|
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* It should set the identifier value of collection children documents. |
78
|
|
|
*/ |
79
|
|
|
public function testSetIdentifierValue() |
80
|
|
|
{ |
81
|
|
|
$identifier = '/path/to/document'; |
82
|
|
|
$this->odmMetadataFactory->getMetadataFor(\stdClass::class)->willReturn( |
|
|
|
|
83
|
|
|
$this->odmMetadata->reveal() |
84
|
|
|
); |
85
|
|
|
$this->metadataFactory->getMetadataForClass(\stdClass::class)->willReturn($this->ctMetadata->reveal()); |
86
|
|
|
|
87
|
|
|
$this->odmMetadata->getIdentifierValue($this->document)->willReturn($identifier); |
|
|
|
|
88
|
|
|
$this->odmMetadata->childrenMappings = [ |
89
|
|
|
'some_collection', |
90
|
|
|
]; |
91
|
|
|
$this->ctMetadata->propertyMetadata = [ |
92
|
|
|
'some_collection' => 'something', |
93
|
|
|
]; |
94
|
|
|
$this->odmMetadata->getFieldValue($this->document, 'some_collection')->willReturn([ |
|
|
|
|
95
|
|
|
$this->child1, |
96
|
|
|
$this->child2, |
97
|
|
|
]); |
98
|
|
|
|
99
|
|
|
$this->encoder->encode('some_collection', 0)->willReturn('some_collection-0'); |
100
|
|
|
$this->encoder->encode('some_collection', 1)->willReturn('some_collection-1'); |
101
|
|
|
$this->odmMetadata->setIdentifierValue($this->child1, $identifier . '/some_collection-0')->shouldBeCalled(); |
102
|
|
|
$this->odmMetadata->setIdentifierValue($this->child2, $identifier . '/some_collection-1')->shouldBeCalled(); |
103
|
|
|
|
104
|
|
|
$this->updater->update($this->documentManager->reveal(), $this->document); |
|
|
|
|
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: