|
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
|
|
|
/** |
|
20
|
|
|
* Tests that InvalidArgumentException is thrown if Store::get is called with the index parameter |
|
21
|
|
|
* and the key also contains an nth value, but they do not match. |
|
22
|
|
|
*/ |
|
23
|
|
|
public function testGetThrowsInvalidArgumentExceptionWithMismatchedNthAndIndex() |
|
24
|
|
|
{ |
|
25
|
|
|
$key = '1st Thing'; |
|
26
|
|
|
$index = 1; |
|
27
|
|
|
|
|
28
|
|
|
/* @noinspection PhpUndefinedMethodInspection */ |
|
29
|
|
|
Phake::when($this->key)->parse($key, $index)->thenThrow(new InvalidArgumentException()); |
|
|
|
|
|
|
30
|
|
|
|
|
31
|
|
|
$this->expectException(InvalidArgumentException::class); |
|
32
|
|
|
|
|
33
|
|
|
$this->store->get($key, $index); |
|
|
|
|
|
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Tests that Store::get returns the item at the end of the stack. |
|
38
|
|
|
*/ |
|
39
|
|
|
public function testGetReturnsItemAtEndOfStack() |
|
40
|
|
|
{ |
|
41
|
|
|
$key = self::KEY; |
|
42
|
|
|
$index = null; |
|
43
|
|
|
$parsedKey = self::KEY; |
|
44
|
|
|
$parsedIndex = null; |
|
45
|
|
|
|
|
46
|
|
|
/* @noinspection PhpUndefinedMethodInspection */ |
|
47
|
|
|
{ |
|
48
|
|
|
Phake::when($this->key)->parse($key, $index)->thenReturn([$parsedKey, $parsedIndex]); |
|
|
|
|
|
|
49
|
|
|
Phake::when($this->store)->keyExists($parsedKey, $parsedIndex)->thenReturn(true); |
|
|
|
|
|
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
$this->assertEquals(self::SECOND_VALUE, $this->store->get(self::KEY)); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Tests that Store::get returns the nth item at of the stack when the $key contains nth. |
|
57
|
|
|
*/ |
|
58
|
|
|
public function testGetWithNthKeyReturnsNthItem() |
|
59
|
|
|
{ |
|
60
|
|
|
$key = '1st ' . self::KEY; |
|
61
|
|
|
$index = null; |
|
62
|
|
|
$parsedKey = self::KEY; |
|
63
|
|
|
$parsedIndex = 0; |
|
64
|
|
|
|
|
65
|
|
|
/* @noinspection PhpUndefinedMethodInspection */ |
|
66
|
|
|
{ |
|
67
|
|
|
Phake::when($this->key)->parse($key, $index)->thenReturn([$parsedKey, $parsedIndex]); |
|
|
|
|
|
|
68
|
|
|
Phake::when($this->store)->keyExists($parsedKey, $parsedIndex)->thenReturn(true); |
|
|
|
|
|
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
$this->assertEquals(self::FIRST_VALUE, $this->store->get($key)); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Tests that Store::get returns the index item at of the stack when index parameter is provided. |
|
76
|
|
|
*/ |
|
77
|
|
|
public function testGetWithIndexParameterReturnsIndexItem() |
|
78
|
|
|
{ |
|
79
|
|
|
$key = self::KEY; |
|
80
|
|
|
$index = 0; |
|
81
|
|
|
$parsedKey = self::KEY; |
|
82
|
|
|
$parsedIndex = 0; |
|
83
|
|
|
|
|
84
|
|
|
/* @noinspection PhpUndefinedMethodInspection */ |
|
85
|
|
|
{ |
|
86
|
|
|
Phake::when($this->key)->parse($key, $index)->thenReturn([$parsedKey, $parsedIndex]); |
|
|
|
|
|
|
87
|
|
|
Phake::when($this->store)->keyExists($parsedKey, $parsedIndex)->thenReturn(true); |
|
|
|
|
|
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
$this->assertEquals(self::FIRST_VALUE, $this->store->get(self::KEY, $index)); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Tests that Store::get returns null when the specified $key does not exist. |
|
95
|
|
|
*/ |
|
96
|
|
|
public function testGetReturnsNullWhenKeyDoesNotExist() |
|
97
|
|
|
{ |
|
98
|
|
|
$key = 'Thing'; |
|
99
|
|
|
$index = null; |
|
100
|
|
|
$parsedKey = 'Thing'; |
|
101
|
|
|
$parsedIndex = null; |
|
102
|
|
|
|
|
103
|
|
|
/* @noinspection PhpUndefinedMethodInspection */ |
|
104
|
|
|
{ |
|
105
|
|
|
Phake::when($this->key)->parse($key, $index)->thenReturn([$parsedKey, $parsedIndex]); |
|
|
|
|
|
|
106
|
|
|
Phake::when($this->store)->keyExists($parsedKey, $parsedIndex)->thenReturn(false); |
|
|
|
|
|
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
$this->assertEquals(null, $this->store->get($key)); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Tests that Store::get returns null when the specified nth $key does not exist. |
|
114
|
|
|
*/ |
|
115
|
|
|
public function testGetReturnsNullWhenNthKeyDoesNotExist() |
|
116
|
|
|
{ |
|
117
|
|
|
$key = '3rd ' . self::KEY; |
|
118
|
|
|
$index = null; |
|
119
|
|
|
$parsedKey = self::KEY; |
|
120
|
|
|
$parsedIndex = 2; |
|
121
|
|
|
|
|
122
|
|
|
/* @noinspection PhpUndefinedMethodInspection */ |
|
123
|
|
|
{ |
|
124
|
|
|
Phake::when($this->key)->parse($key, $index)->thenReturn([$parsedKey, $parsedIndex]); |
|
|
|
|
|
|
125
|
|
|
Phake::when($this->store)->keyExists($parsedKey, $parsedIndex)->thenReturn(false); |
|
|
|
|
|
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
$this->assertEquals(null, $this->store->get($key)); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Tests that Store::get returns null when the specified $index param does not exist. |
|
133
|
|
|
*/ |
|
134
|
|
|
public function testGetReturnsNullWhenIndexDoesNotExist() |
|
135
|
|
|
{ |
|
136
|
|
|
$key = self::KEY; |
|
137
|
|
|
$index = 2; |
|
138
|
|
|
$parsedKey = self::KEY; |
|
139
|
|
|
$parsedIndex = 2; |
|
140
|
|
|
|
|
141
|
|
|
/* @noinspection PhpUndefinedMethodInspection */ |
|
142
|
|
|
{ |
|
143
|
|
|
Phake::when($this->key)->parse($key, $index)->thenReturn([$parsedKey, $parsedIndex]); |
|
|
|
|
|
|
144
|
|
|
Phake::when($this->store)->keyExists($parsedKey, $parsedIndex)->thenReturn(false); |
|
|
|
|
|
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
$this->assertEquals(null, $this->store->get($key, $index)); |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
|