CaptureTest   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 110
Duplicated Lines 15.45 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
c 1
b 0
f 0
lcom 1
cbo 4
dl 17
loc 110
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 17 17 1
B testSendRequest() 0 41 1
B testSendRequestBase() 0 32 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/**
4
 * PAYONE Magento 2 Connector is free software: you can redistribute it and/or modify
5
 * it under the terms of the GNU Lesser General Public License as published by
6
 * the Free Software Foundation, either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * PAYONE Magento 2 Connector is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU Lesser General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU Lesser General Public License
15
 * along with PAYONE Magento 2 Connector. If not, see <http://www.gnu.org/licenses/>.
16
 *
17
 * PHP version 5
18
 *
19
 * @category  Payone
20
 * @package   Payone_Magento2_Plugin
21
 * @author    FATCHIP GmbH <[email protected]>
22
 * @copyright 2003 - 2017 Payone GmbH
23
 * @license   <http://www.gnu.org/licenses/> GNU Lesser General Public License
24
 * @link      http://www.payone.de
25
 */
26
27
namespace Payone\Core\Test\Unit\Model\Api\Request;
28
29
use Magento\Sales\Model\Order;
30
use Payone\Core\Helper\Database;
31
use Payone\Core\Helper\Shop;
32
use Payone\Core\Model\Api\Request\Capture as ClassToTest;
33
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
34
use Payone\Core\Model\Methods\PayoneMethod;
35
use Magento\Payment\Model\Info;
36
use Payone\Core\Helper\Api;
37
use Magento\Sales\Model\Order\Item;
38
use Payone\Core\Test\Unit\BaseTestCase;
39
use Payone\Core\Model\Test\PayoneObjectManager;
40
41
class CaptureTest extends BaseTestCase
42
{
43
    /**
44
     * @var ClassToTest
45
     */
46
    private $classToTest;
47
48
    /**
49
     * @var Api|\PHPUnit_Framework_MockObject_MockObject
50
     */
51
    private $apiHelper;
52
53
    /**
54
     * @var Shop|\PHPUnit_Framework_MockObject_MockObject
55
     */
56
    private $shopHelper;
57
58 View Code Duplication
    protected function setUp()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
59
    {
60
        $objectManager = $this->getObjectManager();
61
62
        $this->shopHelper = $this->getMockBuilder(Shop::class)->disableOriginalConstructor()->getMock();
63
64
        $databaseHelper = $this->getMockBuilder(Database::class)->disableOriginalConstructor()->getMock();
65
        $databaseHelper->method('getSequenceNumber')->willReturn('0');
66
67
        $this->apiHelper = $this->getMockBuilder(Api::class)->disableOriginalConstructor()->getMock();
68
69
        $this->classToTest = $objectManager->getObject(ClassToTest::class, [
70
            'shopHelper' => $this->shopHelper,
71
            'databaseHelper' => $databaseHelper,
72
            'apiHelper' => $this->apiHelper
73
        ]);
74
    }
75
76
    public function testSendRequest()
77
    {
78
        $invoice = ['items' => ['id' => 1]];
79
        $this->shopHelper->method('getRequestParameter')->willReturn($invoice);
80
81
        $payment = $this->getMockBuilder(PayoneMethod::class)->disableOriginalConstructor()->getMock();
82
        $payment->method('getOperationMode')->willReturn('test');
83
84
        $item = $this->getMockBuilder(Item::class)
85
            ->disableOriginalConstructor()
86
            ->setMethods(['getItemId', 'getProductId', 'getQtyOrdered'])
87
            ->getMock();
88
        $item->method('getItemId')->willReturn('id');
89
        $item->method('getProductId')->willReturn('sku');
90
        $item->method('getQtyOrdered')->willReturn(2);
91
92
        $item_missing = $this->getMockBuilder(Item::class)->disableOriginalConstructor()->getMock();
93
        $item_missing->method('getItemId')->willReturn('missing');
94
95
        $order = $this->getMockBuilder(Order::class)
96
            ->disableOriginalConstructor()
97
            ->setMethods(['getRealOrderId', 'getOrderCurrencyCode', 'getAllItems'])
98
            ->getMock();
99
        $order->method('getRealOrderId')->willReturn('54321');
100
        $order->method('getOrderCurrencyCode')->willReturn('EUR');
101
        $order->method('getAllItems')->willReturn([$item, $item_missing]);
102
103
        $paymentInfo = $this->getMockBuilder(Info::class)
104
            ->disableOriginalConstructor()
105
            ->setMethods(['getOrder', 'getParentTransactionId'])
106
            ->getMock();
107
        $paymentInfo->method('getOrder')->willReturn($order);
108
        $paymentInfo->method('getParentTransactionId')->willReturn('12345');
109
110
        $response = ['status' => 'APPROVED'];
111
        $this->apiHelper->method('sendApiRequest')->willReturn($response);
112
        $this->apiHelper->method('isInvoiceDataNeeded')->willReturn(true);
113
114
        $result = $this->classToTest->sendRequest($payment, $paymentInfo, 100);
115
        $this->assertArrayHasKey('status', $result);
116
    }
117
118
    public function testSendRequestBase()
119
    {
120
        $payment = $this->getMockBuilder(PayoneMethod::class)->disableOriginalConstructor()->getMock();
121
        $payment->method('getOperationMode')->willReturn('test');
122
123
        $order = $this->getMockBuilder(Order::class)
124
            ->disableOriginalConstructor()
125
            ->setMethods(['getRealOrderId', 'getOrderCurrencyCode'])
126
            ->getMock();
127
        $order->method('getRealOrderId')->willReturn('54321');
128
        $order->method('getOrderCurrencyCode')->willReturn('EUR');
129
130
        $paymentInfo = $this->getMockBuilder(Info::class)
131
            ->disableOriginalConstructor()
132
            ->setMethods(['getOrder', 'getParentTransactionId'])
133
            ->getMock();
134
        $paymentInfo->method('getOrder')->willReturn($order);
135
        $paymentInfo->method('getParentTransactionId')->willReturn('12345');
136
137
        $response = ['status' => 'APPROVED'];
138
        $this->apiHelper->method('sendApiRequest')->willReturn($response);
139
140
        $this->classToTest->addParameter('test', '', true);
141
142
        $resultMissing = $this->classToTest->getParameter('missing');
143
        $this->assertFalse($resultMissing);
0 ignored issues
show
Bug introduced by
It seems like $resultMissing defined by $this->classToTest->getParameter('missing') on line 142 can also be of type string; however, PHPUnit\Framework\Assert::assertFalse() does only seem to accept boolean, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
144
145
        $this->classToTest->removeParameter('mid');
146
147
        $result = $this->classToTest->sendRequest($payment, $paymentInfo, 100);
148
        $this->assertArrayHasKey('errormessage', $result);
149
    }
150
}
151