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::keyValueContains() |
10
|
|
|
*/ |
11
|
|
|
class KeyValueContainsTest extends AssertTest |
12
|
|
|
{ |
13
|
|
|
public function setUp() |
14
|
|
|
{ |
15
|
|
|
parent::setUp(); |
16
|
|
|
|
17
|
|
|
/* @noinspection PhpUndefinedMethodInspection */ |
18
|
|
|
Phake::when($this->assert)->keyValueContains(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::expect($this->key, 1)->parse($key, $index)->thenReturn([$parsedKey, $parsedIndex]); |
|
|
|
|
32
|
|
|
Phake::expect($this->assert, 1)->keyExists($parsedKey, $parsedIndex)->thenReturn(null); |
|
|
|
|
33
|
|
|
Phake::expect($this->store, 1)->keyValueContains($parsedKey, $value, $parsedIndex)->thenReturn(true); |
|
|
|
|
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
$this->assert->keyValueContains($key, $value, $index); |
|
|
|
|
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function testInvalidArgumentExceptionBubblesUpFromParse() |
40
|
|
|
{ |
41
|
|
|
$key = '10th Thing'; |
42
|
|
|
$index = 5; |
43
|
|
|
$value = 'Some Value'; |
44
|
|
|
$exception = new InvalidArgumentException( |
45
|
|
|
"$index was provided for index param when key '$key' contains an nth value, but they do not match" |
46
|
|
|
); |
47
|
|
|
|
48
|
|
|
/* @noinspection PhpUndefinedMethodInspection */ |
49
|
|
|
Phake::expect($this->key, 1)->parse($key, $index)->thenThrow($exception); |
|
|
|
|
50
|
|
|
|
51
|
|
|
$this->expectException(get_class($exception)); |
52
|
|
|
$this->expectExceptionMessage($exception->getMessage()); |
53
|
|
|
|
54
|
|
|
$this->assert->keyValueContains($key, $value, $index); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function testMissingKeyThrowsOutOfBoundsException() |
58
|
|
|
{ |
59
|
|
|
$key = '10th Thing'; |
60
|
|
|
$index = null; |
61
|
|
|
$parsedKey = 'Thing'; |
62
|
|
|
$parsedIndex = 9; |
63
|
|
|
$value = 'Some Value'; |
64
|
|
|
$exception = new OutOfBoundsException("Entry '$key' was not found in the store."); |
65
|
|
|
|
66
|
|
|
/* @noinspection PhpUndefinedMethodInspection */ |
67
|
|
|
{ |
68
|
|
|
Phake::expect($this->key, 1)->parse($key, $index)->thenReturn([$parsedKey, $parsedIndex]); |
|
|
|
|
69
|
|
|
Phake::expect($this->assert, 1)->keyExists($parsedKey, $parsedIndex)->thenThrow($exception); |
|
|
|
|
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$this->expectException(get_class($exception)); |
73
|
|
|
$this->expectExceptionMessage($exception->getMessage()); |
74
|
|
|
|
75
|
|
|
$this->assert->keyValueContains($key, $value, $index); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function testFailedMatchThrowsRuntimeException() |
79
|
|
|
{ |
80
|
|
|
$key = '10th Thing'; |
81
|
|
|
$index = null; |
82
|
|
|
$parsedKey = 'Thing'; |
83
|
|
|
$parsedIndex = 9; |
84
|
|
|
$value = 'Some Value'; |
85
|
|
|
$exception = new RuntimeException("Entry '$key' does not contain '$value'"); |
86
|
|
|
|
87
|
|
|
/* @noinspection PhpUndefinedMethodInspection */ |
88
|
|
|
{ |
89
|
|
|
Phake::expect($this->key, 1)->parse($key, $index)->thenReturn([$parsedKey, $parsedIndex]); |
|
|
|
|
90
|
|
|
Phake::expect($this->assert, 1)->keyExists($parsedKey, $parsedIndex)->thenReturn(null); |
|
|
|
|
91
|
|
|
Phake::expect($this->store, 1)->keyValueContains($parsedKey, $value, $parsedIndex)->thenReturn(false); |
|
|
|
|
92
|
|
|
Phake::expect($this->key, 1)->build($parsedKey, $parsedIndex)->thenReturn($key); |
|
|
|
|
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$this->expectException(get_class($exception)); |
96
|
|
|
$this->expectExceptionMessage($exception->getMessage()); |
97
|
|
|
|
98
|
|
|
$this->assert->keyValueContains($key, $value, $index); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function testSuccessfulMatchThrowsNoException() |
102
|
|
|
{ |
103
|
|
|
$key = '10th Thing'; |
104
|
|
|
$index = null; |
105
|
|
|
$parsedKey = 'Thing'; |
106
|
|
|
$parsedIndex = 9; |
107
|
|
|
$value = 'Some Value'; |
108
|
|
|
|
109
|
|
|
/* @noinspection PhpUndefinedMethodInspection */ |
110
|
|
|
{ |
111
|
|
|
Phake::expect($this->key, 1)->parse($key, $index)->thenReturn([$parsedKey, $parsedIndex]); |
|
|
|
|
112
|
|
|
Phake::expect($this->assert, 1)->keyExists($parsedKey, $parsedIndex)->thenReturn(null); |
|
|
|
|
113
|
|
|
Phake::expect($this->store, 1)->keyValueContains($parsedKey, $value, $parsedIndex)->thenReturn(true); |
|
|
|
|
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
$this->assert->keyValueContains($key, $value, $index); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|