Completed
Push — master ( 3ff7e6...cf5edc )
by Kamil
96:30 queued 63:14
created

it_is_initializable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 4
rs 10
c 1
b 0
f 0
cc 1
eloc 2
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
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(AddressComparatorInterface $addressComparator, CustomerContextInterface $customerContext)
31
    {
32
        $this->beConstructedWith($addressComparator, $customerContext);
33
    }
34
35
    function it_is_initializable()
36
    {
37
        $this->shouldHaveType(CustomerUniqueAddressAdder::class);
38
    }
39
40
    function it_implements_address_adder_interface()
41
    {
42
        $this->shouldImplement(AddressAdderInterface::class);
43
    }
44
45
    function it_does_nothing_when_there_is_no_customer(
46
        AddressComparatorInterface $addressComparator,
47
        CustomerContextInterface $customerContext,
48
        CustomerInterface $customer,
49
        AddressInterface $address
50
    ) {
51
        $customerContext->getCustomer()->willReturn(null);
52
53
        $addressComparator->same(
54
            Argument::type(AddressInterface::class),
55
            Argument::type(AddressInterface::class)
56
        )->shouldNotBeCalled();
57
58
        $customer->addAddress($address)->shouldNotBeCalled();
59
60
        $this->add($address);
61
    }
62
63
    function it_does_nothing_when_an_address_is_already_present_on_the_customer(
64
        AddressComparatorInterface $addressComparator,
65
        CustomerContextInterface $customerContext,
66
        CustomerInterface $customer,
67
        AddressInterface $address,
68
        Collection $addresses,
69
        \Iterator $iterator
70
    ) {
71
        $iterator->rewind()->shouldBeCalled();
72
        $iterator->valid()->willReturn(true);
73
        $iterator->current()->willReturn($address);
74
75
        $addresses->getIterator()->willReturn($iterator);
76
        $customer->getAddresses()->willReturn($addresses);
77
78
        $customerContext->getCustomer()->willReturn($customer);
79
80
        $addressComparator->same($address, $address)->willReturn(true);
81
82
        $customer->addAddress($address)->shouldNotBeCalled();
83
84
        $this->add($address);
85
    }
86
87
    function it_adds_an_address_when_no_other_is_present_on_the_customer(
88
        AddressComparatorInterface $addressComparator,
89
        CustomerContextInterface $customerContext,
90
        CustomerInterface $customer,
91
        AddressInterface $address,
92
        Collection $addresses,
93
        \Iterator $iterator
94
    ) {
95
        $iterator->rewind()->shouldBeCalled();
96
        $iterator->valid()->willReturn(false);
97
98
        $addresses->getIterator()->willReturn($iterator);
99
        $customer->getAddresses()->willReturn($addresses);
100
101
        $customerContext->getCustomer()->willReturn($customer);
102
103
        $addressComparator->same(
104
            Argument::type(AddressInterface::class),
105
            Argument::type(AddressInterface::class)
106
        )->shouldNotBeCalled();
107
108
        $customer->addAddress($address)->shouldBeCalled();
109
110
        $this->add($address);
111
    }
112
113
    function it_adds_an_address_when_different_than_the_ones_present_on_the_customer(
114
        AddressComparatorInterface $addressComparator,
115
        CustomerContextInterface $customerContext,
116
        CustomerInterface $customer,
117
        AddressInterface $customerAddress,
118
        AddressInterface $newAddress,
119
        Collection $addresses,
120
        \Iterator $iterator
121
    ) {
122
        $iterator->rewind()->shouldBeCalled();
123
        $iterator->valid()->willReturn(true);
124
        $iterator->current()->willReturn($customerAddress);
125
        $iterator->valid()->willReturn(false);
126
127
        $addresses->getIterator()->willReturn($iterator);
128
        $customer->getAddresses()->willReturn($addresses);
129
130
        $customerContext->getCustomer()->willReturn($customer);
131
132
        $addressComparator->same($customerAddress, $newAddress)->willReturn(false);
133
134
        $customer->addAddress($newAddress)->shouldBeCalled();
135
136
        $this->add($newAddress);
137
    }
138
}
139