Completed
Push — 1.10 ( 93e51c...7f32fb )
by
unknown
11:06
created

Iso2CodeProviderTest::testGetIso2CodeByCountryId()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 28
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 28
rs 8.8571
cc 1
eloc 20
nc 1
nop 2
1
<?php
2
3
namespace OroCRM\Bundle\MagentoBundle\Tests\Unit\Provider;
4
5
use Oro\Bundle\AddressBundle\Entity\Country;
6
use Oro\Bundle\AddressBundle\Entity\Repository\CountryRepository;
7
use OroCRM\Bundle\MagentoBundle\Provider\Iso2CodeProvider;
8
use Symfony\Bridge\Doctrine\RegistryInterface;
9
10
class Iso2CodeProviderTest extends \PHPUnit_Framework_TestCase
11
{
12
    /**
13
     * @var RegistryInterface|\PHPUnit_Framework_MockObject_MockObject
14
     */
15
    private $registry;
16
17
    /**
18
     * @var Iso2CodeProvider
19
     */
20
    private $provider;
21
22
    protected function setUp()
23
    {
24
        $this->registry = $this->getMockBuilder(RegistryInterface::class)->getMock();
25
        $this->provider = new Iso2CodeProvider($this->registry);
26
    }
27
28
    /**
29
     * @dataProvider getIso2CodeByCountryIdProvider
30
     * @param string $countryId
31
     * @param string $expectedIso2Code
32
     */
33
    public function testGetIso2CodeByCountryId($countryId, $expectedIso2Code)
34
    {
35
        $countriesArray = [
36
            [
37
                'iso2Code' => 'US',
38
                'iso3Code' => 'USA',
39
                'name' => 'United States',
40
            ],
41
            [
42
                'iso2Code' => 'GB',
43
                'iso3Code' => 'GBR',
44
                'name' => 'United Kingdom',
45
            ],
46
        ];
47
        $countryRepository = $this->getMockBuilder(CountryRepository::class)
48
            ->disableOriginalConstructor()
49
            ->getMock();
50
        $countryRepository->expects($this->once())
51
            ->method('getAllCountryNamesArray')
52
            ->willReturn($countriesArray);
53
        $this->registry->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Symfony\Bridge\Doctrine\RegistryInterface.

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...
54
            ->method('getRepository')
55
            ->with(Country::class)
56
            ->willReturn($countryRepository);
57
58
        $iso2Code = $this->provider->getIso2CodeByCountryId($countryId);
59
        $this->assertEquals($expectedIso2Code, $iso2Code);
60
    }
61
62
    /**
63
     * @return array
64
     */
65
    public function getIso2CodeByCountryIdProvider()
66
    {
67
        return [
68
            'find by iso2' => [
69
                'countryId' => 'US',
70
                'expectedIso2Code' => 'US',
71
            ],
72
            'find by iso3' => [
73
                'countryId' => 'GBR',
74
                'expectedIso2Code' => 'GB',
75
            ],
76
            'find by name' => [
77
                'countryId' => 'United Kingdom',
78
                'expectedIso2Code' => 'GB'
79
            ],
80
            'cannot find' => [
81
                'countryId' => 'SomeCountryId',
82
                'expectedIso2Code' => null,
83
            ],
84
        ];
85
    }
86
}
87