MethodListTest::testAfterGetAvailableMethods()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 9

Duplication

Lines 14
Ratio 100 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 14
loc 14
rs 9.4285
cc 1
eloc 9
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\Model\Plugins;
28
29
use Payone\Core\Model\PayoneConfig;
30
use Payone\Core\Model\Plugins\MethodList as ClassToTest;
31
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
32
use Magento\Payment\Model\MethodList;
33
use Payone\Core\Model\Api\Request\Consumerscore;
34
use Payone\Core\Helper\Consumerscore as ConsumerscoreHelper;
35
use Magento\Checkout\Model\Session;
36
use Magento\Quote\Model\Quote;
37
use Magento\Quote\Model\Quote\Address;
38
use Magento\Payment\Model\MethodInterface;
39
use Payone\Core\Test\Unit\BaseTestCase;
40
use Payone\Core\Model\Test\PayoneObjectManager;
41
42
class MethodListTest extends BaseTestCase
43
{
44
    /**
45
     * @var ClassToTest
46
     */
47
    private $classToTest;
48
49
    /**
50
     * @var ObjectManager|PayoneObjectManager
51
     */
52
    private $objectManager;
53
54
    /**
55
     * @var Consumerscore|\PHPUnit_Framework_MockObject_MockObject
56
     */
57
    private $consumerscore;
58
59
    /**
60
     * @var ConsumerscoreHelper|\PHPUnit_Framework_MockObject_MockObject
61
     */
62
    private $consumerscoreHelper;
63
64
    protected function setUp()
65
    {
66
        $this->objectManager = $this->getObjectManager();
67
68
        $this->consumerscore = $this->getMockBuilder(Consumerscore::class)->disableOriginalConstructor()->getMock();
69
        $this->consumerscoreHelper = $this->getMockBuilder(ConsumerscoreHelper::class)->disableOriginalConstructor()->getMock();
70
        $this->consumerscoreHelper->method('isCreditratingNeeded')->willReturn(true);
71
        $this->consumerscoreHelper->method('getConfigParam')->willReturn(true);
72
        $this->consumerscoreHelper->expects($this->any())
73
            ->method('getAllowedMethodsForScore')
74
            ->willReturnMap([
75
                    ['Y', [PayoneConfig::METHOD_CREDITCARD, PayoneConfig::METHOD_ADVANCE_PAYMENT]],
76
                    ['R', [PayoneConfig::METHOD_DEBIT, PayoneConfig::METHOD_CASH_ON_DELIVERY]]
77
                ]);
78
79
80
        $address = $this->getMockBuilder(Address::class)
81
            ->disableOriginalConstructor()
82
            ->setMethods(['getPayoneAddresscheckScore', 'setPayoneProtectScore', 'getPayoneProtectScore', 'save'])
83
            ->getMock();
84
        $address->method('getPayoneAddresscheckScore')->willReturn('Y');
85
        $address->method('getPayoneProtectScore')->willReturn('Y');
86
        $address->method('setPayoneProtectScore')->willReturn($address);
87
        $address->method('save')->willReturn($address);
88
89
        $quote = $this->getMockBuilder(Quote::class)
90
            ->disableOriginalConstructor()
91
            ->setMethods(['getShippingAddress', 'getGrandTotal'])
92
            ->getMock();
93
        $quote->method('getShippingAddress')->willReturn($address);
94
        $quote->method('getGrandTotal')->willReturn(100.00);
95
96
        $checkoutSession = $this->getMockBuilder(Session::class)->disableOriginalConstructor()->getMock();
97
        $checkoutSession->method('getQuote')->willReturn($quote);
98
99
        $this->classToTest = $this->objectManager->getObject(ClassToTest::class, [
100
            'consumerscore' => $this->consumerscore,
101
            'consumerscoreHelper' => $this->consumerscoreHelper,
102
            'checkoutSession' => $checkoutSession
103
        ]);
104
    }
105
106
    public function testAfterGetAvailableMethodsPreviousCheck()
107
    {
108
        $this->consumerscore->method('sendRequest')->willReturn(true);
109
        $this->consumerscoreHelper->method('getWorstScore')->willReturn('Y');
110
111
        $subject = $this->getMockBuilder(MethodList::class)->disableOriginalConstructor()->getMock();
112
113
        $payment = $this->getMockBuilder(MethodInterface::class)->disableOriginalConstructor()->getMock();
114
        $payment->method('getCode')->willReturn(PayoneConfig::METHOD_DEBIT);
115
        $paymentMethods = [$payment];
116
117
        $result = $this->classToTest->afterGetAvailableMethods($subject, $paymentMethods);
118
        $this->assertInstanceOf(MethodInterface::class, $result[0]);
119
    }
120
121 View Code Duplication
    public function testAfterGetAvailableMethods()
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...
122
    {
123
        $this->consumerscore->method('sendRequest')->willReturn(['score' => 'Y']);
124
        $this->consumerscoreHelper->method('getWorstScore')->willReturn('R');
125
126
        $subject = $this->getMockBuilder(MethodList::class)->disableOriginalConstructor()->getMock();
127
128
        $payment = $this->getMockBuilder(MethodInterface::class)->disableOriginalConstructor()->getMock();
129
        $payment->method('getCode')->willReturn(PayoneConfig::METHOD_CASH_ON_DELIVERY);
130
        $paymentMethods = [$payment];
131
132
        $result = $this->classToTest->afterGetAvailableMethods($subject, $paymentMethods);
133
        $this->assertInstanceOf(MethodInterface::class, $result[0]);
134
    }
135
136 View Code Duplication
    public function testAfterGetAvailableMethodsEmpty()
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...
137
    {
138
        $this->consumerscore->method('sendRequest')->willReturn(['score' => 'Y']);
139
        $this->consumerscoreHelper->method('getWorstScore')->willReturn('R');
140
141
        $subject = $this->getMockBuilder(MethodList::class)->disableOriginalConstructor()->getMock();
142
143
        $payment = $this->getMockBuilder(MethodInterface::class)->disableOriginalConstructor()->getMock();
144
        $payment->method('getCode')->willReturn(PayoneConfig::METHOD_BARZAHLEN);
145
        $paymentMethods = [$payment];
146
147
        $result = $this->classToTest->afterGetAvailableMethods($subject, $paymentMethods);
148
        $this->assertEmpty($result);
149
    }
150
}
151