Completed
Push — master ( 6c4da9...442164 )
by Florian
14s queued 11s
created

MethodListTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 48
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 48
rs 9.125
c 2
b 0
f 0
cc 1
eloc 37
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\Test\Unit\PayoneObjectManager;
41
use Payone\Core\Model\ResourceModel\PaymentBan;
42
43
class MethodListTest extends BaseTestCase
44
{
45
    /**
46
     * @var ClassToTest
47
     */
48
    private $classToTest;
49
50
    /**
51
     * @var ObjectManager|PayoneObjectManager
52
     */
53
    private $objectManager;
54
55
    /**
56
     * @var Consumerscore|\PHPUnit_Framework_MockObject_MockObject
57
     */
58
    private $consumerscore;
59
60
    /**
61
     * @var ConsumerscoreHelper|\PHPUnit_Framework_MockObject_MockObject
62
     */
63
    private $consumerscoreHelper;
64
65
    /**
66
     * @var PaymentBan|\PHPUnit_Framework_MockObject_MockObject
67
     */
68
    private $paymentBan;
69
70
    /**
71
     * @var Quote|\PHPUnit_Framework_MockObject_MockObject
72
     */
73
    private $quote;
74
75
    protected function setUp()
76
    {
77
        $this->objectManager = $this->getObjectManager();
78
79
        $this->consumerscore = $this->getMockBuilder(Consumerscore::class)->disableOriginalConstructor()->getMock();
80
        $this->consumerscoreHelper = $this->getMockBuilder(ConsumerscoreHelper::class)->disableOriginalConstructor()->getMock();
81
        $this->consumerscoreHelper->method('isCreditratingNeeded')->willReturn(true);
82
        $this->consumerscoreHelper->method('getConfigParam')->willReturn(true);
83
        $this->consumerscoreHelper->expects($this->any())
84
            ->method('getAllowedMethodsForScore')
85
            ->willReturnMap([
86
                    ['Y', [PayoneConfig::METHOD_CREDITCARD, PayoneConfig::METHOD_ADVANCE_PAYMENT]],
87
                    ['R', [PayoneConfig::METHOD_DEBIT, PayoneConfig::METHOD_CASH_ON_DELIVERY]]
88
                ]);
89
90
91
        $address = $this->getMockBuilder(Address::class)
92
            ->disableOriginalConstructor()
93
            ->setMethods(['getPayoneAddresscheckScore', 'setPayoneProtectScore', 'getPayoneProtectScore', 'save'])
94
            ->getMock();
95
        $address->method('getPayoneAddresscheckScore')->willReturn('Y');
96
        $address->method('getPayoneProtectScore')->willReturn('Y');
97
        $address->method('setPayoneProtectScore')->willReturn($address);
98
        $address->method('save')->willReturn($address);
99
100
        $this->quote = $this->getMockBuilder(Quote::class)
101
            ->disableOriginalConstructor()
102
            ->setMethods(['getShippingAddress', 'getGrandTotal', 'getCustomerId'])
103
            ->getMock();
104
        $this->quote->method('getShippingAddress')->willReturn($address);
105
        $this->quote->method('getGrandTotal')->willReturn(100.00);
106
107
        $checkoutSession = $this->getMockBuilder(Session::class)
108
            ->disableOriginalConstructor()
109
            ->setMethods(['getQuote', 'getPayonePaymentBans'])
110
            ->getMock();
111
        $checkoutSession->method('getQuote')->willReturn($this->quote);
112
        $checkoutSession->method('getPayonePaymentBans')->willReturn([PayoneConfig::METHOD_DEBIT => '2100-01-01 12:00:00']);
113
114
        $this->paymentBan = $this->getMockBuilder(PaymentBan::class)->disableOriginalConstructor()->getMock();
115
116
        $this->classToTest = $this->objectManager->getObject(ClassToTest::class, [
117
            'consumerscore' => $this->consumerscore,
118
            'consumerscoreHelper' => $this->consumerscoreHelper,
119
            'checkoutSession' => $checkoutSession,
120
            'paymentBan' => $this->paymentBan
121
        ]);
122
    }
123
124
    public function testAfterGetAvailableMethodsPreviousCheck()
125
    {
126
        $this->consumerscore->method('sendRequest')->willReturn(true);
127
        $this->consumerscoreHelper->method('getWorstScore')->willReturn('Y');
128
129
        $subject = $this->getMockBuilder(MethodList::class)->disableOriginalConstructor()->getMock();
130
131
        $payment = $this->getMockBuilder(MethodInterface::class)->disableOriginalConstructor()->getMock();
132
        $payment->method('getCode')->willReturn(PayoneConfig::METHOD_DEBIT);
133
        $paymentMethods = [$payment];
134
135
        $this->quote->method('getCustomerId')->willReturn('5');
136
        $this->paymentBan->method('getPaymentBans')->willReturn([]);
137
138
        $result = $this->classToTest->afterGetAvailableMethods($subject, $paymentMethods);
139
        $this->assertInstanceOf(MethodInterface::class, $result[0]);
140
    }
141
142 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...
143
    {
144
        $this->consumerscore->method('sendRequest')->willReturn(['score' => 'Y']);
145
        $this->consumerscoreHelper->method('getWorstScore')->willReturn('R');
146
147
        $subject = $this->getMockBuilder(MethodList::class)->disableOriginalConstructor()->getMock();
148
149
        $payment = $this->getMockBuilder(MethodInterface::class)->disableOriginalConstructor()->getMock();
150
        $payment->method('getCode')->willReturn(PayoneConfig::METHOD_CASH_ON_DELIVERY);
151
        $paymentMethods = [$payment];
152
153
        $this->quote->method('getCustomerId')->willReturn('5');
154
        $this->paymentBan->method('getPaymentBans')->willReturn([]);
155
156
        $result = $this->classToTest->afterGetAvailableMethods($subject, $paymentMethods);
157
        $this->assertInstanceOf(MethodInterface::class, $result[0]);
158
    }
159
160 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...
161
    {
162
        $this->consumerscore->method('sendRequest')->willReturn(['score' => 'Y']);
163
        $this->consumerscoreHelper->method('getWorstScore')->willReturn('R');
164
165
        $subject = $this->getMockBuilder(MethodList::class)->disableOriginalConstructor()->getMock();
166
167
        $payment = $this->getMockBuilder(MethodInterface::class)->disableOriginalConstructor()->getMock();
168
        $payment->method('getCode')->willReturn(PayoneConfig::METHOD_BARZAHLEN);
169
        $paymentMethods = [$payment];
170
171
        $this->quote->method('getCustomerId')->willReturn('5');
172
        $this->paymentBan->method('getPaymentBans')->willReturn([]);
173
174
        $result = $this->classToTest->afterGetAvailableMethods($subject, $paymentMethods);
175
        $this->assertEmpty($result);
176
    }
177
178
    public function testAfterGetAvailableMethodsBanRegistered()
179
    {
180
        $this->consumerscore->method('sendRequest')->willReturn(true);
181
        $this->consumerscoreHelper->method('getWorstScore')->willReturn('Y');
182
183
        $subject = $this->getMockBuilder(MethodList::class)->disableOriginalConstructor()->getMock();
184
185
        $payment = $this->getMockBuilder(MethodInterface::class)->disableOriginalConstructor()->getMock();
186
        $payment->method('getCode')->willReturn(PayoneConfig::METHOD_DEBIT);
187
        $paymentMethods = [$payment];
188
189
        $this->quote->method('getCustomerId')->willReturn('5');
190
        $ban = [PayoneConfig::METHOD_DEBIT => '2100-01-01 12:00:00'];
191
        $this->paymentBan->method('getPaymentBans')->willReturn($ban);
192
193
        $result = $this->classToTest->afterGetAvailableMethods($subject, $paymentMethods);
194
        $this->assertEmpty($result);
195
    }
196
197
    public function testAfterGetAvailableMethodsBanGuest()
198
    {
199
        $this->consumerscore->method('sendRequest')->willReturn(true);
200
        $this->consumerscoreHelper->method('getWorstScore')->willReturn('Y');
201
202
        $subject = $this->getMockBuilder(MethodList::class)->disableOriginalConstructor()->getMock();
203
204
        $payment = $this->getMockBuilder(MethodInterface::class)->disableOriginalConstructor()->getMock();
205
        $payment->method('getCode')->willReturn(PayoneConfig::METHOD_DEBIT);
206
        $paymentMethods = [$payment];
207
208
        $this->quote->method('getCustomerId')->willReturn(null);
209
210
        $result = $this->classToTest->afterGetAvailableMethods($subject, $paymentMethods);
211
        $this->assertEmpty($result);
212
    }
213
}
214