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