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
|
|
|
namespace spec\Sylius\Bundle\AssociationBundle\EventListener; |
13
|
|
|
|
14
|
|
|
use Doctrine\Common\EventSubscriber; |
15
|
|
|
use Doctrine\ORM\EntityManager; |
16
|
|
|
use Doctrine\ORM\Event\LoadClassMetadataEventArgs; |
17
|
|
|
use Doctrine\ORM\Mapping\ClassMetadataFactory; |
18
|
|
|
use Doctrine\ORM\Mapping\ClassMetadataInfo; |
19
|
|
|
use PhpSpec\ObjectBehavior; |
20
|
|
|
use Prophecy\Argument; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @author Łukasz Chruściel <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
class LoadMetadataSubscriberSpec extends ObjectBehavior |
26
|
|
|
{ |
27
|
|
|
function let() |
28
|
|
|
{ |
29
|
|
|
$this->beConstructedWith([ |
30
|
|
|
'product' => [ |
31
|
|
|
'subject' => 'Some\App\Product\Entity\Product', |
32
|
|
|
'association' => [ |
33
|
|
|
'model' => 'Some\App\Product\Entity\EntityAssociation', |
34
|
|
|
], |
35
|
|
|
'association_type' => [ |
36
|
|
|
'model' => 'Some\App\Product\Entity\AssociationType', |
37
|
|
|
], |
38
|
|
|
], |
39
|
|
|
]); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
function it_is_initializable() |
43
|
|
|
{ |
44
|
|
|
$this->shouldHaveType('Sylius\Bundle\AssociationBundle\EventListener\LoadMetadataSubscriber'); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
function it_is_a_doctrine_event_subscriber() |
48
|
|
|
{ |
49
|
|
|
$this->shouldImplement(EventSubscriber::class); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
function it_subscribes_a_doctrines_load_class_metadata_event() |
53
|
|
|
{ |
54
|
|
|
$this->getSubscribedEvents()->shouldReturn(['loadClassMetadata']); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
function it_loads_class_metadata_for_associations( |
58
|
|
|
LoadClassMetadataEventArgs $eventArguments, |
59
|
|
|
ClassMetadataInfo $metadata, |
60
|
|
|
EntityManager $entityManager, |
61
|
|
|
ClassMetadataFactory $classMetadataFactory, |
62
|
|
|
ClassMetadataInfo $classMetadataInfo |
63
|
|
|
) { |
64
|
|
|
$eventArguments->getClassMetadata()->willReturn($metadata)->shouldBeCalled(); |
65
|
|
|
$eventArguments->getEntityManager()->willReturn($entityManager)->shouldBeCalled(); |
66
|
|
|
$entityManager->getMetadataFactory()->willReturn($classMetadataFactory)->shouldBeCalled(); |
67
|
|
|
$classMetadataInfo->fieldMappings = [ |
68
|
|
|
'id' => [ |
69
|
|
|
'columnName' => 'id', |
70
|
|
|
], |
71
|
|
|
]; |
72
|
|
|
|
73
|
|
|
$classMetadataFactory->getMetadataFor('Some\App\Product\Entity\Product')->willReturn($classMetadataInfo); |
74
|
|
|
$classMetadataFactory->getMetadataFor('Some\App\Product\Entity\AssociationType')->willReturn($classMetadataInfo); |
75
|
|
|
|
76
|
|
|
$subjectMapping = [ |
77
|
|
|
'fieldName' => 'owner', |
78
|
|
|
'targetEntity' => 'Some\App\Product\Entity\Product', |
79
|
|
|
'inversedBy' => 'associations', |
80
|
|
|
'joinColumns' => [[ |
81
|
|
|
'name' => 'product_id', |
82
|
|
|
'referencedColumnName' => 'id', |
83
|
|
|
'nullable' => false, |
84
|
|
|
'onDelete' => 'CASCADE', |
85
|
|
|
]], |
86
|
|
|
]; |
87
|
|
|
$associationMapping = [ |
88
|
|
|
'fieldName' => 'associatedObjects', |
89
|
|
|
'targetEntity' => 'Some\App\Product\Entity\Product', |
90
|
|
|
'joinColumns' => [[ |
91
|
|
|
'name' => 'product_id', |
92
|
|
|
'referencedColumnName' => 'id', |
93
|
|
|
'nullable' => false, |
94
|
|
|
'onDelete' => 'CASCADE', |
95
|
|
|
]], |
96
|
|
|
]; |
97
|
|
|
$associationTypeMapping = [ |
98
|
|
|
'fieldName' => 'type', |
99
|
|
|
'targetEntity' => 'Some\App\Product\Entity\AssociationType', |
100
|
|
|
'joinColumns' => [[ |
101
|
|
|
'name' => 'association_type_id', |
102
|
|
|
'referencedColumnName' => 'id', |
103
|
|
|
'nullable' => false, |
104
|
|
|
'onDelete' => 'CASCADE', |
105
|
|
|
]], |
106
|
|
|
]; |
107
|
|
|
|
108
|
|
|
$metadata->getName()->willReturn('Some\App\Product\Entity\EntityAssociation'); |
109
|
|
|
$metadata->mapManyToOne($subjectMapping)->shouldBeCalled(); |
110
|
|
|
$metadata->mapManyToMany($associationMapping)->shouldBeCalled(); |
111
|
|
|
$metadata->mapManyToOne($associationTypeMapping)->shouldBeCalled(); |
112
|
|
|
|
113
|
|
|
$this->loadClassMetadata($eventArguments); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|