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 testKeyIsParsedAndParsedValuesAreUsed() |
20
|
|
|
{ |
21
|
|
|
$key = '2nd ' . StoreTest::KEY; |
22
|
|
|
$index = null; |
23
|
|
|
$parsedKey = StoreTest::KEY; |
24
|
|
|
$parsedIndex = 1; |
25
|
|
|
$value = substr(self::SECOND_VALUE, 0, 2); |
26
|
|
|
|
27
|
|
|
/* @noinspection PhpUndefinedMethodInspection */ |
28
|
|
|
{ |
29
|
|
|
Phake::when($this->key)->parse($key, $index)->thenReturn([$parsedKey, $parsedIndex]); |
|
|
|
|
30
|
|
|
Phake::when($this->store)->get($parsedKey, $parsedIndex)->thenReturn(self::SECOND_VALUE); |
|
|
|
|
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
$this->assertTrue($this->store->keyValueContains($key, $value, $index)); |
|
|
|
|
34
|
|
|
|
35
|
|
|
/* @noinspection PhpUndefinedMethodInspection */ |
36
|
|
|
{ |
37
|
|
|
Phake::verify($this->key)->parse($key, $index); |
|
|
|
|
38
|
|
|
Phake::verify($this->store)->get($parsedKey, $parsedIndex); |
|
|
|
|
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function testInvalidArgumentExceptionBubblesUpFromParse() |
43
|
|
|
{ |
44
|
|
|
$key = '10th Thing'; |
45
|
|
|
$index = 5; |
46
|
|
|
$exception = new InvalidArgumentException( |
47
|
|
|
"$index was provided for index param when key '$key' contains an nth value, but they do not match" |
48
|
|
|
); |
49
|
|
|
|
50
|
|
|
/* @noinspection PhpUndefinedMethodInspection */ |
51
|
|
|
Phake::when($this->key)->parse($key, $index)->thenThrow($exception); |
|
|
|
|
52
|
|
|
|
53
|
|
|
$this->assertException($exception, function () use ($key, $index) { |
54
|
|
|
$this->store->keyValueContains($key, "Doesn't matter", $index); |
55
|
|
|
}); |
56
|
|
|
|
57
|
|
|
/* @noinspection PhpUndefinedMethodInspection */ |
58
|
|
|
Phake::verify($this->key)->parse($key, $index); |
|
|
|
|
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function returnDataProvider() |
62
|
|
|
{ |
63
|
|
|
return [ |
64
|
|
|
// storedValue, checkedValue, expectedResult |
65
|
|
|
[ 'This is a value', 'is a', true ], |
66
|
|
|
[ 'This is a value', 'words', false ], |
67
|
|
|
]; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @dataProvider returnDataProvider |
72
|
|
|
* @param string $storedValue the value that should be in the store and will be returned by the mocked get() |
73
|
|
|
* @param string $checkedValue the value that will be passed to keyValueContains() |
74
|
|
|
* @param bool $expectedResult the expected results from keyExists() |
75
|
|
|
*/ |
76
|
|
|
public function testReturn($storedValue, $checkedValue, $expectedResult) |
77
|
|
|
{ |
78
|
|
|
$key = StoreTest::KEY; |
79
|
|
|
$index = null; |
80
|
|
|
$parsedKey = $key; |
81
|
|
|
$parsedIndex = $index; |
82
|
|
|
|
83
|
|
|
/* @noinspection PhpUndefinedMethodInspection */ |
84
|
|
|
{ |
85
|
|
|
Phake::when($this->key)->parse($key, $index)->thenReturn([$parsedKey, $parsedIndex]); |
|
|
|
|
86
|
|
|
Phake::when($this->store)->get($parsedKey, $parsedIndex)->thenReturn($storedValue); |
|
|
|
|
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$this->assertEquals($expectedResult, $this->store->keyValueContains($key, $checkedValue, $index)); |
90
|
|
|
|
91
|
|
|
/* @noinspection PhpUndefinedMethodInspection */ |
92
|
|
|
{ |
93
|
|
|
Phake::verify($this->key)->parse($key, $index); |
|
|
|
|
94
|
|
|
Phake::verify($this->store)->get($parsedKey, $parsedIndex); |
|
|
|
|
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|