ToolkitTest::setUp()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 24
c 1
b 0
f 0
rs 8.9713
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
use Payone\Core\Test\Unit\BaseTestCase;
43
use Payone\Core\Model\Test\PayoneObjectManager;
44
45
class ToolkitTest extends BaseTestCase
46
{
47
    /**
48
     * @var ObjectManager|PayoneObjectManager
49
     */
50
    private $objectManager;
51
52
    /**
53
     * @var Toolkit
54
     */
55
    private $toolkit;
56
57
    /**
58
     * @var ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject
59
     */
60
    private $scopeConfig;
61
62
    /**
63
     * @var Shop|\PHPUnit_Framework_MockObject_MockObject
64
     */
65
    private $shopHelper;
66
67
    protected function setUp()
68
    {
69
        $this->objectManager = $this->getObjectManager();
70
71
        $this->scopeConfig = $this->getMockBuilder(ScopeConfigInterface::class)->disableOriginalConstructor()->getMock();
72
        $context = $this->objectManager->getObject(Context::class, ['scopeConfig' => $this->scopeConfig]);
73
74
        $store = $this->getMockBuilder(StoreInterface::class)->disableOriginalConstructor()->getMock();
75
        $store->method('getCode')->willReturn(null);
76
77
        $storeManager = $this->getMockBuilder(StoreManagerInterface::class)->disableOriginalConstructor()->getMock();
78
        $storeManager->method('getStore')->willReturn($store);
79
        $storeManager->method('getStores')->willReturn(['de' => $store, 'en' => $store, 'fr' => $store, 'nl' => $store]);
80
81
        $paymentHelper = $this->objectManager->getObject(Payment::class);
82
        $this->shopHelper = $this->getMockBuilder(Shop::class)->disableOriginalConstructor()->getMock();
83
84
        $this->toolkit = $this->objectManager->getObject(Toolkit::class, [
85
            'context' => $context,
86
            'storeManager' => $storeManager,
87
            'paymentHelper' => $paymentHelper,
88
            'shopHelper' => $this->shopHelper
89
        ]);
90
    }
91
92
    public function testGetAllPayoneSecurityKeys()
93
    {
94
        $this->scopeConfig->expects($this->any())
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Magento\Framework\App\Config\ScopeConfigInterface.

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...
95
            ->method('getValue')
96
            ->willReturnMap(
97
                [
98
                    ['payone_general/global/key', ScopeInterface::SCOPE_STORE, 'de', '12345'],
99
                    ['payone_general/global/key', ScopeInterface::SCOPE_STORE, 'en', '23456'],
100
                    ['payone_general/global/key', ScopeInterface::SCOPE_STORE, 'fr', '12345'],
101
                    ['payone_general/global/key', ScopeInterface::SCOPE_STORE, 'nl', '34567'],
102
                    ['payone_payment/'.PayoneConfig::METHOD_CREDITCARD.'/use_global', ScopeInterface::SCOPE_STORE, 'nl', '0'],
103
                    ['payone_payment/'.PayoneConfig::METHOD_CREDITCARD.'/key', ScopeInterface::SCOPE_STORE, 'nl', 'extra_payment_key'],
104
                ]
105
            );
106
        $result = $this->toolkit->getAllPayoneSecurityKeys();
107
        $expected = ['12345', '23456', '34567', 'extra_payment_key'];
108
        $this->assertEquals($expected, $result);
109
    }
110
111
    public function testIsKeyValid()
112
    {
113
        $key = 'extra_payment_key';
114
        $this->scopeConfig->expects($this->any())
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Magento\Framework\App\Config\ScopeConfigInterface.

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...
115
            ->method('getValue')
116
            ->willReturnMap(
117
                [
118
                    ['payone_general/global/key', ScopeInterface::SCOPE_STORE, 'de', '12345'],
119
                    ['payone_general/global/key', ScopeInterface::SCOPE_STORE, 'en', '23456'],
120
                    ['payone_general/global/key', ScopeInterface::SCOPE_STORE, 'fr', '12345'],
121
                    ['payone_general/global/key', ScopeInterface::SCOPE_STORE, 'nl', '34567'],
122
                    ['payone_payment/'.PayoneConfig::METHOD_CREDITCARD.'/use_global', ScopeInterface::SCOPE_STORE, 'nl', '0'],
123
                    ['payone_payment/'.PayoneConfig::METHOD_CREDITCARD.'/key', ScopeInterface::SCOPE_STORE, 'nl', $key],
124
                ]
125
            );
126
127
        $hash = md5($key);
128
        $result = $this->toolkit->isKeyValid($hash);
129
        $this->assertTrue($result);
130
131
        $result = $this->toolkit->isKeyValid('no hash');
132
        $this->assertFalse($result);
133
    }
134
135
    public function testHandleSubstituteReplacement()
136
    {
137
        $text = 'Lets pass this {{what}}';
138
139
        $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...
140
        $expected = 'Lets pass this test';
141
        $this->assertEquals($expected, $result);
142
143
        $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...
144
        $expected = 'Lets';
145
        $this->assertEquals($expected, $result);
146
    }
147
148
    public function testHandleSubstituteReplacementEmpty()
149
    {
150
        $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...
151
        $expected = '';
152
        $this->assertEquals($expected, $result);
153
    }
154
155
    public function testGetInvoiceAppendix()
156
    {
157
        $text = 'New order with order-nr {{order_increment_id}}. Your customer-id is {{customer_id}}';
158
159
        $this->scopeConfig->expects($this->any())
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Magento\Framework\App\Config\ScopeConfigInterface.

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

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...
177
            ->method('getValue')
178
            ->willReturnMap([['payone_payment/'.PayoneConfig::METHOD_CREDITCARD.'/narrative_text', ScopeInterface::SCOPE_STORE, null, $text]]);
179
180
        $order = $this->getMockBuilder(Order::class)->disableOriginalConstructor()->getMock();
181
        $order->method('getIncrementId')->willReturn('0000000001');
182
183
        $payment = $this->getMockBuilder(PayoneMethod::class)->disableOriginalConstructor()->getMock();
184
        $payment->method('getCode')->willReturn(PayoneConfig::METHOD_CREDITCARD);
185
        $payment->method('getNarrativeTextMaxLength')->willReturn(24);
186
187
        $result = $this->toolkit->getNarrativeText($order, $payment);
188
        $expected = '0000000001 was replaced.';
189
        $this->assertEquals($expected, $result);
190
    }
191
192
    public function testFormatNumber()
193
    {
194
        $result = $this->toolkit->formatNumber(192.20587);
195
        $expected = '192.21';
196
        $this->assertEquals($expected, $result);
197
198
        $result = $this->toolkit->formatNumber(192.20587, 8);
199
        $expected = '192.20587000';
200
        $this->assertEquals($expected, $result);
201
    }
202
203
    public function testIsUTF8()
204
    {
205
        $input = 'not utf-8 - ä';
206
        $result = $this->toolkit->isUTF8(utf8_decode($input));
207
        $this->assertFalse($result);
208
209
        $input = 'utf-8 äöü';
210
        $result = $this->toolkit->isUTF8(utf8_encode($input));
211
        $this->assertTrue($result);
212
    }
213
214 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...
215
    {
216
        $expected = 'value';
217
218
        $dataObject = $this->getMockBuilder(DataObject::class)->disableOriginalConstructor()->getMock();
219
        $dataObject->method('getData')->willReturn($expected);
220
221
        $this->shopHelper->method('getMagentoVersion')->willReturn('2.0.0');
222
223
        $result = $this->toolkit->getAdditionalDataEntry($dataObject, 'key');
224
        $this->assertEquals($expected, $result);
225
    }
226
227
    public function testGetAdditionalDataEntryNew()
228
    {
229
        $expected = 'value';
230
231
        $dataObject = $this->getMockBuilder(DataObject::class)
232
            ->disableOriginalConstructor()
233
            ->setMethods(['getAdditionalData'])
234
            ->getMock();
235
        $dataObject->method('getAdditionalData')->willReturn(['key' => $expected]);
236
237
        $this->shopHelper->method('getMagentoVersion')->willReturn('2.1.3');
238
239
        $result = $this->toolkit->getAdditionalDataEntry($dataObject, 'key');
240
        $this->assertEquals($expected, $result);
241
242
        $result = $this->toolkit->getAdditionalDataEntry($dataObject, 'key2');
243
        $this->assertNull($result);
244
    }
245
}
246