Completed
Push — master ( 1b1f21...9e6ce8 )
by Florian
11s
created

AppointedTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 96
Duplicated Lines 8.33 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 20 1
A testExecute() 0 22 1
B testExecuteException() 0 25 1
A testExecuteNoOrder() 8 8 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\Observer\Transactionstatus;
28
29
use Payone\Core\Observer\Transactionstatus\Appointed as ClassToTest;
30
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
31
use Magento\Framework\Event\Observer;
32
use Magento\Sales\Model\Order;
33
use Magento\Sales\Model\Order\Email\Sender\OrderSender;
34
use Payone\Core\Model\Methods\Creditcard;
35
use Magento\Sales\Model\Order\Payment;
36
use Payone\Core\Model\PayoneConfig;
37
use Magento\Sales\Model\Service\InvoiceService;
38
use Magento\Sales\Model\Order\Invoice;
39
use Payone\Core\Helper\Base;
40
use Payone\Core\Test\Unit\BaseTestCase;
41
use Payone\Core\Test\Unit\PayoneObjectManager;
42
43
class AppointedTest extends BaseTestCase
44
{
45
    /**
46
     * @var ClassToTest
47
     */
48
    private $classToTest;
49
50
    /**
51
     * @var ObjectManager|PayoneObjectManager
52
     */
53
    private $objectManager;
54
55
    /**
56
     * @var OrderSender|\PHPUnit_Framework_MockObject_MockObject
57
     */
58
    private $orderSender;
59
60
    protected function setUp()
61
    {
62
        $this->objectManager = $this->getObjectManager();
63
64
        $this->orderSender = $this->getMockBuilder(OrderSender::class)->disableOriginalConstructor()->getMock();
65
66
        $invoice = $this->getMockBuilder(Invoice::class)->disableOriginalConstructor()->getMock();
67
68
        $invoiceService = $this->getMockBuilder(InvoiceService::class)->disableOriginalConstructor()->getMock();
69
        $invoiceService->method('prepareInvoice')->willReturn($invoice);
70
71
        $baseHelper = $this->getMockBuilder(Base::class)->disableOriginalConstructor()->getMock();
72
        $baseHelper->method('getConfigParam')->willReturn('1');
73
74
        $this->classToTest = $this->objectManager->getObject(ClassToTest::class, [
75
            'orderSender' => $this->orderSender,
76
            'invoiceService' => $invoiceService,
77
            'baseHelper' => $baseHelper
78
        ]);
79
    }
80
81
    public function testExecute()
82
    {
83
        $method = $this->getMockBuilder(Creditcard::class)->disableOriginalConstructor()->getMock();
84
        $method->method('getCode')->willReturn(PayoneConfig::METHOD_CREDITCARD);
85
86
        $payment = $this->getMockBuilder(Payment::class)->disableOriginalConstructor()->getMock();
87
        $payment->method('getLastTransId')->willReturn('123');
88
        $payment->method('getMethodInstance')->willReturn($method);
89
90
        $order = $this->getMockBuilder(Order::class)
91
            ->disableOriginalConstructor()
92
            ->setMethods(['getPayment', 'getEmailSent', 'getPayoneAuthmode', 'save'])
93
            ->getMock();
94
        $order->method('getPayment')->willReturn($payment);
95
        $order->method('getPayoneAuthmode')->willReturn('authorization');
96
97
        $observer = $this->getMockBuilder(Observer::class)->disableOriginalConstructor()->setMethods(['getOrder'])->getMock();
98
        $observer->method('getOrder')->willReturn($order);
99
100
        $result = $this->classToTest->execute($observer);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $result is correct as $this->classToTest->execute($observer) (which targets Payone\Core\Observer\Tra...us\Appointed::execute()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
101
        $this->assertNull($result);
102
    }
103
104
    public function testExecuteException()
105
    {
106
        $exception = new \Exception();
107
        $this->orderSender->method('send')->willThrowException($exception);
108
109
        $method = $this->getMockBuilder(Creditcard::class)->disableOriginalConstructor()->getMock();
110
        $method->method('getCode')->willReturn(PayoneConfig::METHOD_ADVANCE_PAYMENT);
111
112
        $payment = $this->getMockBuilder(Payment::class)->disableOriginalConstructor()->getMock();
113
        $payment->method('getLastTransId')->willReturn('123');
114
        $payment->method('getMethodInstance')->willReturn($method);
115
116
        $order = $this->getMockBuilder(Order::class)
117
            ->disableOriginalConstructor()
118
            ->setMethods(['getPayment', 'getEmailSent', 'getPayoneAuthmode', 'save'])
119
            ->getMock();
120
        $order->method('getPayment')->willReturn($payment);
121
        $order->method('getPayoneAuthmode')->willReturn('authorization');
122
123
        $observer = $this->getMockBuilder(Observer::class)->disableOriginalConstructor()->setMethods(['getOrder'])->getMock();
124
        $observer->method('getOrder')->willReturn($order);
125
126
        $result = $this->classToTest->execute($observer);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $result is correct as $this->classToTest->execute($observer) (which targets Payone\Core\Observer\Tra...us\Appointed::execute()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
127
        $this->assertNull($result);
128
    }
129
130 View Code Duplication
    public function testExecuteNoOrder()
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...
131
    {
132
        $observer = $this->getMockBuilder(Observer::class)->disableOriginalConstructor()->setMethods(['getOrder'])->getMock();
133
        $observer->method('getOrder')->willReturn(null);
134
135
        $result = $this->classToTest->execute($observer);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $result is correct as $this->classToTest->execute($observer) (which targets Payone\Core\Observer\Tra...us\Appointed::execute()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
136
        $this->assertNull($result);
137
    }
138
}
139