Completed
Push — master ( fe4d16...77dac1 )
by Florian
26:03
created

ToolkitTest::testIsKeyValid()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 23
rs 9.0856
cc 1
eloc 16
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\Helper;
28
29
use Payone\Core\Helper\Toolkit;
30
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
31
use Magento\Store\Model\StoreManagerInterface;
32
use Magento\Store\Api\Data\StoreInterface;
33
use Magento\Framework\App\Helper\Context;
34
use Magento\Store\Model\ScopeInterface;
35
use Magento\Framework\App\Config\ScopeConfigInterface;
36
use Payone\Core\Helper\Payment;
37
use Payone\Core\Helper\Shop;
38
use Payone\Core\Model\PayoneConfig;
39
use Magento\Sales\Model\Order;
40
use Payone\Core\Model\Methods\PayoneMethod;
41
use Magento\Framework\DataObject;
42
43
class ToolkitTest extends \PHPUnit_Framework_TestCase
44
{
45
    /**
46
     * @var ObjectManager
47
     */
48
    private $objectManager;
49
50
    /**
51
     * @var Toolkit
52
     */
53
    private $toolkit;
54
55
    /**
56
     * @var ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject
57
     */
58
    private $scopeConfig;
59
60
    /**
61
     * @var Shop|\PHPUnit_Framework_MockObject_MockObject
62
     */
63
    private $shopHelper;
64
65
    protected function setUp()
66
    {
67
        $this->objectManager = new ObjectManager($this);
68
69
        $this->scopeConfig = $this->getMockBuilder(ScopeConfigInterface::class)->disableOriginalConstructor()->getMock();
70
        $context = $this->objectManager->getObject(Context::class, ['scopeConfig' => $this->scopeConfig]);
71
72
        $store = $this->getMockBuilder(StoreInterface::class)->disableOriginalConstructor()->getMock();
73
        $store->method('getCode')->willReturn(null);
74
75
        $storeManager = $this->getMockBuilder(StoreManagerInterface::class)->disableOriginalConstructor()->getMock();
76
        $storeManager->method('getStore')->willReturn($store);
77
        $storeManager->method('getStores')->willReturn(['de' => $store, 'en' => $store, 'fr' => $store, 'nl' => $store]);
78
79
        $paymentHelper = $this->objectManager->getObject(Payment::class);
80
        $this->shopHelper = $this->getMockBuilder(Shop::class)->disableOriginalConstructor()->getMock();
81
82
        $this->toolkit = $this->objectManager->getObject(Toolkit::class, [
83
            'context' => $context,
84
            'storeManager' => $storeManager,
85
            'paymentHelper' => $paymentHelper,
86
            'shopHelper' => $this->shopHelper
87
        ]);
88
    }
89
90
    public function testGetAllPayoneSecurityKeys()
91
    {
92
        $this->scopeConfig->expects($this->any())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Magento\Framework...g\ScopeConfigInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
93
            ->method('getValue')
94
            ->willReturnMap(
95
                [
96
                    ['payone_general/global/key', ScopeInterface::SCOPE_STORE, 'de', '12345'],
97
                    ['payone_general/global/key', ScopeInterface::SCOPE_STORE, 'en', '23456'],
98
                    ['payone_general/global/key', ScopeInterface::SCOPE_STORE, 'fr', '12345'],
99
                    ['payone_general/global/key', ScopeInterface::SCOPE_STORE, 'nl', '34567'],
100
                    ['payone_payment/'.PayoneConfig::METHOD_CREDITCARD.'/use_global', ScopeInterface::SCOPE_STORE, 'nl', '0'],
101
                    ['payone_payment/'.PayoneConfig::METHOD_CREDITCARD.'/key', ScopeInterface::SCOPE_STORE, 'nl', 'extra_payment_key'],
102
                ]
103
            );
104
        $result = $this->toolkit->getAllPayoneSecurityKeys();
105
        $expected = ['12345', '23456', '34567', 'extra_payment_key'];
106
        $this->assertEquals($expected, $result);
107
    }
108
109
    public function testIsKeyValid()
110
    {
111
        $key = 'extra_payment_key';
112
        $this->scopeConfig->expects($this->any())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Magento\Framework...g\ScopeConfigInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
113
            ->method('getValue')
114
            ->willReturnMap(
115
                [
116
                    ['payone_general/global/key', ScopeInterface::SCOPE_STORE, 'de', '12345'],
117
                    ['payone_general/global/key', ScopeInterface::SCOPE_STORE, 'en', '23456'],
118
                    ['payone_general/global/key', ScopeInterface::SCOPE_STORE, 'fr', '12345'],
119
                    ['payone_general/global/key', ScopeInterface::SCOPE_STORE, 'nl', '34567'],
120
                    ['payone_payment/'.PayoneConfig::METHOD_CREDITCARD.'/use_global', ScopeInterface::SCOPE_STORE, 'nl', '0'],
121
                    ['payone_payment/'.PayoneConfig::METHOD_CREDITCARD.'/key', ScopeInterface::SCOPE_STORE, 'nl', $key],
122
                ]
123
            );
124
125
        $hash = md5($key);
126
        $result = $this->toolkit->isKeyValid($hash);
127
        $this->assertTrue($result);
128
129
        $result = $this->toolkit->isKeyValid('no hash');
130
        $this->assertFalse($result);
131
    }
132
133
    public function testHandleSubstituteReplacement()
134
    {
135
        $text = 'Lets pass this {{what}}';
136
137
        $result = $this->toolkit->handleSubstituteReplacement($text, ['{{what}}' => 'test']);
0 ignored issues
show
Documentation introduced by
array('{{what}}' => 'test') is of type array<string,string,{"{{what}}":"string"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
138
        $expected = 'Lets pass this test';
139
        $this->assertEquals($expected, $result);
140
141
        $result = $this->toolkit->handleSubstituteReplacement($text, ['{{what}}' => 'test'], 4);
0 ignored issues
show
Documentation introduced by
array('{{what}}' => 'test') is of type array<string,string,{"{{what}}":"string"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
142
        $expected = 'Lets';
143
        $this->assertEquals($expected, $result);
144
    }
145
146
    public function testHandleSubstituteReplacementEmpty()
147
    {
148
        $result = $this->toolkit->handleSubstituteReplacement('', ['{{replace_with}}' => 'something_different']);
0 ignored issues
show
Documentation introduced by
array('{{replace_with}}'... 'something_different') is of type array<string,string,{"{{...lace_with}}":"string"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
149
        $expected = '';
150
        $this->assertEquals($expected, $result);
151
    }
152
153
    public function testGetInvoiceAppendix()
154
    {
155
        $text = 'New order with order-nr {{order_increment_id}}. Your customer-id is {{customer_id}}';
156
157
        $this->scopeConfig->expects($this->any())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Magento\Framework...g\ScopeConfigInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
158
            ->method('getValue')
159
            ->willReturnMap([['payone_general/invoicing/invoice_appendix', ScopeInterface::SCOPE_STORE, null, $text]]);
160
161
        $order = $this->getMockBuilder(Order::class)->disableOriginalConstructor()->getMock();
162
        $order->method('getIncrementId')->willReturn('0000000001');
163
        $order->method('getCustomerId')->willReturn('123');
164
165
        $result = $this->toolkit->getInvoiceAppendix($order);
166
        $expected = 'New order with order-nr 0000000001. Your customer-id is 123';
167
        $this->assertEquals($expected, $result);
168
    }
169
170
    public function testGetNarrativeText()
171
    {
172
        $text = '{{order_increment_id}} was replaced. You cant read this';
173
174
        $this->scopeConfig->expects($this->any())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Magento\Framework...g\ScopeConfigInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
175
            ->method('getValue')
176
            ->willReturnMap([['payone_payment/'.PayoneConfig::METHOD_CREDITCARD.'/narrative_text', ScopeInterface::SCOPE_STORE, null, $text]]);
177
178
        $order = $this->getMockBuilder(Order::class)->disableOriginalConstructor()->getMock();
179
        $order->method('getIncrementId')->willReturn('0000000001');
180
181
        $payment = $this->getMockBuilder(PayoneMethod::class)->disableOriginalConstructor()->getMock();
182
        $payment->method('getCode')->willReturn(PayoneConfig::METHOD_CREDITCARD);
183
        $payment->method('getNarrativeTextMaxLength')->willReturn(24);
184
185
        $result = $this->toolkit->getNarrativeText($order, $payment);
186
        $expected = '0000000001 was replaced.';
187
        $this->assertEquals($expected, $result);
188
    }
189
190
    public function testFormatNumber()
191
    {
192
        $result = $this->toolkit->formatNumber(192.20587);
193
        $expected = '192.21';
194
        $this->assertEquals($expected, $result);
195
196
        $result = $this->toolkit->formatNumber(192.20587, 8);
197
        $expected = '192.20587000';
198
        $this->assertEquals($expected, $result);
199
    }
200
201
    public function testIsUTF8()
202
    {
203
        $input = 'not utf-8 - ä';
204
        $result = $this->toolkit->isUTF8(utf8_decode($input));
205
        $this->assertFalse($result);
206
207
        $input = 'utf-8 äöü';
208
        $result = $this->toolkit->isUTF8(utf8_encode($input));
209
        $this->assertTrue($result);
210
    }
211
212 View Code Duplication
    public function testGetAdditionalDataEntryOld()
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...
213
    {
214
        $expected = 'value';
215
216
        $dataObject = $this->getMockBuilder(DataObject::class)->disableOriginalConstructor()->getMock();
217
        $dataObject->method('getData')->willReturn($expected);
218
219
        $this->shopHelper->method('getMagentoVersion')->willReturn('2.0.0');
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Payone\Core\Helper\Shop>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
220
221
        $result = $this->toolkit->getAdditionalDataEntry($dataObject, 'key');
222
        $this->assertEquals($expected, $result);
223
    }
224
225
    public function testGetAdditionalDataEntryNew()
226
    {
227
        $expected = 'value';
228
229
        $dataObject = $this->getMockBuilder(DataObject::class)
230
            ->disableOriginalConstructor()
231
            ->setMethods(['getAdditionalData'])
232
            ->getMock();
233
        $dataObject->method('getAdditionalData')->willReturn(['key' => $expected]);
234
235
        $this->shopHelper->method('getMagentoVersion')->willReturn('2.1.3');
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Payone\Core\Helper\Shop>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
236
237
        $result = $this->toolkit->getAdditionalDataEntry($dataObject, 'key');
238
        $this->assertEquals($expected, $result);
239
240
        $result = $this->toolkit->getAdditionalDataEntry($dataObject, 'key2');
241
        $this->assertNull($result);
242
    }
243
}
244