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