Completed
Push — attribute-null-value ( 0820e8 )
by Kamil
34:26 queued 12:15
created

AttributeValueSpec::its_value_can_be_set_to_null()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 14
nc 2
nop 1
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\Component\Attribute\Model;
13
14
use PhpSpec\ObjectBehavior;
15
use Sylius\Component\Attribute\Model\AttributeInterface;
16
use Sylius\Component\Attribute\Model\AttributeSubjectInterface;
17
use Sylius\Component\Attribute\Model\AttributeValue;
18
use Sylius\Component\Attribute\Model\AttributeValueInterface;
19
20
/**
21
 * @author Paweł Jędrzejewski <[email protected]>
22
 * @author Mateusz Zalewski <[email protected]>
23
 */
24
final class AttributeValueSpec extends ObjectBehavior
25
{
26
    function it_is_initializable()
27
    {
28
        $this->shouldHaveType(AttributeValue::class);
29
    }
30
31
    function it_implements_Sylius_subject_attribute_interface()
0 ignored issues
show
Coding Style introduced by
function it_implements_S...t_attribute_interface() does not seem to conform to the naming convention (^(?:(?:[a-z]|__)[a-zA-Z0-9]*|[a-z][a-z0-9_]*)$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
32
    {
33
        $this->shouldImplement(AttributeValueInterface::class);
34
    }
35
36
    function it_has_no_id_by_default()
37
    {
38
        $this->getId()->shouldReturn(null);
39
    }
40
41
    function it_does_not_belong_to_a_subject_by_default()
42
    {
43
        $this->getSubject()->shouldReturn(null);
44
    }
45
46
    function it_allows_assigning_itself_to_a_subject(AttributeSubjectInterface $subject)
47
    {
48
        $this->setSubject($subject);
49
        $this->getSubject()->shouldReturn($subject);
50
    }
51
52
    function it_allows_detaching_itself_from_a_subject(AttributeSubjectInterface $subject)
53
    {
54
        $this->setSubject($subject);
55
        $this->getSubject()->shouldReturn($subject);
56
57
        $this->setSubject(null);
58
        $this->getSubject()->shouldReturn(null);
59
    }
60
61
    function it_has_no_attribute_defined_by_default()
62
    {
63
        $this->getAttribute()->shouldReturn(null);
64
    }
65
66
    function its_attribute_is_definable(AttributeInterface $attribute)
67
    {
68
        $this->setAttribute($attribute);
69
        $this->getAttribute()->shouldReturn($attribute);
70
    }
71
72
    function it_has_no_default_locale_code()
73
    {
74
        $this->getLocaleCode()->shouldReturn(null);
75
    }
76
77
    function its_locale_code_is_mutable()
78
    {
79
        $this->setLocaleCode('en');
80
        $this->getLocaleCode()->shouldReturn('en');
81
    }
82
83
    function it_has_no_value_by_default()
84
    {
85
        $this->getValue()->shouldReturn(null);
86
    }
87
88
    function its_value_is_mutable_based_on_attribute_storage_type(AttributeInterface $attribute)
89
    {
90
        $storageTypeToExampleData = [
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $storageTypeToExampleData exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
91
            'boolean' => false,
92
            'text' => 'Lorem ipsum',
93
            'integer' => 42,
94
            'float' => 6.66,
95
            'datetime' => new \DateTime(),
96
            'date' => new \DateTime(),
97
            'json' => ['foo' => 'bar'],
98
        ];
99
100
        foreach ($storageTypeToExampleData as $storageType => $exampleData) {
101
            $attribute->getStorageType()->willReturn($storageType);
102
            $this->setAttribute($attribute);
103
104
            $this->setValue($exampleData);
105
            $this->getValue()->shouldReturn($exampleData);
106
        }
107
    }
108
109
    function its_value_can_be_set_to_null(AttributeInterface $attribute)
110
    {
111
        $storageTypes = [
112
            'boolean',
113
            'text',
114
            'integer',
115
            'float',
116
            'datetime',
117
            'date',
118
            'json',
119
        ];
120
121
        foreach ($storageTypes as $storageType) {
122
            $attribute->getStorageType()->willReturn($storageType);
123
            $this->setAttribute($attribute);
124
125
            $this->setValue(null);
126
            $this->getValue()->shouldReturn(null);
127
        }
128
    }
129
130
    function it_throws_exception_when_trying_to_get_code_without_attribute_defined()
131
    {
132
        $this
133
            ->shouldThrow(\BadMethodCallException::class)
134
            ->during('getCode')
135
        ;
136
    }
137
138
    function it_returns_its_attribute_code(AttributeInterface $attribute)
139
    {
140
        $attribute->getCode()->willReturn('tshirt_material');
141
        $this->setAttribute($attribute);
142
143
        $this->getCode()->shouldReturn('tshirt_material');
144
    }
145
146
    function it_throws_exception_when_trying_to_get_name_without_attribute_defined()
147
    {
148
        $this
149
            ->shouldThrow(\BadMethodCallException::class)
150
            ->during('getName')
151
        ;
152
    }
153
154
    function it_returns_its_attribute_name(AttributeInterface $attribute)
155
    {
156
        $attribute->getName()->willReturn('T-Shirt material');
157
        $this->setAttribute($attribute);
158
159
        $this->getName()->shouldReturn('T-Shirt material');
160
    }
161
162
    function it_throws_exception_when_trying_to_get_type_without_attribute_defined()
163
    {
164
        $this
165
            ->shouldThrow(\BadMethodCallException::class)
166
            ->during('getType')
167
        ;
168
    }
169
170
    function it_returns_its_attribute_type(AttributeInterface $attribute)
171
    {
172
        $attribute->getType()->willReturn('choice');
173
        $this->setAttribute($attribute);
174
175
        $this->getType()->shouldReturn('choice');
176
    }
177
}
178