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