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::expect($this->key, 1)->parse($key, $index)->thenReturn([$parsedKey, $parsedIndex]); |
|
|
|
|
29
|
|
|
Phake::expect($this->store, 1)->keyExists($parsedKey, $parsedIndex)->thenReturn(true); |
|
|
|
|
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
$this->assertEquals(StoreTest::SECOND_VALUE, $this->store->get($key, $index)); |
|
|
|
|
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function testInvalidArgumentExceptionBubblesUpFromParse() |
36
|
|
|
{ |
37
|
|
|
$key = '10th Thing'; |
38
|
|
|
$index = 5; |
39
|
|
|
$exception = new InvalidArgumentException( |
40
|
|
|
"$index was provided for index param when key '$key' contains an nth value, but they do not match" |
41
|
|
|
); |
42
|
|
|
|
43
|
|
|
/* @noinspection PhpUndefinedMethodInspection */ |
44
|
|
|
Phake::expect($this->key, 1)->parse($key, $index)->thenThrow($exception); |
|
|
|
|
45
|
|
|
|
46
|
|
|
$this->expectException(get_class($exception)); |
47
|
|
|
$this->expectExceptionMessage($exception->getMessage()); |
48
|
|
|
|
49
|
|
|
$this->store->get($key, $index); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function testReturnsNullWhenKeyDoesNotExist() |
53
|
|
|
{ |
54
|
|
|
$key = StoreTest::KEY; |
55
|
|
|
$index = 2; |
56
|
|
|
$parsedKey = $key; |
57
|
|
|
$parsedIndex = $index; |
58
|
|
|
|
59
|
|
|
/* @noinspection PhpUndefinedMethodInspection */ |
60
|
|
|
{ |
61
|
|
|
Phake::expect($this->key, 1)->parse($key, $index)->thenReturn([$parsedKey, $parsedIndex]); |
|
|
|
|
62
|
|
|
Phake::expect($this->store, 1)->keyExists($parsedKey, $parsedIndex)->thenReturn(false); |
|
|
|
|
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$this->assertNull($this->store->get($key, $index)); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function testLastItemIsReturnedWhenParsedIndexIsNull() |
69
|
|
|
{ |
70
|
|
|
$key = StoreTest::KEY; |
71
|
|
|
$index = null; |
72
|
|
|
$parsedKey = $key; |
73
|
|
|
$parsedIndex = $index; |
74
|
|
|
|
75
|
|
|
/* @noinspection PhpUndefinedMethodInspection */ |
76
|
|
|
{ |
77
|
|
|
Phake::expect($this->key, 1)->parse($key, $index)->thenReturn([$parsedKey, $parsedIndex]); |
|
|
|
|
78
|
|
|
Phake::expect($this->store, 1)->keyExists($parsedKey, $parsedIndex)->thenReturn(true); |
|
|
|
|
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$this->assertEquals(StoreTest::SECOND_VALUE, $this->store->get($key, $index)); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function testIndexItemIsReturnedWhenParsedIndexIsNotNull() |
85
|
|
|
{ |
86
|
|
|
$key = '1st ' . StoreTest::KEY; |
87
|
|
|
$index = null; |
88
|
|
|
$parsedKey = StoreTest::KEY; |
89
|
|
|
$parsedIndex = 0; |
90
|
|
|
|
91
|
|
|
/* @noinspection PhpUndefinedMethodInspection */ |
92
|
|
|
{ |
93
|
|
|
Phake::expect($this->key, 1)->parse($key, $index)->thenReturn([$parsedKey, $parsedIndex]); |
|
|
|
|
94
|
|
|
Phake::expect($this->store, 1)->keyExists($parsedKey, $parsedIndex)->thenReturn(true); |
|
|
|
|
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$this->assertEquals(StoreTest::FIRST_VALUE, $this->store->get($key, $index)); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|