1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tests\Cache; |
4
|
|
|
|
5
|
|
|
use Ds\Cache\Cache; |
6
|
|
|
use Ds\Cache\CacheStorageInterface; |
7
|
|
|
use Psr\SimpleCache\InvalidArgumentException; |
8
|
|
|
use Tests\Cache\Mock\IteratorMock; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Protected/Private Method Tests for Ds\Cache\Cache |
12
|
|
|
* |
13
|
|
|
* @package Tests\Cache |
14
|
|
|
*/ |
15
|
|
|
class CachePrivateTest extends \PHPUnit\Framework\TestCase |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var Cache |
19
|
|
|
*/ |
20
|
|
|
public $cache; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject |
24
|
|
|
*/ |
25
|
|
|
public $storageMock; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* |
29
|
|
|
*/ |
30
|
|
|
public function setUp() |
31
|
|
|
{ |
32
|
|
|
$this->storageMock = $this->getMockBuilder(CacheStorageInterface::class)->getMock(); |
33
|
|
|
$this->cache = new Cache($this->storageMock); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Test that array has keys. |
38
|
|
|
*/ |
39
|
|
View Code Duplication |
public function testArrayHasKeys(){ |
|
|
|
|
40
|
|
|
$data = ['a' => 'foo', 'b' => 'bar']; |
41
|
|
|
$method = $this->getCacheMethod('_hasKeys'); |
42
|
|
|
$actual = $method->invokeArgs($this->cache, [$data]); |
43
|
|
|
$this->assertEquals(true, $actual); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Test exception thrown when array is missing keys. |
48
|
|
|
*/ |
49
|
|
View Code Duplication |
public function testArrayHasNoKeys(){ |
|
|
|
|
50
|
|
|
$this->expectException(InvalidArgumentException::class); |
51
|
|
|
$data = ['foo','bar']; |
52
|
|
|
$method = $this->getCacheMethod('_hasKeys'); |
53
|
|
|
$method->invokeArgs($this->cache, [$data]); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Test array contains a failure. |
58
|
|
|
*/ |
59
|
|
View Code Duplication |
public function testHasFailure(){ |
|
|
|
|
60
|
|
|
$results = [true,true,true,false,true]; |
61
|
|
|
$method = $this->getCacheMethod('_hasFailure'); |
62
|
|
|
$actual = $method->invokeArgs($this->cache, [$results]); |
63
|
|
|
$expected = true; |
64
|
|
|
$this->assertEquals($expected, $actual); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* |
69
|
|
|
*/ |
70
|
|
View Code Duplication |
public function testHasNoFailure(){ |
|
|
|
|
71
|
|
|
$results = [true,true,true,true,true]; |
72
|
|
|
$method = $this->getCacheMethod('_hasFailure'); |
73
|
|
|
$actual = $method->invokeArgs($this->cache, [$results]); |
74
|
|
|
$expected = false; |
75
|
|
|
$this->assertEquals($expected, $actual); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Test that array is Traversable |
80
|
|
|
*/ |
81
|
|
View Code Duplication |
public function testIsTraversable(){ |
|
|
|
|
82
|
|
|
$data = ['a','b','c','d']; |
83
|
|
|
$method = $this->getCacheMethod('_isTraversable'); |
84
|
|
|
$actual = $method->invokeArgs($this->cache, [$data]); |
85
|
|
|
$expected = true; |
86
|
|
|
$this->assertEquals($expected, $actual); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Test that string is not Traversable |
91
|
|
|
*/ |
92
|
|
View Code Duplication |
public function testIsNotTraversable(){ |
|
|
|
|
93
|
|
|
$this->expectException(InvalidArgumentException::class); |
94
|
|
|
$data = 'some-random-string'; |
95
|
|
|
$method = $this->getCacheMethod('_isTraversable'); |
96
|
|
|
$method->invokeArgs($this->cache, [$data]); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Test that instance of Iterator is traversable |
101
|
|
|
*/ |
102
|
|
|
public function testIsTraversableIterator(){ |
103
|
|
|
$iterator = new IteratorMock(); |
104
|
|
|
$method = $this->getCacheMethod('_isTraversable'); |
105
|
|
|
$actual = $method->invokeArgs($this->cache, [$iterator]); |
106
|
|
|
$expected = true; |
107
|
|
|
$this->assertEquals($expected, $actual); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Reflect protected/private Methods. |
112
|
|
|
* @param $method |
113
|
|
|
* @return \ReflectionMethod |
114
|
|
|
*/ |
115
|
|
|
private function getCacheMethod($method){ |
116
|
|
|
$reflector = new \ReflectionClass('\Ds\Cache\Cache'); |
117
|
|
|
$method = $reflector->getMethod($method); |
118
|
|
|
$method->setAccessible(true); |
119
|
|
|
return $method; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
} |
123
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.