ShopTest::testGetStoreId()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 4
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\Shop;
30
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
31
use Magento\Framework\App\ProductMetadata;
32
use Magento\Store\Model\StoreManagerInterface;
33
use Magento\Store\Api\Data\StoreInterface;
34
use Magento\Framework\App\Helper\Context;
35
use Magento\Framework\App\Config\ScopeConfigInterface;
36
use Magento\Store\Model\ScopeInterface;
37
use Payone\Core\Test\Unit\BaseTestCase;
38
use Payone\Core\Model\Test\PayoneObjectManager;
39
40
class ShopTest extends BaseTestCase
41
{
42
    /**
43
     * @var ObjectManager|PayoneObjectManager
44
     */
45
    private $objectManager;
46
47
    /**
48
     * @var Shop
49
     */
50
    protected $shop;
51
52
    /**
53
     * @var ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject
54
     */
55
    private $scopeConfig;
56
57
    protected function setUp()
58
    {
59
        $this->objectManager = $this->getObjectManager();
60
61
        $this->scopeConfig = $this->getMockBuilder(ScopeConfigInterface::class)->disableOriginalConstructor()->getMock();
62
        $context = $this->objectManager->getObject(Context::class, ['scopeConfig' => $this->scopeConfig]);
63
64
        $productMetadata = $this->getMockBuilder(ProductMetadata::class)->disableOriginalConstructor()->getMock();
65
        $productMetadata->method('getEdition')->willReturn('Community');
66
        $productMetadata->method('getVersion')->willReturn('2.0.0');
67
68
        $store = $this->getMockBuilder(StoreInterface::class)->disableOriginalConstructor()->getMock();
69
        $store->method('getId')->willReturn(1);
70
71
        $storeManager = $this->getMockBuilder(StoreManagerInterface::class)->disableOriginalConstructor()->getMock();
72
        $storeManager->method('getStore')->willReturn($store);
73
        $this->shop = $this->objectManager->getObject(Shop::class, [
74
            'context' => $context,
75
            'storeManager' => $storeManager,
76
            'productMetadata' => $productMetadata
77
        ]);
78
    }
79
80
    public function testGetMagentoEdition()
81
    {
82
        $result = $this->shop->getMagentoEdition();
83
        $expected = ['Community', 'Enterprise'];
84
        $this->assertContains($result, $expected);
85
    }
86
87
    public function testGetMagentoVersion()
88
    {
89
        $result = $this->shop->getMagentoVersion();
90
        $this->assertNotEmpty($result);
91
    }
92
93
    public function testGetStoreId()
94
    {
95
        $result = $this->shop->getStoreId();
96
        $expected = 1;
97
        $this->assertEquals($expected, $result);
98
    }
99
100
    public function testGetLocale()
101
    {
102
        $expected = 'de';
103
        $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...
104
            ->method('getValue')
105
            ->willReturnMap([['general/locale/code', ScopeInterface::SCOPE_STORE, null, $expected]]);
106
        $result = $this->shop->getLocale();
107
        $this->assertEquals($expected, $result);
108
    }
109
}
110