1
|
|
|
<?php namespace Chekote\NounStore\Store; |
2
|
|
|
|
3
|
|
|
use Chekote\Phake\Phake; |
4
|
|
|
use InvalidArgumentException; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* @covers \Chekote\NounStore\Store::keyValueContains() |
8
|
|
|
*/ |
9
|
|
|
class KeyValueContainsTest extends StoreTest |
10
|
|
|
{ |
11
|
|
|
public function setUp() |
12
|
|
|
{ |
13
|
|
|
parent::setUp(); |
14
|
|
|
|
15
|
|
|
/* @noinspection PhpUndefinedMethodInspection */ |
16
|
|
|
Phake::when($this->store)->keyValueContains(Phake::anyParameters())->thenCallParent(); |
|
|
|
|
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
public function successScenariosDataProvider() |
20
|
|
|
{ |
21
|
|
|
return [ |
22
|
|
|
// key index, parsedKey, parsedIndex, storedValue, checkedValue |
23
|
|
|
['1st ' . self::KEY, null, self::KEY, 0, self::FIRST_VALUE, substr(self::FIRST_VALUE, 0, 2) ], // key with nth |
24
|
|
|
['2nd ' . self::KEY, null, self::KEY, 1, self::SECOND_VALUE, substr(self::SECOND_VALUE, 0, 2) ], // key with nth |
25
|
|
|
[self::KEY, null, self::KEY, null, self::MOST_RECENT_VALUE, substr(self::MOST_RECENT_VALUE, 0, 2) ], // key without nth |
26
|
|
|
[self::KEY, 0, self::KEY, 0, self::FIRST_VALUE, substr(self::FIRST_VALUE, 0, 2) ], // with index |
27
|
|
|
[self::KEY, 1, self::KEY, 1, self::SECOND_VALUE, substr(self::SECOND_VALUE, 0, 2) ], // with index |
28
|
|
|
]; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function failureScenariosDataProvider() |
32
|
|
|
{ |
33
|
|
|
return [ |
34
|
|
|
// key, index, parsedKey, parsedIndex, storedValue, checkedValue |
35
|
|
|
['1st ' . self::KEY, null, self::KEY, 0, self::FIRST_VALUE, self::SECOND_VALUE ], // value mismatch |
36
|
|
|
['2nd ' . self::KEY, null, self::KEY, 1, self::SECOND_VALUE, self::FIRST_VALUE ], // value mismatch |
37
|
|
|
['3rd ' . self::KEY, null, self::KEY, 2, null, null ], // no such nth |
38
|
|
|
['No Such Key', null, 'No Such Key', null, null, null ], // no such key |
39
|
|
|
]; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function parseExceptionScenariosDataProvider() |
43
|
|
|
{ |
44
|
|
|
return [ |
45
|
|
|
// key value index exception class exception message |
46
|
|
|
['1st ' . self::KEY, null, 1, InvalidArgumentException::class, "1 was provided for index param when key '1st " . self::KEY . "' contains an nth value, but they do not match"], |
47
|
|
|
]; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Executes a success scenario against the method. |
52
|
|
|
* |
53
|
|
|
* @dataProvider successScenariosDataProvider |
54
|
|
|
* @param string $key the key to pass to the keyValueContains() method. |
55
|
|
|
* @param int $index the index to pass to the keyValueContains() method. |
56
|
|
|
* @param string $parsedKey the key that the mocked parse() method should return. |
57
|
|
|
* @param string $parsedIndex the index that the mocked parse() method should return. |
58
|
|
|
* @param mixed $storedValue the value that the mocked get() method should return. |
59
|
|
|
* @param mixed $checkedValue the value to pass to the keyValueContains() method. |
60
|
|
|
*/ |
61
|
|
|
public function testSuccessScenario($key, $index, $parsedKey, $parsedIndex, $storedValue, $checkedValue) |
62
|
|
|
{ |
63
|
|
|
/* @noinspection PhpUndefinedMethodInspection */ |
64
|
|
|
{ |
65
|
|
|
Phake::when($this->key)->parse($key, $index)->thenReturn([$parsedKey, $parsedIndex]); |
|
|
|
|
66
|
|
|
Phake::when($this->store)->get($parsedKey, $parsedIndex)->thenReturn($storedValue); |
|
|
|
|
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$this->assertTrue($this->store->keyValueContains($key, $checkedValue, $index)); |
|
|
|
|
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Executes a failure scenario against the method. |
74
|
|
|
* |
75
|
|
|
* @dataProvider failureScenariosDataProvider |
76
|
|
|
* @param string $key the key to pass to the keyValueContains() method. |
77
|
|
|
* @param int $index the index to pass to the keyValueContains() method. |
78
|
|
|
* @param string $parsedKey the key that the mocked parse() method should return. |
79
|
|
|
* @param string $parsedIndex the index that the mocked parse() method should return. |
80
|
|
|
* @param mixed $storedValue the value that the mocked get() method should return. |
81
|
|
|
* @param mixed $checkedValue the value to pass to the keyValueContains() method. |
82
|
|
|
*/ |
83
|
|
|
public function testFailureScenario($key, $index, $parsedKey, $parsedIndex, $storedValue, $checkedValue) |
84
|
|
|
{ |
85
|
|
|
/* @noinspection PhpUndefinedMethodInspection */ |
86
|
|
|
{ |
87
|
|
|
Phake::when($this->key)->parse($key, $index)->thenReturn([$parsedKey, $parsedIndex]); |
|
|
|
|
88
|
|
|
Phake::when($this->store)->get($parsedKey, $parsedIndex)->thenReturn($storedValue); |
|
|
|
|
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$this->assertFalse($this->store->keyValueContains($key, $checkedValue, $index)); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Executes an exception scenario against the method. |
96
|
|
|
* |
97
|
|
|
* @dataProvider parseExceptionScenariosDataProvider |
98
|
|
|
* @param string $key the key to pass to the method. |
99
|
|
|
* @param mixed $value the value to pass to the method. |
100
|
|
|
* @param int $index the index to pass to the method. |
101
|
|
|
* @param string $exceptionClass the expected class of the exception. |
102
|
|
|
* @param string $exceptionMessage the expected message of the exception. |
103
|
|
|
*/ |
104
|
|
|
public function testParseExceptionScenario($key, $value, $index, $exceptionClass, $exceptionMessage) |
105
|
|
|
{ |
106
|
|
|
/* @noinspection PhpUndefinedMethodInspection */ |
107
|
|
|
Phake::when($this->key)->parse($key, $index)->thenThrow(new $exceptionClass($exceptionMessage)); |
|
|
|
|
108
|
|
|
|
109
|
|
|
$this->expectException($exceptionClass); |
110
|
|
|
$this->expectExceptionMessage($exceptionMessage); |
111
|
|
|
|
112
|
|
|
/* @noinspection PhpUnhandledExceptionInspection */ |
113
|
|
|
$this->store->keyValueContains($key, $value, $index); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|