PlaceOrderTest   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 56 1
A testExecute() 0 6 1
A testExecuteValidation() 0 6 1
A testExecuteException() 0 9 1
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\PlaceOrder 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\Checkout\Api\AgreementsValidatorInterface;
35
use Magento\Quote\Api\CartManagementInterface;
36
use Magento\Framework\App\Console\Response;
37
use Magento\Store\App\Response\Redirect as RedirectResponse;
38
use Magento\Framework\UrlInterface;
39
use Magento\Framework\App\Request\Http;
40
use Magento\Framework\Message\ManagerInterface;
41
use Magento\Quote\Model\Quote\Address;
42
use Payone\Core\Test\Unit\BaseTestCase;
43
use Payone\Core\Model\Test\PayoneObjectManager;
44
45
class PlaceOrderTest extends BaseTestCase
46
{
47
    /**
48
     * @var ClassToTest
49
     */
50
    private $classToTest;
51
52
    /**
53
     * @var ObjectManager|PayoneObjectManager
54
     */
55
    private $objectManager;
56
57
    /**
58
     * @var Session|\PHPUnit_Framework_MockObject_MockObject
59
     */
60
    private $checkoutSession;
61
62
    /**
63
     * @var AgreementsValidatorInterface|\PHPUnit_Framework_MockObject_MockObject
64
     */
65
    private $agreementValidator;
66
67
    /**
68
     * @var CartManagementInterface|\PHPUnit_Framework_MockObject_MockObject
69
     */
70
    private $cartManagement;
71
72
    /**
73
     * @var RequestInterface|\PHPUnit_Framework_MockObject_MockObject
74
     */
75
    private $request;
76
77
    protected function setUp()
78
    {
79
        $this->objectManager = $this->getObjectManager();
80
81
        $redirectResponse = $this->getMockBuilder(Response::class)->disableOriginalConstructor()->getMock();
82
83
        $redirect = $this->getMockBuilder(RedirectResponse::class)->disableOriginalConstructor()->getMock();
84
        $redirect->method('redirect')->willReturn($redirectResponse);
85
86
        $response = $this->getMockBuilder(Response::class)
87
            ->disableOriginalConstructor()
88
            ->setMethods(['setRedirect'])
89
            ->getMock();
90
91
        $url = $this->getMockBuilder(UrlInterface::class)->disableOriginalConstructor()->getMock();
92
93
        $this->request = $this->getMockBuilder(Http::class)->disableOriginalConstructor()->setMethods(['getBeforeForwardInfo'])->getMock();
94
95
        $messageManager = $this->getMockBuilder(ManagerInterface::class)->disableOriginalConstructor()->getMock();
96
97
        $context = $this->getMockBuilder(Context::class)->disableOriginalConstructor()->getMock();
98
        $context->method('getRedirect')->willReturn($redirect);
99
        $context->method('getRequest')->willReturn($this->request);
100
        $context->method('getResponse')->willReturn($response);
101
        $context->method('getUrl')->willReturn($url);
102
        $context->method('getMessageManager')->willReturn($messageManager);
103
104
        $address = $this->getMockBuilder(Address::class)->disableOriginalConstructor()->getMock();
105
106
        $quote = $this->getMockBuilder(Quote::class)->disableOriginalConstructor()->getMock();
107
        $quote->method('getBillingAddress')->willReturn($address);
108
        $quote->method('getShippingAddress')->willReturn($address);
109
        $quote->method('getIsVirtual')->willReturn(false);
110
        $quote->method('getId')->willReturn('12345');
111
        $quote->method('setIsActive')->willReturn($quote);
112
113
        $this->checkoutSession = $this->getMockBuilder(Session::class)
114
            ->disableOriginalConstructor()
115
            ->setMethods(['getQuote', 'setLastQuoteId', 'setLastSuccessQuoteId', 'unsPayoneWorkorderId'])
116
            ->getMock();
117
        $this->checkoutSession->method('getQuote')->willReturn($quote);
118
        $this->checkoutSession->method('setLastQuoteId')->willReturn($this->checkoutSession);
119
        $this->checkoutSession->method('setLastSuccessQuoteId')->willReturn($this->checkoutSession);
120
121
        $this->agreementValidator = $this->getMockBuilder(AgreementsValidatorInterface::class)->disableOriginalConstructor()->getMock();
122
        $this->agreementValidator->method('isValid')->willReturn(false);
123
124
        $this->cartManagement = $this->getMockBuilder(CartManagementInterface::class)->disableOriginalConstructor()->getMock();
125
126
        $this->classToTest = $this->objectManager->getObject(ClassToTest::class, [
127
            'context' => $context,
128
            'checkoutSession' => $this->checkoutSession,
129
            'agreementValidator' => $this->agreementValidator,
130
            'cartManagement' => $this->cartManagement
131
        ]);
132
    }
133
134
    public function testExecute()
135
    {
136
        $this->request->method('getBeforeForwardInfo')->willReturn(false);
137
        $result = $this->classToTest->execute();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $result is correct as $this->classToTest->execute() (which targets Payone\Core\Controller\O...e\PlaceOrder::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...
138
        $this->assertNull($result);
139
    }
140
141
    public function testExecuteValidation()
142
    {
143
        $this->request->method('getBeforeForwardInfo')->willReturn([]);
144
        $result = $this->classToTest->execute();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $result is correct as $this->classToTest->execute() (which targets Payone\Core\Controller\O...e\PlaceOrder::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...
145
        $this->assertNull($result);
146
    }
147
148
    public function testExecuteException()
149
    {
150
        $exception = new \Exception();
151
        $this->cartManagement->method('placeOrder')->willThrowException($exception);
152
153
        $this->request->method('getBeforeForwardInfo')->willReturn(false);
154
        $result = $this->classToTest->execute();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $result is correct as $this->classToTest->execute() (which targets Payone\Core\Controller\O...e\PlaceOrder::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...
155
        $this->assertNull($result);
156
    }
157
}
158