1
|
|
|
<?php namespace Chekote\NounStore\Store; |
2
|
|
|
|
3
|
|
|
use Chekote\Phake\Phake; |
4
|
|
|
use InvalidArgumentException; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* @covers \Chekote\NounStore\Store::get() |
8
|
|
|
*/ |
9
|
|
|
class GetTest extends StoreTest |
10
|
|
|
{ |
11
|
|
|
public function setUp() |
12
|
|
|
{ |
13
|
|
|
parent::setUp(); |
14
|
|
|
|
15
|
|
|
/* @noinspection PhpUndefinedMethodInspection */ |
16
|
|
|
Phake::when($this->store)->get(Phake::anyParameters())->thenCallParent(); |
|
|
|
|
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
public function testKeyIsParsedAndParsedValuesAreUsed() |
20
|
|
|
{ |
21
|
|
|
$key = '2nd ' . StoreTest::KEY; |
22
|
|
|
$index = null; |
23
|
|
|
$parsedKey = StoreTest::KEY; |
24
|
|
|
$parsedIndex = 1; |
25
|
|
|
|
26
|
|
|
/* @noinspection PhpUndefinedMethodInspection */ |
27
|
|
|
{ |
28
|
|
|
Phake::when($this->key)->parse($key, $index)->thenReturn([$parsedKey, $parsedIndex]); |
|
|
|
|
29
|
|
|
Phake::when($this->store)->keyExists($parsedKey, $parsedIndex)->thenReturn(true); |
|
|
|
|
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
$this->assertEquals(StoreTest::SECOND_VALUE, $this->store->get($key, $index)); |
|
|
|
|
33
|
|
|
|
34
|
|
|
/* @noinspection PhpUndefinedMethodInspection */ |
35
|
|
|
{ |
36
|
|
|
Phake::verify($this->key)->parse($key, $index); |
|
|
|
|
37
|
|
|
Phake::verify($this->store)->keyExists($parsedKey, $parsedIndex); |
|
|
|
|
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function testInvalidArgumentExceptionBubblesUpFromParse() |
42
|
|
|
{ |
43
|
|
|
$key = '10th Thing'; |
44
|
|
|
$index = 5; |
45
|
|
|
$exception = new InvalidArgumentException( |
46
|
|
|
"$index was provided for index param when key '$key' contains an nth value, but they do not match" |
47
|
|
|
); |
48
|
|
|
|
49
|
|
|
/* @noinspection PhpUndefinedMethodInspection */ |
50
|
|
|
Phake::when($this->key)->parse($key, $index)->thenThrow($exception); |
|
|
|
|
51
|
|
|
|
52
|
|
|
$this->assertException($exception, function () use ($key, $index) { |
53
|
|
|
$this->store->get($key, $index); |
54
|
|
|
}); |
55
|
|
|
|
56
|
|
|
/* @noinspection PhpUndefinedMethodInspection */ |
57
|
|
|
Phake::verify($this->key)->parse($key, $index); |
|
|
|
|
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function testReturnsNullWhenKeyDoesNotExist() |
61
|
|
|
{ |
62
|
|
|
$key = StoreTest::KEY; |
63
|
|
|
$index = 2; |
64
|
|
|
$parsedKey = $key; |
65
|
|
|
$parsedIndex = $index; |
66
|
|
|
|
67
|
|
|
/* @noinspection PhpUndefinedMethodInspection */ |
68
|
|
|
{ |
69
|
|
|
Phake::when($this->key)->parse($key, $index)->thenReturn([$parsedKey, $parsedIndex]); |
|
|
|
|
70
|
|
|
Phake::when($this->store)->keyExists($parsedKey, $parsedIndex)->thenReturn(false); |
|
|
|
|
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$this->assertNull($this->store->get($key, $index)); |
74
|
|
|
|
75
|
|
|
/* @noinspection PhpUndefinedMethodInspection */ |
76
|
|
|
{ |
77
|
|
|
Phake::verify($this->key)->parse($key, $index); |
|
|
|
|
78
|
|
|
Phake::verify($this->store)->keyExists($parsedKey, $parsedIndex); |
|
|
|
|
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function testLastItemIsReturnedWhenParsedIndexIsNull() |
83
|
|
|
{ |
84
|
|
|
$key = StoreTest::KEY; |
85
|
|
|
$index = null; |
86
|
|
|
$parsedKey = $key; |
87
|
|
|
$parsedIndex = $index; |
88
|
|
|
|
89
|
|
|
/* @noinspection PhpUndefinedMethodInspection */ |
90
|
|
|
{ |
91
|
|
|
Phake::when($this->key)->parse($key, $index)->thenReturn([$parsedKey, $parsedIndex]); |
|
|
|
|
92
|
|
|
Phake::when($this->store)->keyExists($parsedKey, $parsedIndex)->thenReturn(true); |
|
|
|
|
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$this->assertEquals(StoreTest::SECOND_VALUE, $this->store->get($key, $index)); |
96
|
|
|
|
97
|
|
|
/* @noinspection PhpUndefinedMethodInspection */ |
98
|
|
|
{ |
99
|
|
|
Phake::verify($this->key)->parse($key, $index); |
|
|
|
|
100
|
|
|
Phake::verify($this->store)->keyExists($parsedKey, $parsedIndex); |
|
|
|
|
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function testIndexItemIsReturnedWhenParsedIndexIsNotNull() |
105
|
|
|
{ |
106
|
|
|
$key = '1st ' . StoreTest::KEY; |
107
|
|
|
$index = null; |
108
|
|
|
$parsedKey = StoreTest::KEY; |
109
|
|
|
$parsedIndex = 0; |
110
|
|
|
|
111
|
|
|
/* @noinspection PhpUndefinedMethodInspection */ |
112
|
|
|
{ |
113
|
|
|
Phake::when($this->key)->parse($key, $index)->thenReturn([$parsedKey, $parsedIndex]); |
|
|
|
|
114
|
|
|
Phake::when($this->store)->keyExists($parsedKey, $parsedIndex)->thenReturn(true); |
|
|
|
|
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
$this->assertEquals(StoreTest::FIRST_VALUE, $this->store->get($key, $index)); |
118
|
|
|
|
119
|
|
|
/* @noinspection PhpUndefinedMethodInspection */ |
120
|
|
|
{ |
121
|
|
|
Phake::verify($this->key)->parse($key, $index); |
|
|
|
|
122
|
|
|
Phake::verify($this->store)->keyExists($parsedKey, $parsedIndex); |
|
|
|
|
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|