Completed
Push — master ( 342d7b...50fff4 )
by Florian
13s
created

PredispatchCheckoutIndexTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 87
Duplicated Lines 31.03 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 4
lcom 1
cbo 4
dl 27
loc 87
rs 10
c 2
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
B setUp() 0 39 1
A testExecute() 7 7 1
A testExecuteLocalizedException() 10 10 1
A testExecuteException() 10 10 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;
28
29
use Payone\Core\Observer\PredispatchCheckoutIndex as ClassToTest;
30
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
31
use Magento\Framework\Event\Observer;
32
use Magento\Checkout\Model\Session;
33
use Magento\Sales\Model\OrderFactory;
34
use Magento\Sales\Model\Order;
35
use Magento\Framework\Exception\NoSuchEntityException;
36
use Payone\Core\Test\Unit\BaseTestCase;
37
use Payone\Core\Model\Test\PayoneObjectManager;
38
39
class PredispatchCheckoutIndexTest extends BaseTestCase
40
{
41
    /**
42
     * @var ClassToTest
43
     */
44
    private $classToTest;
45
46
    /**
47
     * @var ObjectManager|PayoneObjectManager
48
     */
49
    private $objectManager;
50
51
    /**
52
     * @var Session|\PHPUnit_Framework_MockObject_MockObject
53
     */
54
    private $checkoutSession;
55
56
    protected function setUp()
57
    {
58
        $this->objectManager = $this->getObjectManager();
59
60
        $this->checkoutSession = $this->getMockBuilder(Session::class)
61
            ->disableOriginalConstructor()
62
            ->setMethods([
63
                'getPayoneCustomerIsRedirected',
64
                'getLastOrderId',
65
                'restoreQuote',
66
                'unsLastQuoteId',
67
                'unsLastSuccessQuoteId',
68
                'unsLastOrderId',
69
                'unsLastRealOrderId',
70
                'unsPayoneCustomerIsRedirected',
71
                'setIsPayoneRedirectCancellation'
72
            ])
73
            ->getMock();
74
        $this->checkoutSession->method('getPayoneCustomerIsRedirected')->willReturn(true);
75
        $this->checkoutSession->method('getLastOrderId')->willReturn('123');
76
        $this->checkoutSession->method('unsLastQuoteId')->willReturn($this->checkoutSession);
77
        $this->checkoutSession->method('unsLastSuccessQuoteId')->willReturn($this->checkoutSession);
78
        $this->checkoutSession->method('unsLastOrderId')->willReturn($this->checkoutSession);
79
80
        $order = $this->getMockBuilder(Order::class)->disableOriginalConstructor()->getMock();
81
        $order->method('load')->willReturn($order);
82
        $order->method('cancel')->willReturn($order);
83
84
        $orderFactory = $this->getMockBuilder(OrderFactory::class)
85
            ->disableOriginalConstructor()
86
            ->setMethods(['create'])
87
            ->getMock();
88
        $orderFactory->method('create')->willReturn($order);
89
90
        $this->classToTest = $this->objectManager->getObject(ClassToTest::class, [
91
            'checkoutSession' => $this->checkoutSession,
92
            'orderFactory' => $orderFactory,
93
        ]);
94
    }
95
96 View Code Duplication
    public function testExecute()
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...
97
    {
98
        $observer = $this->getMockBuilder(Observer::class)->disableOriginalConstructor()->getMock();
99
100
        $result = $this->classToTest->execute($observer);
101
        $this->assertNull($result);
102
    }
103
104 View Code Duplication
    public function testExecuteLocalizedException()
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...
105
    {
106
        $exception = $this->objectManager->getObject(NoSuchEntityException::class);
107
        $this->checkoutSession->expects($this->once())->method('restoreQuote')->willThrowException($exception);
108
109
        $observer = $this->getMockBuilder(Observer::class)->disableOriginalConstructor()->getMock();
110
111
        $result = $this->classToTest->execute($observer);
112
        $this->assertNull($result);
113
    }
114
115 View Code Duplication
    public function testExecuteException()
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...
116
    {
117
        $exception = $this->objectManager->getObject(\Exception::class);
118
        $this->checkoutSession->expects($this->once())->method('restoreQuote')->willThrowException($exception);
119
120
        $observer = $this->getMockBuilder(Observer::class)->disableOriginalConstructor()->getMock();
121
122
        $result = $this->classToTest->execute($observer);
123
        $this->assertNull($result);
124
    }
125
}
126