Completed
Push — master ( f54cb8...625557 )
by Kamil
19:28
created

LoadMetadataSubscriberSpec::let()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 10
nc 1
nop 0
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\AttributeBundle\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
use Sylius\Bundle\AttributeBundle\Doctrine\ORM\Subscriber\LoadMetadataSubscriber;
24
25
final class LoadMetadataSubscriberSpec extends ObjectBehavior
26
{
27
    function let(): void
28
    {
29
        $this->beConstructedWith([
30
            'product' => [
31
                'subject' => 'Some\App\Product\Entity\Product',
32
                'attribute' => [
33
                    'classes' => [
34
                        'model' => 'Some\App\Product\Entity\Attribute',
35
                    ],
36
                ],
37
                'attribute_value' => [
38
                    'classes' => [
39
                        'model' => 'Some\App\Product\Entity\AttributeValue',
40
                    ],
41
                ],
42
            ],
43
        ]);
44
    }
45
46
    function it_is_initializable(): void
47
    {
48
        $this->shouldHaveType(LoadMetadataSubscriber::class);
49
    }
50
51
    function it_is_a_doctrine_event_subscriber(): void
52
    {
53
        $this->shouldImplement(EventSubscriber::class);
54
    }
55
56
    function it_subscribes_load_class_metadata_event(): void
57
    {
58
        $this->getSubscribedEvents()->shouldReturn(['loadClassMetadata']);
59
    }
60
61
    function it_does_not_add_a_many_to_one_mapping_if_the_class_is_not_a_configured_attribute_value_model(
62
        LoadClassMetadataEventArgs $eventArgs,
63
        ClassMetadataInfo $metadata,
64
        EntityManager $entityManager,
65
        ClassMetadataFactory $classMetadataFactory
66
    ): void {
67
        $eventArgs->getEntityManager()->willReturn($entityManager);
68
        $entityManager->getMetadataFactory()->willReturn($classMetadataFactory);
69
70
        $eventArgs->getClassMetadata()->willReturn($metadata);
71
        $metadata->getName()->willReturn('KeepMoving\ThisClass\DoesNot\Concern\You');
72
73
        $metadata->mapManyToOne(Argument::any())->shouldNotBeCalled();
74
75
        $this->loadClassMetadata($eventArgs);
76
    }
77
78
    function it_maps_many_to_one_associations_from_the_attribute_value_model_to_the_subject_model_and_the_attribute_model(
79
        LoadClassMetadataEventArgs $eventArgs,
80
        ClassMetadataInfo $metadata,
81
        EntityManager $entityManager,
82
        ClassMetadataFactory $classMetadataFactory,
83
        ClassMetadataInfo $classMetadataInfo
84
    ): void {
85
        $eventArgs->getEntityManager()->willReturn($entityManager);
86
        $entityManager->getMetadataFactory()->willReturn($classMetadataFactory);
87
        $classMetadataInfo->fieldMappings = [
88
            'id' => [
89
                'columnName' => 'id',
90
            ],
91
        ];
92
        $classMetadataFactory->getMetadataFor('Some\App\Product\Entity\Product')->willReturn($classMetadataInfo);
93
        $classMetadataFactory->getMetadataFor('Some\App\Product\Entity\Attribute')->willReturn($classMetadataInfo);
94
95
        $eventArgs->getClassMetadata()->willReturn($metadata);
96
        $eventArgs->getClassMetadata()->willReturn($metadata);
97
        $metadata->getName()->willReturn('Some\App\Product\Entity\AttributeValue');
98
99
        $subjectMapping = [
100
            'fieldName' => 'subject',
101
            'targetEntity' => 'Some\App\Product\Entity\Product',
102
            'inversedBy' => 'attributes',
103
            'joinColumns' => [[
104
                'name' => 'product_id',
105
                'referencedColumnName' => 'id',
106
                'nullable' => false,
107
                'onDelete' => 'CASCADE',
108
            ]],
109
        ];
110
111
        $attributeMapping = [
112
            'fieldName' => 'attribute',
113
            'targetEntity' => 'Some\App\Product\Entity\Attribute',
114
            'joinColumns' => [[
115
                'name' => 'attribute_id',
116
                'referencedColumnName' => 'id',
117
                'nullable' => false,
118
                'onDelete' => 'CASCADE',
119
            ]],
120
        ];
121
122
        $metadata->mapManyToOne($subjectMapping)->shouldBeCalled();
123
        $metadata->mapManyToOne($attributeMapping)->shouldBeCalled();
124
125
        $this->loadClassMetadata($eventArgs);
126
    }
127
128
    function it_does_not_add_values_one_to_many_mapping_if_the_class_is_not_a_configured_attribute_model(
129
        LoadClassMetadataEventArgs $eventArgs,
130
        ClassMetadataInfo $metadata,
131
        EntityManager $entityManager,
132
        ClassMetadataFactory $classMetadataFactory
133
    ): void {
134
        $eventArgs->getEntityManager()->willReturn($entityManager);
135
        $entityManager->getMetadataFactory()->willReturn($classMetadataFactory);
136
137
        $eventArgs->getClassMetadata()->willReturn($metadata);
138
        $metadata->getName()->willReturn('KeepMoving\ThisClass\DoesNot\Concern\You');
139
140
        $metadata->mapOneToMany(Argument::any())->shouldNotBeCalled();
141
142
        $this->loadClassMetadata($eventArgs);
143
    }
144
}
145