KeyValueDictionaryTest   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 55
rs 10
c 0
b 0
f 0
wmc 3
lcom 1
cbo 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
A buildMock() 0 9 1
A testFixDictionaryIndex() 0 22 1
A testGetSingleColumn() 0 13 1
1
<?php
2
namespace FwlibTest\Db;
3
4
use Fwlib\Db\KeyValueDictionary;
5
use Fwolf\Wrapper\PHPUnit\PHPUnitTestCase;
6
use PHPUnit_Framework_MockObject_MockObject as MockObject;
7
8
/**
9
 * @copyright   Copyright 2015 Fwolf
10
 * @license     http://www.gnu.org/licenses/lgpl.html LGPL-3.0+
11
 */
12
class KeyValueDictionaryTest extends PHPUnitTestCase
13
{
14
    /**
15
     * @param   string[] $methods
16
     * @return  MockObject|KeyValueDictionary
17
     */
18
    protected function buildMock(array $methods = null)
19
    {
20
        $mock = $this->getMockBuilder(KeyValueDictionary::class)
21
            ->setMethods($methods)
22
            ->disableOriginalConstructor()
23
            ->getMock();
24
25
        return $mock;
26
    }
27
28
29
    public function testFixDictionaryIndex()
30
    {
31
        $dict = $this->buildMock();
32
33
        $this->reflectionSet($dict, 'dictionary', [
34
            'foo' => 21,
35
            'bar' => 42,
36
        ]);
37
        $this->reflectionCall($dict, 'fixDictionaryIndex');
38
39
        $dictAr = $this->reflectionGet($dict, 'dictionary');
40
        $this->assertEqualArray($dictAr, [
41
            'foo' => [
42
                'code'  => 'foo',
43
                'title' => 21,
44
            ],
45
            'bar' => [
46
                'code'  => 'bar',
47
                'title' => 42,
48
            ],
49
        ]);
50
    }
51
52
53
    public function testGetSingleColumn()
54
    {
55
        $dict = $this->buildMock();
56
        $dict->set([
0 ignored issues
show
Bug introduced by
The method set does only exist in Fwlib\Db\KeyValueDictionary, but not in PHPUnit_Framework_MockObject_MockObject.

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...
57
            ['foo', 21],
58
            ['bar', 42],
59
        ]);
60
61
        $this->assertEqualArray($dict->getSingleColumn(), [
0 ignored issues
show
Bug introduced by
The method getSingleColumn does only exist in Fwlib\Db\KeyValueDictionary, but not in PHPUnit_Framework_MockObject_MockObject.

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...
62
            'foo' => 21,
63
            'bar' => 42,
64
        ]);
65
    }
66
}
67