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

CheckoutSubmitBeforeTest::testExecuteNoQuote()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 8
Ratio 100 %

Importance

Changes 0
Metric Value
dl 8
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 0
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\Model\PayoneConfig;
30
use Payone\Core\Observer\CheckoutSubmitBefore as ClassToTest;
31
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
32
use Payone\Core\Model\Api\Request\Consumerscore;
33
use Payone\Core\Helper\Consumerscore as ConsumerscoreHelper;
34
use Magento\Quote\Model\Quote;
35
use Magento\Quote\Model\Quote\Address;
36
use Magento\Sales\Model\Order\Payment;
37
use Magento\Payment\Model\MethodInterface;
38
use Magento\Payment\Model\InfoInterface;
39
use Magento\Framework\Exception\LocalizedException;
40
use Magento\Framework\Event\Observer;
41
use Magento\Store\Model\ScopeInterface;
42
use Payone\Core\Test\Unit\BaseTestCase;
43
use Payone\Core\Model\Test\PayoneObjectManager;
44
45
class CheckoutSubmitBeforeTest 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 Consumerscore|\PHPUnit_Framework_MockObject_MockObject
59
     */
60
    private $consumerscore;
61
62
    /**
63
     * @var ConsumerscoreHelper|\PHPUnit_Framework_MockObject_MockObject
64
     */
65
    private $consumerscoreHelper;
66
67
    protected function setUp()
68
    {
69
        $this->objectManager = $this->getObjectManager();
70
71
        $this->consumerscore = $this->getMockBuilder(Consumerscore::class)->disableOriginalConstructor()->getMock();
72
        $this->consumerscoreHelper = $this->getMockBuilder(ConsumerscoreHelper::class)->disableOriginalConstructor()->getMock();
73
74
        $this->classToTest = $this->objectManager->getObject(ClassToTest::class, [
75
            'consumerscore' => $this->consumerscore,
76
            'consumerscoreHelper' => $this->consumerscoreHelper
77
        ]);
78
    }
79
80
    public function testCreditratingNotNeeded()
81
    {
82
        $this->consumerscoreHelper->method('isCreditratingNeeded')->willReturn(false);
83
84
        $quote = $this->getMockBuilder(Quote::class)
85
            ->disableOriginalConstructor()
86
            ->setMethods(['getGrandTotal'])
87
            ->getMock();
88
        $quote->method('getGrandTotal')->willReturn(123.45);
89
90
        $result = $this->classToTest->isCreditratingNeeded($quote);
91
        $this->assertFalse($result);
92
    }
93
94
    public function testIsCreditratingNeededPaymentTypeMissing()
95
    {
96
        $this->consumerscoreHelper->method('isCreditratingNeeded')->willReturn(true);
97
        $this->consumerscoreHelper->method('getConfigParam')->willReturn(PayoneConfig::METHOD_CREDITCARD.','.PayoneConfig::METHOD_DEBIT);
98
99
        $paymentMethod = $this->getMockBuilder(MethodInterface::class)->disableOriginalConstructor()->getMock();
100
        $paymentMethod->method('getCode')->willReturn(PayoneConfig::METHOD_PAYPAL);
101
102
        $payment = $this->getMockBuilder(Payment::class)->disableOriginalConstructor()->getMock();
103
        $payment->method('getMethodInstance')->willReturn($paymentMethod);
104
105
        $quote = $this->getMockBuilder(Quote::class)->disableOriginalConstructor()->getMock();
106
        $quote->method('getPayment')->willReturn($payment);
107
108
        $result = $this->classToTest->isCreditratingNeeded($quote);
109
        $this->assertFalse($result);
110
    }
111
112 View Code Duplication
    public function testIsCreditratingNeededNotAgreedByCustomer()
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...
113
    {
114
        $this->consumerscoreHelper->method('isCreditratingNeeded')->willReturn(true);
115
        $this->consumerscoreHelper->method('getConfigParam')->willReturn(PayoneConfig::METHOD_CREDITCARD.','.PayoneConfig::METHOD_DEBIT);
116
117
        $infoInstance = $this->getMockBuilder(InfoInterface::class)->disableOriginalConstructor()->getMock();
118
        $infoInstance->method('getAdditionalInformation')->willReturn(false);
119
120
        $paymentMethod = $this->getMockBuilder(MethodInterface::class)->disableOriginalConstructor()->getMock();
121
        $paymentMethod->method('getCode')->willReturn(PayoneConfig::METHOD_CREDITCARD);
122
        $paymentMethod->method('getInfoInstance')->willReturn($infoInstance);
123
124
        $payment = $this->getMockBuilder(Payment::class)->disableOriginalConstructor()->getMock();
125
        $payment->method('getMethodInstance')->willReturn($paymentMethod);
126
127
        $quote = $this->getMockBuilder(Quote::class)->disableOriginalConstructor()->getMock();
128
        $quote->method('getPayment')->willReturn($payment);
129
130
        $result = $this->classToTest->isCreditratingNeeded($quote);
131
        $this->assertFalse($result);
132
    }
133
134 View Code Duplication
    public function testIsCreditratingNeeded()
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...
135
    {
136
        $this->consumerscoreHelper->method('isCreditratingNeeded')->willReturn(true);
137
        $this->consumerscoreHelper->method('getConfigParam')->willReturn(PayoneConfig::METHOD_CREDITCARD.','.PayoneConfig::METHOD_DEBIT);
138
139
        $infoInstance = $this->getMockBuilder(InfoInterface::class)->disableOriginalConstructor()->getMock();
140
        $infoInstance->method('getAdditionalInformation')->willReturn(true);
141
142
        $paymentMethod = $this->getMockBuilder(MethodInterface::class)->disableOriginalConstructor()->getMock();
143
        $paymentMethod->method('getCode')->willReturn(PayoneConfig::METHOD_CREDITCARD);
144
        $paymentMethod->method('getInfoInstance')->willReturn($infoInstance);
145
146
        $payment = $this->getMockBuilder(Payment::class)->disableOriginalConstructor()->getMock();
147
        $payment->method('getMethodInstance')->willReturn($paymentMethod);
148
149
        $quote = $this->getMockBuilder(Quote::class)->disableOriginalConstructor()->getMock();
150
        $quote->method('getPayment')->willReturn($payment);
151
152
        $result = $this->classToTest->isCreditratingNeeded($quote);
153
        $this->assertTrue($result);
154
    }
155
156
    public function testIsPaymentApplicableForScoreGreen()
157
    {
158
        $quote = $this->getMockBuilder(Quote::class)->disableOriginalConstructor()->getMock();
159
160
        $result = $this->classToTest->isPaymentApplicableForScore($quote, 'G');
161
        $this->assertTrue($result);
162
    }
163
164
    public function testIsPaymentApplicableForScoreYellow()
165
    {
166
        $this->consumerscoreHelper->expects($this->any())
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Payone\Core\Helper\Consumerscore.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
167
            ->method('getAllowedMethodsForScore')
168
            ->willReturnMap([
169
                ['Y', [PayoneConfig::METHOD_ADVANCE_PAYMENT, PayoneConfig::METHOD_BILLSAFE]],
170
                ['R', [PayoneConfig::METHOD_DEBIT, PayoneConfig::METHOD_CREDITCARD]]
171
            ]);
172
173
        $paymentMethod = $this->getMockBuilder(MethodInterface::class)->disableOriginalConstructor()->getMock();
174
        $paymentMethod->method('getCode')->willReturn(PayoneConfig::METHOD_CREDITCARD);
175
176
        $payment = $this->getMockBuilder(Payment::class)->disableOriginalConstructor()->getMock();
177
        $payment->method('getMethodInstance')->willReturn($paymentMethod);
178
179
        $quote = $this->getMockBuilder(Quote::class)->disableOriginalConstructor()->getMock();
180
        $quote->method('getPayment')->willReturn($payment);
181
182
        $result = $this->classToTest->isPaymentApplicableForScore($quote, 'Y');
183
        $this->assertTrue($result);
184
185
        $result = $this->classToTest->isPaymentApplicableForScore($quote, 'R');
186
        $this->assertTrue($result);
187
188
        $result = $this->classToTest->isPaymentApplicableForScore($quote, 'X');
189
        $this->assertFalse($result);
190
    }
191
192
    public function testCheckoutNeedsToBeStopped()
193
    {
194
        $this->consumerscoreHelper->method('getConfigParam')->willReturn('stop_checkout');
195
196
        $result = $this->classToTest->checkoutNeedsToBeStopped(['status' => 'VALID']);
197
        $this->assertFalse($result);
198
199
        $result = $this->classToTest->checkoutNeedsToBeStopped(['status' => 'ERROR']);
200
        $this->assertTrue($result);
201
    }
202
203 View Code Duplication
    public function testGetScoreByCreditrating()
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...
204
    {
205
        $expected = 'G';
206
207
        $this->consumerscore->method('sendRequest')->willReturn(true);
208
209
        $address = $this->getMockBuilder(Address::class)->disableOriginalConstructor()->setMethods(['getPayoneProtectScore'])->getMock();
210
        $address->method('getPayoneProtectScore')->willReturn($expected);
211
212
        $result = $this->classToTest->getScoreByCreditrating($address);
213
        $this->assertEquals($expected, $result);
214
    }
215
216
    public function testGetScoreByCreditratingScoreIsset()
217
    {
218
        $expected = 'G';
219
220
        $this->consumerscore->method('sendRequest')->willReturn(['status' => 'VALID', 'score' => $expected]);
221
222
        $address = $this->getMockBuilder(Address::class)
223
            ->disableOriginalConstructor()
224
            ->setMethods(['getPayoneProtectScore', 'setPayoneProtectScore', 'save'])
225
            ->getMock();
226
        $address->method('getPayoneProtectScore')->willReturn($expected);
227
        $address->method('setPayoneProtectScore')->willReturn($address);
228
229
        $result = $this->classToTest->getScoreByCreditrating($address);
230
        $this->assertEquals($expected, $result);
231
    }
232
233
    public function testGetScoreByCreditratingException()
234
    {
235
        $this->consumerscoreHelper->method('getConfigParam')->willReturn(null);
236
        $this->consumerscore->method('sendRequest')->willReturn(false);
237
238
        $address = $this->getMockBuilder(Address::class)->disableOriginalConstructor()->getMock();
239
240
        $this->expectException(LocalizedException::class);
241
        $this->classToTest->getScoreByCreditrating($address);
242
    }
243
244
    /**
245
     * @return \PHPUnit_Framework_MockObject_MockObject
246
     */
247
    private function getExecuteObserver()
248
    {
249
        $address = $this->getMockBuilder(Address::class)
250
            ->disableOriginalConstructor()
251
            ->setMethods(['getPayoneAddresscheckScore', 'getPayoneProtectScore', 'setPayoneProtectScore', 'save'])
252
            ->getMock();
253
        $address->method('getPayoneAddresscheckScore')->willReturn('G');
254
        $address->method('setPayoneProtectScore')->willReturn($address);
255
256
        $infoInstance = $this->getMockBuilder(InfoInterface::class)->disableOriginalConstructor()->getMock();
257
        $infoInstance->method('getAdditionalInformation')->willReturn(true);
258
259
        $paymentMethod = $this->getMockBuilder(MethodInterface::class)->disableOriginalConstructor()->getMock();
260
        $paymentMethod->method('getCode')->willReturn(PayoneConfig::METHOD_CREDITCARD);
261
        $paymentMethod->method('getInfoInstance')->willReturn($infoInstance);
262
263
        $payment = $this->getMockBuilder(Payment::class)->disableOriginalConstructor()->getMock();
264
        $payment->method('getMethodInstance')->willReturn($paymentMethod);
265
266
        $quote = $this->getMockBuilder(Quote::class)->disableOriginalConstructor()->getMock();
267
        $quote->method('getBillingAddress')->willReturn($address);
268
        $quote->method('getShippingAddress')->willReturn($address);
269
        $quote->method('getPayment')->willReturn($payment);
270
271
        $observer = $this->getMockBuilder(Observer::class)->disableOriginalConstructor()->setMethods(['getQuote'])->getMock();
272
        $observer->method('getQuote')->willReturn($quote);
273
274
        return $observer;
275
    }
276
277
    public function testExecute()
278
    {
279
        $this->consumerscore->method('sendRequest')->willReturn(['status' => 'VALID', 'score' => 'G']);
280
        $this->consumerscoreHelper->method('isCreditratingNeeded')->willReturn(true);
281
        $this->consumerscoreHelper->method('getWorstScore')->willReturn('G');
282
        $this->consumerscoreHelper->expects($this->any())
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Payone\Core\Helper\Consumerscore.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
283
            ->method('getConfigParam')
284
            ->willReturnMap(
285
                [
286
                    ['enabled', 'address_check', 'payone_protect', null, true],
287
                    ['enabled_for_payment_methods', 'creditrating', 'payone_protect', null, PayoneConfig::METHOD_CREDITCARD.','.PayoneConfig::METHOD_DEBIT]
288
                ]
289
            );
290
291
        $observer = $this->getExecuteObserver();
292
293
        $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\Che...SubmitBefore::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...
294
        $this->assertNull($result);
295
    }
296
297 View Code Duplication
    public function testExecuteNoQuote()
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...
298
    {
299
        $observer = $this->getMockBuilder(Observer::class)->disableOriginalConstructor()->setMethods(['getQuote'])->getMock();
300
        $observer->method('getQuote')->willReturn(null);
301
302
        $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\Che...SubmitBefore::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...
303
        $this->assertNull($result);
304
    }
305
306
    public function testExecuteException()
307
    {
308
        $this->consumerscore->method('sendRequest')->willReturn(['status' => 'VALID', 'score' => 'G']);
309
        $this->consumerscoreHelper->method('isCreditratingNeeded')->willReturn(true);
310
        $this->consumerscoreHelper->method('getWorstScore')->willReturn('X');
311
        $this->consumerscoreHelper->expects($this->any())
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Payone\Core\Helper\Consumerscore.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
312
            ->method('getConfigParam')
313
            ->willReturnMap(
314
                [
315
                    ['enabled', 'address_check', 'payone_protect', null, true],
316
                    ['enabled_for_payment_methods', 'creditrating', 'payone_protect', null, PayoneConfig::METHOD_CREDITCARD.','.PayoneConfig::METHOD_DEBIT]
317
                ]
318
            );
319
        $this->consumerscoreHelper->expects($this->any())
320
            ->method('getAllowedMethodsForScore')
321
            ->willReturnMap([
322
                ['Y', [PayoneConfig::METHOD_ADVANCE_PAYMENT, PayoneConfig::METHOD_BILLSAFE]],
323
                ['R', [PayoneConfig::METHOD_DEBIT, PayoneConfig::METHOD_CREDITCARD]]
324
            ]);
325
326
        $observer = $this->getExecuteObserver();
327
328
        $this->expectException(LocalizedException::class);
329
        $this->classToTest->execute($observer);
330
    }
331
}
332