Completed
Push — master ( b50753...137f43 )
by Florian
03:57
created

EnvironmentTest::testIsRemoteIpNotValid()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 8

Duplication

Lines 15
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 15
loc 15
c 1
b 0
f 0
rs 9.4285
cc 1
eloc 8
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\Environment;
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 Magento\Framework\HTTP\PhpEnvironment\RemoteAddress;
37
38
class EnvironmentTest extends \PHPUnit_Framework_TestCase
39
{
40
    /**
41
     * @var ObjectManager
42
     */
43
    private $objectManager;
44
45
    /**
46
     * @var Environment
47
     */
48
    private $environment;
49
50
    /**
51
     * @var ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject
52
     */
53
    private $scopeConfig;
54
55 View Code Duplication
    protected function setUp()
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...
56
    {
57
        $this->objectManager = new ObjectManager($this);
58
59
        $remoteAddress = $this->getMockBuilder(RemoteAddress::class)->disableOriginalConstructor()->getMock();
60
        $remoteAddress->method('getRemoteAddress')->willReturn('192.168.1.100');
61
62
        $this->scopeConfig = $this->getMockBuilder(ScopeConfigInterface::class)->disableOriginalConstructor()->getMock();
63
        $context = $this->objectManager->getObject(Context::class, ['scopeConfig' => $this->scopeConfig, 'remoteAddress' => $remoteAddress]);
64
65
        $store = $this->getMockBuilder(StoreInterface::class)->disableOriginalConstructor()->getMock();
66
        $store->method('getCode')->willReturn(null);
67
68
        $storeManager = $this->getMockBuilder(StoreManagerInterface::class)->disableOriginalConstructor()->getMock();
69
        $storeManager->method('getStore')->willReturn($store);
70
71
        $this->environment = $this->objectManager->getObject(Environment::class, [
72
            'context' => $context,
73
            'storeManager' => $storeManager
74
        ]);
75
    }
76
77
    public function testGetEncoding()
78
    {
79
        $result = $this->environment->getEncoding();
80
        $expected = 'UTF-8';
81
        $this->assertEquals($expected, $result);
82
    }
83
84
    public function testGetRemoteIp()
85
    {
86
        $expected = '192.168.1.100';
87
88
        $result = $this->environment->getRemoteIp();
89
        $this->assertEquals($expected, $result);
90
    }
91
92 View Code Duplication
    public function testIsRemoteIpValid()
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...
93
    {
94
        $sWhitelist = "127.0.0.1\n166.7.1.*\n192.168.*.*\n217.8.19.7";
95
96
        $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...
97
            ->method('getValue')
98
            ->willReturnMap(
99
                [
100
                    ['payone_misc/processing/valid_ips', ScopeInterface::SCOPE_STORE, null, $sWhitelist]
101
                ]
102
            );
103
104
        $result = $this->environment->isRemoteIpValid();
105
        $this->assertTrue($result);
106
    }
107
108 View Code Duplication
    public function testIsRemoteIpNotValid()
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...
109
    {
110
        $sWhitelist = "127.0.0.1\n166.7.1.*\n217.8.19.7";
111
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_misc/processing/valid_ips', ScopeInterface::SCOPE_STORE, null, $sWhitelist]
117
                ]
118
            );
119
120
        $result = $this->environment->isRemoteIpValid();
121
        $this->assertFalse($result);
122
    }
123
124 View Code Duplication
    public function testIsRemoteIpDirectValid()
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...
125
    {
126
        $sWhitelist = "127.0.0.1\n166.7.1.*\n217.8.19.7\n192.168.1.100";
127
128
        $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...
129
            ->method('getValue')
130
            ->willReturnMap(
131
                [
132
                    ['payone_misc/processing/valid_ips', ScopeInterface::SCOPE_STORE, null, $sWhitelist]
133
                ]
134
            );
135
136
        $result = $this->environment->isRemoteIpValid();
137
        $this->assertTrue($result);
138
    }
139
}
140