|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Sylius package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Paweł Jędrzejewski |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
declare(strict_types=1); |
|
13
|
|
|
|
|
14
|
|
|
namespace spec\Sylius\Bundle\ReviewBundle\Doctrine\ORM\Subscriber; |
|
15
|
|
|
|
|
16
|
|
|
use Doctrine\Common\EventSubscriber; |
|
17
|
|
|
use Doctrine\ORM\EntityManager; |
|
18
|
|
|
use Doctrine\ORM\Event\LoadClassMetadataEventArgs; |
|
19
|
|
|
use Doctrine\ORM\Mapping\ClassMetadataFactory; |
|
20
|
|
|
use Doctrine\ORM\Mapping\ClassMetadataInfo; |
|
21
|
|
|
use PhpSpec\ObjectBehavior; |
|
22
|
|
|
use Prophecy\Argument; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @author Mateusz Zalewski <[email protected]> |
|
26
|
|
|
* @author Grzegorz Sadowski <[email protected]> |
|
27
|
|
|
*/ |
|
28
|
|
|
final class LoadMetadataSubscriberSpec extends ObjectBehavior |
|
29
|
|
|
{ |
|
30
|
|
|
function let(): void |
|
31
|
|
|
{ |
|
32
|
|
|
$this->beConstructedWith([ |
|
33
|
|
|
'reviewable' => [ |
|
34
|
|
|
'subject' => 'AcmeBundle\Entity\ReviewableModel', |
|
35
|
|
|
'review' => [ |
|
36
|
|
|
'classes' => [ |
|
37
|
|
|
'model' => 'AcmeBundle\Entity\ReviewModel', |
|
38
|
|
|
], |
|
39
|
|
|
], |
|
40
|
|
|
'reviewer' => [ |
|
41
|
|
|
'classes' => [ |
|
42
|
|
|
'model' => 'AcmeBundle\Entity\ReviewerModel', |
|
43
|
|
|
], |
|
44
|
|
|
], |
|
45
|
|
|
], |
|
46
|
|
|
]); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
function it_implements_event_subscriber(): void |
|
50
|
|
|
{ |
|
51
|
|
|
$this->shouldImplement(EventSubscriber::class); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
function it_has_subscribed_events(): void |
|
55
|
|
|
{ |
|
56
|
|
|
$this->getSubscribedEvents()->shouldReturn(['loadClassMetadata']); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
function it_maps_proper_relations_for_review_model( |
|
60
|
|
|
ClassMetadataFactory $metadataFactory, |
|
61
|
|
|
ClassMetadataInfo $classMetadataInfo, |
|
62
|
|
|
ClassMetadataInfo $metadata, |
|
63
|
|
|
EntityManager $entityManager, |
|
64
|
|
|
LoadClassMetadataEventArgs $eventArguments |
|
65
|
|
|
): void { |
|
66
|
|
|
$eventArguments->getClassMetadata()->willReturn($metadata); |
|
67
|
|
|
$eventArguments->getEntityManager()->willReturn($entityManager); |
|
68
|
|
|
$entityManager->getMetadataFactory()->willReturn($metadataFactory); |
|
69
|
|
|
|
|
70
|
|
|
$classMetadataInfo->fieldMappings = ['id' => ['columnName' => 'id']]; |
|
71
|
|
|
$metadataFactory->getMetadataFor('AcmeBundle\Entity\ReviewableModel')->willReturn($classMetadataInfo); |
|
72
|
|
|
$metadataFactory->getMetadataFor('AcmeBundle\Entity\ReviewerModel')->willReturn($classMetadataInfo); |
|
73
|
|
|
$metadata->getName()->willReturn('AcmeBundle\Entity\ReviewModel'); |
|
74
|
|
|
|
|
75
|
|
|
$metadata->mapManyToOne([ |
|
76
|
|
|
'fieldName' => 'reviewSubject', |
|
77
|
|
|
'targetEntity' => 'AcmeBundle\Entity\ReviewableModel', |
|
78
|
|
|
'inversedBy' => 'reviews', |
|
79
|
|
|
'joinColumns' => [[ |
|
80
|
|
|
'name' => 'reviewable_id', |
|
81
|
|
|
'referencedColumnName' => 'id', |
|
82
|
|
|
'nullable' => false, |
|
83
|
|
|
'onDelete' => 'CASCADE', |
|
84
|
|
|
]], |
|
85
|
|
|
])->shouldBeCalled(); |
|
86
|
|
|
|
|
87
|
|
|
$metadata->mapManyToOne([ |
|
88
|
|
|
'fieldName' => 'author', |
|
89
|
|
|
'targetEntity' => 'AcmeBundle\Entity\ReviewerModel', |
|
90
|
|
|
'joinColumns' => [[ |
|
91
|
|
|
'name' => 'author_id', |
|
92
|
|
|
'referencedColumnName' => 'id', |
|
93
|
|
|
'nullable' => false, |
|
94
|
|
|
'onDelete' => 'CASCADE', |
|
95
|
|
|
]], |
|
96
|
|
|
'cascade' => ['persist'], |
|
97
|
|
|
])->shouldBeCalled(); |
|
98
|
|
|
|
|
99
|
|
|
$this->loadClassMetadata($eventArguments); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
function it_maps_proper_relations_for_reviewable_model( |
|
103
|
|
|
ClassMetadataFactory $metadataFactory, |
|
104
|
|
|
ClassMetadataInfo $metadata, |
|
105
|
|
|
EntityManager $entityManager, |
|
106
|
|
|
LoadClassMetadataEventArgs $eventArguments |
|
107
|
|
|
): void { |
|
108
|
|
|
$eventArguments->getClassMetadata()->willReturn($metadata); |
|
109
|
|
|
$eventArguments->getEntityManager()->willReturn($entityManager); |
|
110
|
|
|
$entityManager->getMetadataFactory()->willReturn($metadataFactory); |
|
111
|
|
|
$metadata->getName()->willReturn('AcmeBundle\Entity\ReviewableModel'); |
|
112
|
|
|
|
|
113
|
|
|
$metadata->mapOneToMany([ |
|
114
|
|
|
'fieldName' => 'reviews', |
|
115
|
|
|
'targetEntity' => 'AcmeBundle\Entity\ReviewModel', |
|
116
|
|
|
'mappedBy' => 'reviewSubject', |
|
117
|
|
|
'cascade' => ['all'], |
|
118
|
|
|
])->shouldBeCalled(); |
|
119
|
|
|
|
|
120
|
|
|
$this->loadClassMetadata($eventArguments); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
function it_skips_mapping_configuration_if_metadata_name_is_not_different( |
|
124
|
|
|
ClassMetadataFactory $metadataFactory, |
|
125
|
|
|
ClassMetadataInfo $metadata, |
|
126
|
|
|
EntityManager $entityManager, |
|
127
|
|
|
LoadClassMetadataEventArgs $eventArguments |
|
128
|
|
|
): void { |
|
129
|
|
|
$this->beConstructedWith([ |
|
130
|
|
|
'reviewable' => [ |
|
131
|
|
|
'subject' => 'AcmeBundle\Entity\ReviewableModel', |
|
132
|
|
|
'review' => [ |
|
133
|
|
|
'classes' => [ |
|
134
|
|
|
'model' => 'AcmeBundle\Entity\BadReviewModel', |
|
135
|
|
|
], |
|
136
|
|
|
], |
|
137
|
|
|
'reviewer' => [ |
|
138
|
|
|
'classes' => [ |
|
139
|
|
|
'model' => 'AcmeBundle\Entity\ReviewerModel', |
|
140
|
|
|
], |
|
141
|
|
|
], |
|
142
|
|
|
], |
|
143
|
|
|
]); |
|
144
|
|
|
|
|
145
|
|
|
$eventArguments->getClassMetadata()->willReturn($metadata); |
|
146
|
|
|
$eventArguments->getEntityManager()->willReturn($entityManager); |
|
147
|
|
|
$entityManager->getMetadataFactory()->willReturn($metadataFactory); |
|
148
|
|
|
$metadata->getName()->willReturn('AcmeBundle\Entity\ReviewModel'); |
|
149
|
|
|
|
|
150
|
|
|
$metadata->mapManyToOne(Argument::type('array'))->shouldNotBeCalled(); |
|
151
|
|
|
$metadata->mapManyToOne(Argument::type('array'))->shouldNotBeCalled(); |
|
152
|
|
|
|
|
153
|
|
|
$this->loadClassMetadata($eventArguments); |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
|