Completed
Push — master ( bcb54c...8151ca )
by Kamil
05:51 queued 19s
created

ConvertPaymentActionSpec::it_executes_request()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 74

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 74
rs 8.5672
c 0
b 0
f 0
cc 1
nc 1
nop 9

How to fix   Long Method    Many Parameters   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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
declare(strict_types=1);
13
14
namespace spec\Sylius\Bundle\PayumBundle\Action\Paypal\ExpressCheckout;
15
16
use Doctrine\Common\Collections\ArrayCollection;
17
use Payum\Core\Action\ActionInterface;
18
use Payum\Core\Exception\RequestNotSupportedException;
19
use Payum\Core\Request\Convert;
20
use PhpSpec\ObjectBehavior;
21
use Sylius\Component\Core\Model\AddressInterface;
22
use Sylius\Component\Core\Model\AdjustmentInterface;
23
use Sylius\Component\Core\Model\CustomerInterface;
24
use Sylius\Component\Core\Model\OrderInterface;
25
use Sylius\Component\Core\Model\OrderItemInterface;
26
use Sylius\Component\Core\Model\PaymentInterface;
27
use Sylius\Component\Core\Model\ProductInterface;
28
use Sylius\Component\Core\Model\ProductVariantInterface;
29
use Sylius\Component\Core\Payment\InvoiceNumberGeneratorInterface;
30
31
final class ConvertPaymentActionSpec extends ObjectBehavior
32
{
33
    function let(InvoiceNumberGeneratorInterface $invoiceNumberGenerator): void
34
    {
35
        $this->beConstructedWith($invoiceNumberGenerator);
36
    }
37
38
    function it_implements_action_interface(): void
39
    {
40
        $this->shouldImplement(ActionInterface::class);
41
    }
42
43
    function it_executes_request(
44
        InvoiceNumberGeneratorInterface $invoiceNumberGenerator,
45
        Convert $request,
46
        PaymentInterface $payment,
47
        OrderInterface $order,
48
        OrderItemInterface $orderItem,
49
        ProductVariantInterface $productVariant,
50
        ProductInterface $product,
51
        CustomerInterface $customer,
52
        AddressInterface $billingAddress
53
    ): void {
54
        $request->getTo()->willReturn('array');
55
56
        $payment->getId()->willReturn(19);
57
58
        $order->getId()->willReturn(92);
59
        $order->getId()->willReturn(92);
60
        $order->getCurrencyCode()->willReturn('PLN');
61
        $order->getTotal()->willReturn(88000);
62
        $order->getItems()->willReturn(new ArrayCollection([$orderItem->getWrappedObject()]));
63
        $order->getAdjustmentsTotalRecursively(AdjustmentInterface::TAX_ADJUSTMENT)->willReturn(0);
64
        $order->getOrderPromotionTotal()->willReturn(0);
65
        $order->getShippingTotal()->willReturn(8000);
66
67
        $orderItem->getVariant()->willReturn($productVariant);
68
        $orderItem->getDiscountedUnitPrice()->willReturn(80000);
69
        $orderItem->getQuantity()->willReturn(1);
70
71
        $productVariant->getProduct()->willReturn($product);
72
73
        $product->getName()->willReturn('Lamborghini Aventador Model');
74
75
        $order->getCustomer()->willReturn($customer);
76
        $customer->getEmail()->willReturn('[email protected]');
77
78
        $order->getBillingAddress()->willReturn($billingAddress);
79
        $billingAddress->getCountryCode()->willReturn('US');
80
        $billingAddress->getFullName()->willReturn('John Doe');
81
        $billingAddress->getStreet()->willReturn('Main St. 123');
82
        $billingAddress->getCity()->willReturn('New York');
83
        $billingAddress->getPostcode()->willReturn('20500');
84
        $billingAddress->getPhoneNumber()->willReturn('888222111');
85
        $billingAddress->getProvinceCode()->willReturn('NY');
86
87
        $request->getSource()->willReturn($payment);
88
        $payment->getOrder()->willReturn($order);
89
90
        $invoiceNumberGenerator->generate($order, $payment)->willReturn('19-92');
91
        $details = [
92
            'PAYMENTREQUEST_0_INVNUM' => '19-92',
93
            'PAYMENTREQUEST_0_CURRENCYCODE' => 'PLN',
94
            'PAYMENTREQUEST_0_AMT' => 880.00,
95
            'PAYMENTREQUEST_0_ITEMAMT' => 880.00,
96
            'EMAIL' => '[email protected]',
97
            'LOCALECODE' => 'US',
98
            'PAYMENTREQUEST_0_SHIPTONAME' => 'John Doe',
99
            'PAYMENTREQUEST_0_SHIPTOSTREET' => 'Main St. 123',
100
            'PAYMENTREQUEST_0_SHIPTOCITY' => 'New York',
101
            'PAYMENTREQUEST_0_SHIPTOZIP' => '20500',
102
            'PAYMENTREQUEST_0_SHIPTOCOUNTRYCODE' => 'US',
103
            'PAYMENTREQUEST_0_SHIPTOPHONENUM' => '888222111',
104
            'PAYMENTREQUEST_0_SHIPTOSTATE' => 'NY',
105
            'L_PAYMENTREQUEST_0_NAME0' => 'Lamborghini Aventador Model',
106
            'L_PAYMENTREQUEST_0_AMT0' => 800.00,
107
            'L_PAYMENTREQUEST_0_QTY0' => 1,
108
            'L_PAYMENTREQUEST_0_NAME1' => 'Shipping Total',
109
            'L_PAYMENTREQUEST_0_AMT1' => 80.00,
110
            'L_PAYMENTREQUEST_0_QTY1' => 1,
111
        ];
112
113
        $request->setResult($details)->shouldBeCalled();
114
115
        $this->execute($request);
116
    }
117
118
    function it_throws_exception_when_source_is_not_a_payment_interface(Convert $request): void
119
    {
120
        $request->getSource()->willReturn(null);
121
122
        $this
123
            ->shouldThrow(RequestNotSupportedException::class)
124
            ->during('execute', [$request])
125
        ;
126
    }
127
}
128