Completed
Push — master ( 4f4b30...79d213 )
by Kamil
49:20 queued 33s
created

OrderContextSpec   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 12

Importance

Changes 3
Bugs 2 Features 0
Metric Value
wmc 6
c 3
b 2
f 0
lcom 0
cbo 12
dl 0
loc 123
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A let() 0 21 1
A it_is_initializable() 0 4 1
A it_implements_context_interface() 0 4 1
B it_creates_customers_order() 0 24 1
B it_adds_shipping_payment_and_addressing_info_to_an_order() 0 34 1
B it_adds_single_item_by_customer() 0 28 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\Behat\Context\Setup;
13
14
use Behat\Behat\Context\Context;
15
use Doctrine\Common\Collections\Collection;
16
use Doctrine\Common\Persistence\ObjectManager;
17
use PhpSpec\ObjectBehavior;
18
use Sylius\Bundle\OrderBundle\Modifier\OrderItemQuantityModifierInterface;
19
use Sylius\Component\Core\Model\AddressInterface;
20
use Sylius\Component\Core\Model\ChannelInterface;
21
use Sylius\Component\Core\Model\CustomerInterface;
22
use Sylius\Component\Core\Model\OrderInterface;
23
use Sylius\Component\Core\Model\OrderItemInterface;
24
use Sylius\Component\Core\Model\PaymentInterface;
25
use Sylius\Component\Core\Model\ProductInterface;
26
use Sylius\Component\Core\Model\ProductVariantInterface;
27
use Sylius\Component\Core\Model\ShipmentInterface;
28
use Sylius\Component\Core\Model\ShippingMethodInterface;
29
use Sylius\Component\Core\OrderProcessing\OrderShipmentFactoryInterface;
30
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
31
use Sylius\Component\Core\Test\Services\SharedStorageInterface;
32
use Sylius\Component\Currency\Model\CurrencyInterface;
33
use Sylius\Component\Payment\Factory\PaymentFactoryInterface;
34
use Sylius\Component\Payment\Model\PaymentMethodInterface;
35
use Sylius\Component\Resource\Factory\FactoryInterface;
36
37
/**
38
 * @author Łukasz Chruściel <[email protected]>
39
 */
40
class OrderContextSpec extends ObjectBehavior
41
{
42
    function let(
43
        SharedStorageInterface $sharedStorage,
44
        FactoryInterface $orderFactory,
45
        OrderRepositoryInterface $orderRepository,
46
        OrderShipmentFactoryInterface $orderShipmentFactory,
47
        PaymentFactoryInterface $paymentFactory,
48
        FactoryInterface $orderItemFactory,
49
        OrderItemQuantityModifierInterface $itemQuantityModifier,
50
        ObjectManager $objectManager
51
    ) {
52
        $this->beConstructedWith(
53
            $sharedStorage,
54
            $orderRepository,
55
            $orderFactory,
56
            $orderShipmentFactory,
57
            $paymentFactory,
58
            $orderItemFactory,
59
            $itemQuantityModifier,
60
            $objectManager
61
        );
62
    }
63
64
    function it_is_initializable()
65
    {
66
        $this->shouldHaveType('Sylius\Behat\Context\Setup\OrderContext');
67
    }
68
69
    function it_implements_context_interface()
70
    {
71
        $this->shouldImplement(Context::class);
72
    }
73
74
    function it_creates_customers_order(
75
        CustomerInterface $customer,
76
        ChannelInterface $channel,
77
        CurrencyInterface $currency,
78
        FactoryInterface $orderFactory,
79
        OrderInterface $order,
80
        OrderRepositoryInterface $orderRepository,
81
        SharedStorageInterface $sharedStorage
82
    ) {
83
        $sharedStorage->get('channel')->willReturn($channel);
84
        $sharedStorage->get('currency')->willReturn($currency);
85
86
        $orderFactory->createNew()->willReturn($order);
87
88
        $order->setCustomer($customer)->shouldBeCalled();
89
        $order->setChannel($channel)->shouldBeCalled();
90
        $order->setCurrency($currency)->shouldBeCalled();
91
        $order->setNumber('#00000022')->shouldBeCalled();
92
93
        $orderRepository->add($order)->shouldBeCalled();
94
        $sharedStorage->set('order', $order)->shouldBeCalled();
95
96
        $this->theCustomerPlacedAnOrder($customer, '#00000022');
97
    }
98
99
    function it_adds_shipping_payment_and_addressing_info_to_an_order(
100
        AddressInterface $address,
101
        Collection $shipmentCollection,
102
        OrderInterface $order,
103
        OrderShipmentFactoryInterface $orderShipmentFactory,
104
        PaymentFactoryInterface $paymentFactory,
105
        PaymentInterface $payment,
106
        PaymentMethodInterface $paymentMethod,
107
        SharedStorageInterface $sharedStorage,
108
        ShipmentInterface $shipment,
109
        ShippingMethodInterface $shippingMethod,
110
        ObjectManager $objectManager
111
    ) {
112
        $sharedStorage->get('order')->willReturn($order);
113
114
        $order->getCurrency()->willReturn('EUR');
115
        $order->getTotal()->willReturn(1234);
116
        $order->getShipments()->willReturn($shipmentCollection);
117
118
        $shipmentCollection->first()->willReturn($shipment);
119
120
        $paymentFactory->createWithAmountAndCurrency(1234, 'EUR')->willReturn($payment);
121
122
        $order->setBillingAddress($address)->shouldBeCalled();
123
        $order->setShippingAddress($address)->shouldBeCalled();
124
        $order->addPayment($payment)->shouldBeCalled();
125
        $payment->setMethod($paymentMethod)->shouldBeCalled();
126
        $shipment->setMethod($shippingMethod)->shouldBeCalled();
127
        $orderShipmentFactory->createForOrder($order)->shouldBeCalled();
128
129
        $objectManager->flush()->shouldBeCalled();
130
131
        $this->theCustomerChoseShippingToWithPayment($shippingMethod, $address, $paymentMethod);
132
    }
133
134
    function it_adds_single_item_by_customer(
135
        FactoryInterface $orderItemFactory,
136
        OrderInterface $order,
137
        OrderItemInterface $item,
138
        OrderItemQuantityModifierInterface $itemQuantityModifier,
139
        ProductInterface $product,
140
        SharedStorageInterface $sharedStorage,
141
        ProductVariantInterface $variant,
142
        ObjectManager $objectManager
143
    ) {
144
        $sharedStorage->get('order')->willReturn($order);
145
146
        $orderItemFactory->createNew()->willReturn($item);
147
148
        $product->getMasterVariant()->willReturn($variant);
149
        $product->getPrice()->willReturn(1234);
150
151
        $itemQuantityModifier->modify($item, 1)->shouldBeCalled();
152
153
        $item->setVariant($variant)->shouldBeCalled();
154
        $item->setUnitPrice(1234)->shouldBeCalled();
155
156
        $order->addItem($item)->shouldBeCalled();
157
158
        $objectManager->flush()->shouldBeCalled();
159
160
        $this->theCustomerBoughtSingle($product);
161
    }
162
}
163