Completed
Push — master ( 99b9b3...0bcb3b )
by Kamil
18:37
created

OrderPaymentProviderSpec   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 164
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 9

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 9
dl 0
loc 164
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A let() 0 11 1
A it_is_initializable() 0 4 1
A it_implements_order_payment_provider_interface() 0 4 1
B it_provides_payment_in_configured_state_with_payment_method_from_last_cancelled_payment() 0 27 1
B it_provides_payment_in_configured_state_with_payment_method_from_last_failed_payment() 0 28 1
B it_provides_payment_in_configured_state_with_default_payment_method() 0 28 1
B it_does_not_apply_any_transition_if_target_state_is_the_same_as_new_payment() 0 32 1
A it_throws_exception_if_payment_method_cannot_be_resolved_for_provided_payment() 0 20 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\Payment\Provider;
13
14
use PhpSpec\ObjectBehavior;
15
use Prophecy\Argument;
16
use SM\Factory\FactoryInterface as StateMachineFactoryInterface;
17
use Sylius\Component\Core\Model\OrderInterface;
18
use Sylius\Component\Core\Model\PaymentInterface;
19
use Sylius\Component\Core\Model\PaymentMethodInterface;
20
use Sylius\Component\Core\Payment\Exception\NotProvidedOrderPaymentException;
21
use Sylius\Component\Core\Payment\Provider\OrderPaymentProvider;
22
use Sylius\Component\Core\Payment\Provider\OrderPaymentProviderInterface;
23
use Sylius\Component\Payment\Factory\PaymentFactoryInterface;
24
use Sylius\Component\Payment\PaymentTransitions;
25
use Sylius\Component\Payment\Resolver\DefaultPaymentMethodResolverInterface;
26
use Sylius\Component\Resource\StateMachine\StateMachineInterface;
27
28
/**
29
 * @author Mateusz Zalewski <[email protected]>
30
 */
31
final class OrderPaymentProviderSpec extends ObjectBehavior
32
{
33
    function let(
34
        DefaultPaymentMethodResolverInterface $defaultPaymentMethodResolver,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $defaultPaymentMethodResolver 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...
35
        PaymentFactoryInterface $paymentFactory,
36
        StateMachineFactoryInterface $stateMachineFactory
37
    ) {
38
        $this->beConstructedWith(
39
            $defaultPaymentMethodResolver,
40
            $paymentFactory,
41
            $stateMachineFactory
42
        );
43
    }
44
45
    function it_is_initializable()
46
    {
47
        $this->shouldHaveType(OrderPaymentProvider::class);
48
    }
49
50
    function it_implements_order_payment_provider_interface()
51
    {
52
        $this->shouldImplement(OrderPaymentProviderInterface::class);
53
    }
54
55
    function it_provides_payment_in_configured_state_with_payment_method_from_last_cancelled_payment(
56
        OrderInterface $order,
57
        PaymentFactoryInterface $paymentFactory,
58
        PaymentInterface $lastCancelledPayment,
59
        PaymentInterface $newPayment,
60
        PaymentMethodInterface $paymentMethod,
61
        StateMachineFactoryInterface $stateMachineFactory,
62
        StateMachineInterface $stateMachine
63
    ) {
64
        $order->getTotal()->willReturn(1000);
65
        $order->getCurrencyCode()->willReturn('USD');
66
        $order->getLastPayment(PaymentInterface::STATE_CANCELLED)->willReturn($lastCancelledPayment);
67
68
        $lastCancelledPayment->getMethod()->willReturn($paymentMethod);
69
70
        $paymentFactory->createWithAmountAndCurrencyCode(1000, 'USD')->willReturn($newPayment);
71
72
        $newPayment->setMethod($paymentMethod)->shouldBeCalled();
73
        $newPayment->getState()->willReturn(PaymentInterface::STATE_CART);
74
        $newPayment->setOrder($order)->shouldBeCalled();
75
76
        $stateMachineFactory->get($newPayment, PaymentTransitions::GRAPH)->willReturn($stateMachine);
77
        $stateMachine->getTransitionToState(PaymentInterface::STATE_NEW)->willReturn(PaymentTransitions::TRANSITION_CREATE);
78
        $stateMachine->apply(PaymentTransitions::TRANSITION_CREATE)->shouldBeCalled();
79
80
        $this->provideOrderPayment($order, PaymentInterface::STATE_NEW)->shouldReturn($newPayment);
81
    }
82
83
    function it_provides_payment_in_configured_state_with_payment_method_from_last_failed_payment(
84
        OrderInterface $order,
85
        PaymentFactoryInterface $paymentFactory,
86
        PaymentInterface $lastFailedPayment,
87
        PaymentInterface $newPayment,
88
        PaymentMethodInterface $paymentMethod,
89
        StateMachineFactoryInterface $stateMachineFactory,
90
        StateMachineInterface $stateMachine
91
    ) {
92
        $order->getTotal()->willReturn(1000);
93
        $order->getCurrencyCode()->willReturn('USD');
94
        $order->getLastPayment(PaymentInterface::STATE_CANCELLED)->willReturn(null);
95
        $order->getLastPayment(PaymentInterface::STATE_FAILED)->willReturn($lastFailedPayment);
96
97
        $lastFailedPayment->getMethod()->willReturn($paymentMethod);
98
99
        $paymentFactory->createWithAmountAndCurrencyCode(1000, 'USD')->willReturn($newPayment);
100
101
        $newPayment->setMethod($paymentMethod)->shouldBeCalled();
102
        $newPayment->getState()->willReturn(PaymentInterface::STATE_CART);
103
        $newPayment->setOrder($order)->shouldBeCalled();
104
105
        $stateMachineFactory->get($newPayment, PaymentTransitions::GRAPH)->willReturn($stateMachine);
106
        $stateMachine->getTransitionToState(PaymentInterface::STATE_NEW)->willReturn(PaymentTransitions::TRANSITION_CREATE);
107
        $stateMachine->apply(PaymentTransitions::TRANSITION_CREATE)->shouldBeCalled();
108
109
        $this->provideOrderPayment($order, PaymentInterface::STATE_NEW)->shouldReturn($newPayment);
110
    }
111
112
    function it_provides_payment_in_configured_state_with_default_payment_method(
113
        DefaultPaymentMethodResolverInterface $defaultPaymentMethodResolver,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $defaultPaymentMethodResolver 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...
114
        OrderInterface $order,
115
        PaymentFactoryInterface $paymentFactory,
116
        PaymentInterface $newPayment,
117
        PaymentMethodInterface $paymentMethod,
118
        StateMachineFactoryInterface $stateMachineFactory,
119
        StateMachineInterface $stateMachine
120
    ) {
121
        $order->getTotal()->willReturn(1000);
122
        $order->getCurrencyCode()->willReturn('USD');
123
        $order->getLastPayment(PaymentInterface::STATE_CANCELLED)->willReturn(null);
124
        $order->getLastPayment(PaymentInterface::STATE_FAILED)->willReturn(null);
125
126
        $paymentFactory->createWithAmountAndCurrencyCode(1000, 'USD')->willReturn($newPayment);
127
        $newPayment->setOrder($order)->shouldBeCalled();
128
129
        $defaultPaymentMethodResolver->getDefaultPaymentMethod($newPayment)->willReturn($paymentMethod);
130
131
        $newPayment->setMethod($paymentMethod)->shouldBeCalled();
132
        $newPayment->getState()->willReturn(PaymentInterface::STATE_CART);
133
134
        $stateMachineFactory->get($newPayment, PaymentTransitions::GRAPH)->willReturn($stateMachine);
135
        $stateMachine->getTransitionToState(PaymentInterface::STATE_NEW)->willReturn(PaymentTransitions::TRANSITION_CREATE);
136
        $stateMachine->apply(PaymentTransitions::TRANSITION_CREATE)->shouldBeCalled();
137
138
        $this->provideOrderPayment($order, PaymentInterface::STATE_NEW)->shouldReturn($newPayment);
139
    }
140
141
    function it_does_not_apply_any_transition_if_target_state_is_the_same_as_new_payment(
142
        DefaultPaymentMethodResolverInterface $defaultPaymentMethodResolver,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $defaultPaymentMethodResolver 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...
143
        OrderInterface $order,
144
        PaymentFactoryInterface $paymentFactory,
145
        PaymentInterface $newPayment,
146
        PaymentMethodInterface $paymentMethod,
147
        StateMachineFactoryInterface $stateMachineFactory
148
    ) {
149
        $this->beConstructedWith(
150
            $defaultPaymentMethodResolver,
151
            $paymentFactory,
152
            $stateMachineFactory,
153
            PaymentInterface::STATE_CART
154
        );
155
156
        $order->getTotal()->willReturn(1000);
157
        $order->getCurrencyCode()->willReturn('USD');
158
        $order->getLastPayment(PaymentInterface::STATE_CANCELLED)->willReturn(null);
159
        $order->getLastPayment(PaymentInterface::STATE_FAILED)->willReturn(null);
160
161
        $paymentFactory->createWithAmountAndCurrencyCode(1000, 'USD')->willReturn($newPayment);
162
        $newPayment->setOrder($order)->shouldBeCalled();
163
164
        $defaultPaymentMethodResolver->getDefaultPaymentMethod($newPayment)->willReturn($paymentMethod);
165
166
        $newPayment->setMethod($paymentMethod)->shouldBeCalled();
167
        $newPayment->getState()->willReturn(PaymentInterface::STATE_NEW);
168
169
        $stateMachineFactory->get(Argument::any())->shouldNotBeCalled();
170
171
        $this->provideOrderPayment($order, PaymentInterface::STATE_NEW)->shouldReturn($newPayment);
172
    }
173
    
174
    function it_throws_exception_if_payment_method_cannot_be_resolved_for_provided_payment(
175
        OrderInterface $order,
176
        PaymentFactoryInterface $paymentFactory,
177
        PaymentInterface $lastFailedPayment,
178
        PaymentInterface $newPayment
179
    ) {
180
        $order->getTotal()->willReturn(1000);
181
        $order->getCurrencyCode()->willReturn('USD');
182
        $order->getLastPayment(PaymentInterface::STATE_CANCELLED)->willReturn(null);
183
        $order->getLastPayment(PaymentInterface::STATE_FAILED)->willReturn($lastFailedPayment);
184
185
        $lastFailedPayment->getMethod()->willReturn(null);
186
187
        $paymentFactory->createWithAmountAndCurrencyCode(1000, 'USD')->willReturn($newPayment);
188
189
        $this
190
            ->shouldThrow(NotProvidedOrderPaymentException::class)
191
            ->during('provideOrderPayment', [$order, PaymentInterface::STATE_NEW])
192
        ;
193
    }
194
}
195