Completed
Push — master ( 85508a...fbb5f8 )
by Kamil
24:40
created

CustomerUniqueAddressAdderSpec   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 7

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 7
dl 0
loc 122
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A let() 0 7 1
A it_is_initializable() 0 4 1
A it_implements_address_adder_interface() 0 4 1
A it_does_nothing_when_there_is_no_customer() 0 19 1
B it_does_nothing_when_an_address_is_already_present_on_the_customer() 0 25 1
B it_adds_an_address_when_no_other_is_present_on_the_customer() 0 27 1
B it_adds_an_address_when_different_than_the_ones_present_on_the_customer() 0 27 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\Core\Customer;
13
14
use Doctrine\Common\Collections\Collection;
15
use Doctrine\Common\Persistence\ObjectManager;
16
use PhpSpec\ObjectBehavior;
17
use Prophecy\Argument;
18
use Sylius\Component\Addressing\Comparator\AddressComparatorInterface;
19
use Sylius\Component\Core\Customer\AddressAdderInterface;
20
use Sylius\Component\Core\Customer\CustomerUniqueAddressAdder;
21
use Sylius\Component\Core\Model\AddressInterface;
22
use Sylius\Component\Core\Model\CustomerInterface;
23
use Sylius\Component\Customer\Context\CustomerContextInterface;
24
25
/**
26
 * @author Jan Góralski <[email protected]>
27
 */
28
final class CustomerUniqueAddressAdderSpec extends ObjectBehavior
29
{
30
    function let(
31
        AddressComparatorInterface $addressComparator,
32
        CustomerContextInterface $customerContext,
33
        ObjectManager $customerManager
34
    ) {
35
        $this->beConstructedWith($addressComparator, $customerContext, $customerManager);
36
    }
37
38
    function it_is_initializable()
39
    {
40
        $this->shouldHaveType(CustomerUniqueAddressAdder::class);
41
    }
42
43
    function it_implements_address_adder_interface()
44
    {
45
        $this->shouldImplement(AddressAdderInterface::class);
46
    }
47
48
    function it_does_nothing_when_there_is_no_customer(
49
        AddressComparatorInterface $addressComparator,
50
        CustomerContextInterface $customerContext,
51
        ObjectManager $customerManager,
52
        CustomerInterface $customer,
53
        AddressInterface $address
54
    ) {
55
        $customerContext->getCustomer()->willReturn(null);
56
57
        $addressComparator->same(
58
            Argument::type(AddressInterface::class),
59
            Argument::type(AddressInterface::class)
60
        )->shouldNotBeCalled();
61
62
        $customer->addAddress($address)->shouldNotBeCalled();
63
        $customerManager->flush()->shouldNotBeCalled();
64
65
        $this->add($address);
66
    }
67
68
    function it_does_nothing_when_an_address_is_already_present_on_the_customer(
69
        AddressComparatorInterface $addressComparator,
70
        CustomerContextInterface $customerContext,
71
        ObjectManager $customerManager,
72
        CustomerInterface $customer,
73
        AddressInterface $address,
74
        Collection $addresses,
75
        \Iterator $iterator
76
    ) {
77
        $iterator->rewind()->shouldBeCalled();
78
        $iterator->valid()->willReturn(true);
79
        $iterator->current()->willReturn($address);
80
81
        $addresses->getIterator()->willReturn($iterator);
82
        $customer->getAddresses()->willReturn($addresses);
83
84
        $customerContext->getCustomer()->willReturn($customer);
85
86
        $addressComparator->same($address, $address)->willReturn(true);
87
88
        $customer->addAddress($address)->shouldNotBeCalled();
89
        $customerManager->flush()->shouldNotBeCalled();
90
91
        $this->add($address);
92
    }
93
94
    function it_adds_an_address_when_no_other_is_present_on_the_customer(
95
        AddressComparatorInterface $addressComparator,
96
        CustomerContextInterface $customerContext,
97
        ObjectManager $customerManager,
98
        CustomerInterface $customer,
99
        AddressInterface $address,
100
        Collection $addresses,
101
        \Iterator $iterator
102
    ) {
103
        $iterator->rewind()->shouldBeCalled();
104
        $iterator->valid()->willReturn(false);
105
106
        $addresses->getIterator()->willReturn($iterator);
107
        $customer->getAddresses()->willReturn($addresses);
108
109
        $customerContext->getCustomer()->willReturn($customer);
110
111
        $addressComparator->same(
112
            Argument::type(AddressInterface::class),
113
            Argument::type(AddressInterface::class)
114
        )->shouldNotBeCalled();
115
116
        $customer->addAddress($address)->shouldBeCalled();
117
        $customerManager->flush()->shouldBeCalled();
118
119
        $this->add($address);
120
    }
121
122
    function it_adds_an_address_when_different_than_the_ones_present_on_the_customer(
123
        AddressComparatorInterface $addressComparator,
124
        CustomerContextInterface $customerContext,
125
        ObjectManager $customerManager,
126
        CustomerInterface $customer,
127
        AddressInterface $customerAddress,
128
        AddressInterface $newAddress,
129
        Collection $addresses,
130
        \Iterator $iterator
131
    ) {
132
        $iterator->rewind()->shouldBeCalled();
133
        $iterator->valid()->willReturn(true);
134
        $iterator->current()->willReturn($customerAddress);
135
        $iterator->valid()->willReturn(false);
136
137
        $addresses->getIterator()->willReturn($iterator);
138
        $customer->getAddresses()->willReturn($addresses);
139
140
        $customerContext->getCustomer()->willReturn($customer);
141
142
        $addressComparator->same($customerAddress, $newAddress)->willReturn(false);
143
144
        $customer->addAddress($newAddress)->shouldBeCalled();
145
        $customerManager->flush()->shouldBeCalled();
146
147
        $this->add($newAddress);
148
    }
149
}
150