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