WithVersionTraitTest::buildMock()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 1
nop 0
dl 0
loc 25
rs 9.52
c 0
b 0
f 0
1
<?php
2
namespace FwlibTest\Cache\Handler\Helper;
3
4
use Fwlib\Cache\Handler\Helper\WithVersionTrait;
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 WithVersionTraitTest extends PHPUnitTestCase
13
{
14
    /**
15
     * @var int[]
16
     */
17
    protected $versions = [];
18
19
20
    /**
21
     * @return MockObject | \Fwlib\Cache\Handler\Helper\WithVersionTrait
22
     */
23
    protected function buildMock()
24
    {
25
        $mock = $this->getMockBuilder(WithVersionTrait::class)
26
            ->setMethods(['get', 'set'])
27
            ->getMockForTrait();
28
29
        $mock->expects($this->any())
30
            ->method('get')
31
            ->willReturnCallback(function($key) {
32
                return array_key_exists($key, $this->versions)
33
                    ? $this->versions[$key]
34
                    : null;
35
            });
36
37
        $mock->expects($this->any())
38
            ->method('set')
39
            ->willReturnCallback(function($key, $value) {
40
                $this->versions[$key] = $value;
41
            });
42
43
        /** @noinspection PhpUndefinedFieldInspection */
44
        $mock->versionSuffix = '-ver';
0 ignored issues
show
Bug introduced by
Accessing versionSuffix on the interface PHPUnit_Framework_MockObject_MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
45
46
        return $mock;
47
    }
48
49
50
    public function test()
51
    {
52
        $handler = $this->buildMock();
53
54
        $key = 'foo';
55
        $this->assertEquals(1, $handler->getVersion($key));
56
        $this->assertEquals(2, $handler->increaseVersion($key));
57
        $this->assertEquals(2, $handler->getVersion($key));
58
59
        $this->versions['foo-ver'] = 65534;
60
        $this->assertEquals(65535, $handler->increaseVersion($key));
61
        $this->assertEquals(1, $handler->increaseVersion($key));
62
    }
63
}
64