1
|
|
|
<?php namespace Chekote\NounStore\Assert; |
2
|
|
|
|
3
|
|
|
use Chekote\Phake\Phake; |
4
|
|
|
use InvalidArgumentException; |
5
|
|
|
use OutOfBoundsException; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* @covers \Chekote\NounStore\Assert::keyExists() |
9
|
|
|
*/ |
10
|
|
|
class KeyExistsTest extends AssertTest |
11
|
|
|
{ |
12
|
|
|
public function setUp() |
13
|
|
|
{ |
14
|
|
|
parent::setUp(); |
15
|
|
|
|
16
|
|
|
/* @noinspection PhpUndefinedMethodInspection */ |
17
|
|
|
Phake::when($this->assert)->keyExists(Phake::anyParameters())->thenCallParent(); |
|
|
|
|
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
public function testKeyIsParsedAndParsedValuesAreUsed() |
21
|
|
|
{ |
22
|
|
|
$key = '10th Thing'; |
23
|
|
|
$index = null; |
24
|
|
|
$parsedKey = 'Thing'; |
25
|
|
|
$parsedIndex = 9; |
26
|
|
|
|
27
|
|
|
/* @noinspection PhpUndefinedMethodInspection */ |
28
|
|
|
{ |
29
|
|
|
Phake::expect($this->key, 1)->parse($key, $index)->thenReturn([$parsedKey, $parsedIndex]); |
|
|
|
|
30
|
|
|
Phake::expect($this->store, 1)->keyExists($parsedKey, $parsedIndex)->thenReturn(true); |
|
|
|
|
31
|
|
|
Phake::expect($this->store, 1)->get($parsedKey, $parsedIndex)->thenReturn('something'); |
|
|
|
|
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
$this->assert->keyExists($key, $index); |
|
|
|
|
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function testInvalidArgumentExceptionBubblesUpFromParse() |
38
|
|
|
{ |
39
|
|
|
$key = '10th Thing'; |
40
|
|
|
$index = 5; |
41
|
|
|
$exception = new InvalidArgumentException( |
42
|
|
|
"$index was provided for index param when key '$key' contains an nth value, but they do not match" |
43
|
|
|
); |
44
|
|
|
|
45
|
|
|
/* @noinspection PhpUndefinedMethodInspection */ |
46
|
|
|
Phake::expect($this->key, 1)->parse($key, $index)->thenThrow($exception); |
|
|
|
|
47
|
|
|
|
48
|
|
|
$this->expectException(get_class($exception)); |
49
|
|
|
$this->expectExceptionMessage($exception->getMessage()); |
50
|
|
|
|
51
|
|
|
$this->assert->keyExists($key, $index); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function testMissingKeyThrowsOutOfBoundsException() |
55
|
|
|
{ |
56
|
|
|
$key = '10th Thing'; |
57
|
|
|
$index = null; |
58
|
|
|
$parsedKey = 'Thing'; |
59
|
|
|
$parsedIndex = 9; |
60
|
|
|
|
61
|
|
|
$exception = new OutOfBoundsException("Entry '$key' was not found in the store."); |
62
|
|
|
|
63
|
|
|
/* @noinspection PhpUndefinedMethodInspection */ |
64
|
|
|
{ |
65
|
|
|
Phake::expect($this->key, 1)->parse($key, $index)->thenReturn([$parsedKey, $parsedIndex]); |
|
|
|
|
66
|
|
|
Phake::expect($this->store, 1)->keyExists($parsedKey, $parsedIndex)->thenReturn(false); |
|
|
|
|
67
|
|
|
Phake::expect($this->key, 1)->build($parsedKey, $parsedIndex)->thenReturn($key); |
|
|
|
|
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$this->expectException(get_class($exception)); |
71
|
|
|
$this->expectExceptionMessage($exception->getMessage()); |
72
|
|
|
|
73
|
|
|
$this->assert->keyExists($key, $index); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function testStoredValueIsReturned() |
77
|
|
|
{ |
78
|
|
|
$key = '10th Thing'; |
79
|
|
|
$index = null; |
80
|
|
|
$parsedKey = 'Thing'; |
81
|
|
|
$parsedIndex = 9; |
82
|
|
|
$value = 'Some Value'; |
83
|
|
|
|
84
|
|
|
/* @noinspection PhpUndefinedMethodInspection */ |
85
|
|
|
{ |
86
|
|
|
Phake::expect($this->key, 1)->parse($key, $index)->thenReturn([$parsedKey, $parsedIndex]); |
|
|
|
|
87
|
|
|
Phake::expect($this->store, 1)->keyExists($parsedKey, $parsedIndex)->thenReturn(true); |
|
|
|
|
88
|
|
|
Phake::expect($this->store, 1)->get($parsedKey, $parsedIndex)->thenReturn($value); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$this->assertEquals($value, $this->assert->keyExists($key, $index)); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|