|
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'; |
|
|
|
|
|
|
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
|
|
|
|
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: