ReviewTest::setUp()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 58
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 58
c 1
b 0
f 0
rs 9.639
cc 1
eloc 42
nc 1
nop 0

How to fix   Long Method   

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:

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\Controller\Onepage;
28
29
use Magento\Quote\Model\Quote;
30
use Payone\Core\Controller\Onepage\Review as ClassToTest;
31
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
32
use Magento\Checkout\Model\Session;
33
use Magento\Framework\App\Action\Context;
34
use Magento\Framework\App\Request\Http;
35
use Magento\Quote\Model\Quote\Address;
36
use Magento\Framework\View\Result\PageFactory;
37
use Magento\Framework\View\Result\Page;
38
use Magento\Framework\Controller\ResultFactory;
39
use Magento\Framework\Controller\Result\Redirect;
40
use Magento\Quote\Api\Data\CartExtension;
41
use Magento\Quote\Model\ShippingAssignment;
42
use Magento\Quote\Model\Shipping;
43
use Payone\Core\Test\Unit\BaseTestCase;
44
use Payone\Core\Model\Test\PayoneObjectManager;
45
46
47
class ReviewTest extends BaseTestCase
48
{
49
    /**
50
     * @var ClassToTest
51
     */
52
    private $classToTest;
53
54
    /**
55
     * @var ObjectManager|PayoneObjectManager
56
     */
57
    private $objectManager;
58
59
    /**
60
     * @var Session|\PHPUnit_Framework_MockObject_MockObject
61
     */
62
    private $checkoutSession;
63
64
    /**
65
     * @var Redirect|\PHPUnit_Framework_MockObject_MockObject
66
     */
67
    private $request;
68
69
    protected function setUp()
70
    {
71
        $this->objectManager = $this->getObjectManager();
72
73
        $this->request = $this->getMockBuilder(Http::class)->disableOriginalConstructor()->getMock();
74
        $this->request->method('getParam')->willReturn('free');
75
76
        $redirect = $this->getMockBuilder(Redirect::class)->disableOriginalConstructor()->getMock();
77
        $redirect->method('setPath')->willReturn($redirect);
78
79
        $resultFactory = $this->getMockBuilder(ResultFactory::class)->disableOriginalConstructor()->getMock();
80
        $resultFactory->method('create')->willReturn($redirect);
81
82
        $context = $this->getMockBuilder(Context::class)->disableOriginalConstructor()->getMock();
83
        $context->method('getRequest')->willReturn($this->request);
84
        $context->method('getResultFactory')->willReturn($resultFactory);
85
86
        $address = $this->getMockBuilder(Address::class)
87
            ->disableOriginalConstructor()
88
            ->setMethods(['getShippingMethod', 'setShippingMethod'])
89
            ->getMock();
90
        $address->method('getShippingMethod')->willReturn('not_free');
91
        $address->method('setShippingMethod')->willReturn($address);
92
93
        $shipping = $this->getMockBuilder(Shipping::class)->disableOriginalConstructor()->getMock();
94
95
        $assignment = $this->getMockBuilder(ShippingAssignment::class)->disableOriginalConstructor()->getMock();
96
        $assignment->method('getShipping')->willReturn($shipping);
97
98
        $cartExtension = $this->getMockBuilder(CartExtension::class)
99
            ->disableOriginalConstructor()
100
            ->setMethods(['getShippingAssignments'])
101
            ->getMock();
102
        $cartExtension->method('getShippingAssignments')->willReturn([$assignment]);
103
104
        $quote = $this->getMockBuilder(Quote::class)->disableOriginalConstructor()->getMock();
105
        $quote->method('getBillingAddress')->willReturn($address);
106
        $quote->method('getShippingAddress')->willReturn($address);
107
        $quote->method('getIsVirtual')->willReturn(false);
108
        $quote->method('getExtensionAttributes')->willReturn($cartExtension);
109
110
        $this->checkoutSession = $this->getMockBuilder(Session::class)
111
            ->disableOriginalConstructor()
112
            ->setMethods(['getQuote', 'getPayoneWorkorderId'])
113
            ->getMock();
114
        $this->checkoutSession->method('getQuote')->willReturn($quote);
115
116
        $page = $this->getMockBuilder(Page::class)->disableOriginalConstructor()->getMock();
117
118
        $pageFactory = $this->getMockBuilder(PageFactory::class)->disableOriginalConstructor()->getMock();
119
        $pageFactory->method('create')->willReturn($page);
120
121
        $this->classToTest = $this->objectManager->getObject(ClassToTest::class, [
122
            'context' => $context,
123
            'checkoutSession' => $this->checkoutSession,
124
            'pageFactory' => $pageFactory
125
        ]);
126
    }
127
128 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...
129
    {
130
        $this->checkoutSession->method('getPayoneWorkorderId')->willReturn(null);
131
132
        $this->request->method('getBeforeForwardInfo')->willReturn(false);
133
        $result = $this->classToTest->execute();
134
        $this->assertInstanceOf(Redirect::class, $result);
135
    }
136
137 View Code Duplication
    public function testExecuteRedirect()
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...
138
    {
139
        $this->checkoutSession->method('getPayoneWorkorderId')->willReturn('12345');
140
141
        $this->request->method('getBeforeForwardInfo')->willReturn(false);
142
        $result = $this->classToTest->execute();
143
        $this->assertInstanceOf(Page::class, $result);
144
    }
145
}
146