|
1
|
|
|
<?php namespace Chekote\NounStore\Assert; |
|
2
|
|
|
|
|
3
|
|
|
use Chekote\NounStore\Store\StoreTest; |
|
4
|
|
|
use Chekote\Phake\Phake; |
|
5
|
|
|
use InvalidArgumentException; |
|
6
|
|
|
use OutOfBoundsException; |
|
7
|
|
|
use RuntimeException; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* @covers \Chekote\NounStore\Assert::keyValueContains() |
|
11
|
|
|
*/ |
|
12
|
|
|
class KeyValueContainsTest extends AssertTest |
|
13
|
|
|
{ |
|
14
|
|
|
public function setUp() |
|
15
|
|
|
{ |
|
16
|
|
|
parent::setUp(); |
|
17
|
|
|
|
|
18
|
|
|
/* @noinspection PhpUndefinedMethodInspection */ |
|
19
|
|
|
Phake::when($this->assert)->keyValueContains(Phake::anyParameters())->thenCallParent(); |
|
|
|
|
|
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
public function successScenariosDataProvider() |
|
23
|
|
|
{ |
|
24
|
|
|
return [ |
|
25
|
|
|
// key index, parsed key, parsed index, value, |
|
26
|
|
|
['1st ' . StoreTest::KEY, null, StoreTest::KEY, 0, substr(StoreTest::FIRST_VALUE, 0, 2) ], // key with nth |
|
27
|
|
|
['2nd ' . StoreTest::KEY, null, StoreTest::KEY, 1, substr(StoreTest::SECOND_VALUE, 0, 2) ], // key with nth |
|
28
|
|
|
[StoreTest::KEY, 2, StoreTest::KEY, 2, substr(StoreTest::MOST_RECENT_VALUE, 0, 2)], // key without nth |
|
29
|
|
|
[StoreTest::KEY, 0, StoreTest::KEY, 0, substr(StoreTest::FIRST_VALUE, 0, 2)], // with index |
|
30
|
|
|
[StoreTest::KEY, 1, StoreTest::KEY, 1, substr(StoreTest::SECOND_VALUE, 0, 2)], // with index |
|
31
|
|
|
]; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function parseExceptionDataProvider() |
|
35
|
|
|
{ |
|
36
|
|
|
return [ |
|
37
|
|
|
// key, index, value, exception class, exception message |
|
38
|
|
|
['1st ' . StoreTest::KEY, 1, null, InvalidArgumentException::class, "1 was provided for index param when key '1st " . StoreTest::KEY . "' contains an nth value, but they do not match"], |
|
39
|
|
|
]; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function keyExistsExceptionDataProvider() |
|
43
|
|
|
{ |
|
44
|
|
|
return [ |
|
45
|
|
|
// key, index, parsed key, parsed index, value, exception class, exception message |
|
46
|
|
|
['3rd ' . StoreTest::KEY, null, StoreTest::KEY, 2, null, OutOfBoundsException::class, "Entry '3rd " . StoreTest::KEY . "' was not found in the store." ], |
|
47
|
|
|
['No Such Key', null, StoreTest::KEY, 0, null, OutOfBoundsException::class, "Entry 'No Such Key' was not found in the store." ], |
|
48
|
|
|
]; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function keyValueContainsExceptionDataProvider() |
|
52
|
|
|
{ |
|
53
|
|
|
return [ |
|
54
|
|
|
// key, index, parsed key, parsed index, built key, value, exception class, exception message |
|
55
|
|
|
['1st ' . StoreTest::KEY, null, StoreTest::KEY, 0, '1st ' . StoreTest::KEY, StoreTest::SECOND_VALUE, RuntimeException::class, "Entry '1st " . StoreTest::KEY . "' does not contain '" . StoreTest::SECOND_VALUE . "'" ], |
|
56
|
|
|
['2nd ' . StoreTest::KEY, null, StoreTest::KEY, 1, '2nd ' . StoreTest::KEY, StoreTest::FIRST_VALUE, RuntimeException::class, "Entry '2nd " . StoreTest::KEY . "' does not contain '" . StoreTest::FIRST_VALUE . "'" ], |
|
57
|
|
|
]; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Executes a success scenario against the method. |
|
62
|
|
|
* |
|
63
|
|
|
* @dataProvider successScenariosDataProvider |
|
64
|
|
|
* @param string $key the key to pass to the method. |
|
65
|
|
|
* @param int $index the index to pass to the method. |
|
66
|
|
|
* @param string $parsedKey the key that the mocked parsed method should return. |
|
67
|
|
|
* @param int $parsedIndex the index that the mocked parsed method should return. |
|
68
|
|
|
* @param mixed $value the value to pass to the method. |
|
69
|
|
|
*/ |
|
70
|
|
|
public function testSuccessScenario($key, $index, $parsedKey, $parsedIndex, $value) |
|
71
|
|
|
{ |
|
72
|
|
|
/* @noinspection PhpUndefinedMethodInspection */ |
|
73
|
|
|
{ |
|
74
|
|
|
Phake::when($this->key)->parse($key, $index)->thenReturn([$parsedKey, $parsedIndex]); |
|
|
|
|
|
|
75
|
|
|
Phake::when($this->assert)->keyExists($parsedKey, $parsedIndex)->thenReturn(true); |
|
|
|
|
|
|
76
|
|
|
Phake::when($this->store)->keyValueContains($parsedKey, $value, $parsedIndex)->thenReturn(true); |
|
|
|
|
|
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
$this->assert->keyValueContains($key, $value, $index); |
|
|
|
|
|
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Executes a failure scenario caused by the parse method. |
|
84
|
|
|
* |
|
85
|
|
|
* @dataProvider parseExceptionDataProvider |
|
86
|
|
|
* @param string $key the key to pass to the method. |
|
87
|
|
|
* @param int $index the index to pass to the method. |
|
88
|
|
|
* @param mixed $value the value to pass to the method. |
|
89
|
|
|
* @param string $exceptionClass the expected class of the exception. |
|
90
|
|
|
* @param string $exceptionMessage the expected message of the exception. |
|
91
|
|
|
*/ |
|
92
|
|
|
public function testParseExceptionScenario($key, $index, $value, $exceptionClass, $exceptionMessage) |
|
93
|
|
|
{ |
|
94
|
|
|
/* @noinspection PhpUndefinedMethodInspection */ |
|
95
|
|
|
{ |
|
96
|
|
|
Phake::when($this->key)->parse($key, $index)->thenThrow(new $exceptionClass($exceptionMessage)); |
|
|
|
|
|
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
$this->expectException($exceptionClass); |
|
100
|
|
|
$this->expectExceptionMessage($exceptionMessage); |
|
101
|
|
|
|
|
102
|
|
|
/* @noinspection PhpUnhandledExceptionInspection */ |
|
103
|
|
|
$this->assert->keyValueContains($key, $value, $index); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Executes a failure scenario caused by the keyExists method on the store. |
|
108
|
|
|
* |
|
109
|
|
|
* @dataProvider keyExistsExceptionDataProvider |
|
110
|
|
|
* @param string $key the key to pass to the method. |
|
111
|
|
|
* @param int $index the index to pass to the method. |
|
112
|
|
|
* @param string $parsedKey the key that the mocked parsed method should return. |
|
113
|
|
|
* @param int $parsedIndex the index that the mocked parsed method should return. |
|
114
|
|
|
* @param mixed $value the value to pass to the method. |
|
115
|
|
|
* @param string $exceptionClass the expected class of the exception. |
|
116
|
|
|
* @param string $exceptionMessage the expected message of the exception. |
|
117
|
|
|
*/ |
|
118
|
|
|
public function testKeyExistsExceptionScenario($key, $index, $parsedKey, $parsedIndex, $value, $exceptionClass, $exceptionMessage) |
|
119
|
|
|
{ |
|
120
|
|
|
/* @noinspection PhpUndefinedMethodInspection */ |
|
121
|
|
|
{ |
|
122
|
|
|
Phake::when($this->key)->parse($key, $index)->thenReturn([$parsedKey, $parsedIndex]); |
|
|
|
|
|
|
123
|
|
|
Phake::when($this->assert)->keyExists($parsedKey, $parsedIndex)->thenThrow(new $exceptionClass($exceptionMessage)); |
|
|
|
|
|
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
$this->expectException($exceptionClass); |
|
127
|
|
|
$this->expectExceptionMessage($exceptionMessage); |
|
128
|
|
|
|
|
129
|
|
|
/* @noinspection PhpUnhandledExceptionInspection */ |
|
130
|
|
|
$this->assert->keyValueContains($key, $value, $index); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Executes a failure scenario caused by the keyExists method on the store. |
|
135
|
|
|
* |
|
136
|
|
|
* @dataProvider keyValueContainsExceptionDataProvider |
|
137
|
|
|
* @param string $key the key to pass to the method. |
|
138
|
|
|
* @param int $index the index to pass to the method. |
|
139
|
|
|
* @param string $parsedKey the key that the mocked parsed method should return. |
|
140
|
|
|
* @param int $parsedIndex the index that the mocked parsed method should return. |
|
141
|
|
|
* @param mixed $value the value to pass to the method. |
|
142
|
|
|
* @param string $exceptionClass the expected class of the exception. |
|
143
|
|
|
* @param string $exceptionMessage the expected message of the exception. |
|
144
|
|
|
*/ |
|
145
|
|
|
public function testKeyValueContainsExceptionScenario($key, $index, $parsedKey, $parsedIndex, $builtKey, $value, $exceptionClass, $exceptionMessage) |
|
146
|
|
|
{ |
|
147
|
|
|
/* @noinspection PhpUndefinedMethodInspection */ |
|
148
|
|
|
{ |
|
149
|
|
|
Phake::when($this->key)->parse($key, $index)->thenReturn([$parsedKey, $parsedIndex]); |
|
|
|
|
|
|
150
|
|
|
Phake::when($this->assert)->keyExists($parsedKey, $parsedIndex)->thenReturn(true); |
|
|
|
|
|
|
151
|
|
|
Phake::when($this->store)->keyValueContains($parsedKey, $value, $parsedIndex)->thenReturn(false); |
|
|
|
|
|
|
152
|
|
|
Phake::when($this->key)->build($parsedKey, $parsedIndex)->thenReturn($builtKey); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
$this->expectException($exceptionClass); |
|
156
|
|
|
$this->expectExceptionMessage($exceptionMessage); |
|
157
|
|
|
|
|
158
|
|
|
/* @noinspection PhpUnhandledExceptionInspection */ |
|
159
|
|
|
$this->assert->keyValueContains($key, $value, $index); |
|
160
|
|
|
} |
|
161
|
|
|
} |
|
162
|
|
|
|