|
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
|
|
|
$key = '10th Thing'; |
|
22
|
|
|
$index = null; |
|
23
|
|
|
$parsedKey = 'Thing'; |
|
24
|
|
|
$parsedIndex = 9; |
|
25
|
|
|
|
|
26
|
|
|
/** @noinspection PhpUndefinedMethodInspection */ |
|
27
|
|
|
{ |
|
28
|
|
|
Phake::when($this->key)->parse($key, $index)->thenReturn([$parsedKey, $parsedIndex]); |
|
|
|
|
|
|
29
|
|
|
Phake::when($this->store)->keyExists($parsedKey, $parsedIndex)->thenReturn(true); |
|
|
|
|
|
|
30
|
|
|
Phake::when($this->store)->get($parsedKey, $parsedIndex)->thenReturn('something'); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
$this->assert->keyExists($key, $index); |
|
|
|
|
|
|
34
|
|
|
|
|
35
|
|
|
/** @noinspection PhpUndefinedMethodInspection */ |
|
36
|
|
|
{ |
|
37
|
|
|
Phake::verify($this->key)->parse($key, $index); |
|
|
|
|
|
|
38
|
|
|
Phake::verify($this->store)->keyExists($parsedKey, $parsedIndex); |
|
|
|
|
|
|
39
|
|
|
Phake::verify($this->store)->get($parsedKey, $parsedIndex); |
|
40
|
|
|
} |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function testInvalidArgumentExceptionBubblesUpFromParse() { |
|
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->assert->keyExists($key, $index); |
|
55
|
|
|
}); |
|
56
|
|
|
|
|
57
|
|
|
/** @noinspection PhpUndefinedMethodInspection */ |
|
58
|
|
|
Phake::verify($this->key)->parse($key, $index); |
|
|
|
|
|
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function testMissingKeyThrowsOutOfBoundsException() { |
|
62
|
|
|
$key = '10th Thing'; |
|
63
|
|
|
$index = null; |
|
64
|
|
|
$parsedKey = 'Thing'; |
|
65
|
|
|
$parsedIndex = 9; |
|
66
|
|
|
|
|
67
|
|
|
$exception = new OutOfBoundsException("Entry '$key' was not found in the store."); |
|
68
|
|
|
|
|
69
|
|
|
/** @noinspection PhpUndefinedMethodInspection */ |
|
70
|
|
|
{ |
|
71
|
|
|
Phake::when($this->key)->parse($key, $index)->thenReturn([$parsedKey, $parsedIndex]); |
|
|
|
|
|
|
72
|
|
|
Phake::when($this->store)->keyExists($parsedKey, $parsedIndex)->thenReturn(false); |
|
|
|
|
|
|
73
|
|
|
Phake::when($this->key)->build($parsedKey, $parsedIndex)->thenReturn($key); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
$this->assertException($exception, function () use ($key, $index) { |
|
77
|
|
|
$this->assert->keyExists($key, $index); |
|
78
|
|
|
}); |
|
79
|
|
|
|
|
80
|
|
|
/** @noinspection PhpUndefinedMethodInspection */ |
|
81
|
|
|
{ |
|
82
|
|
|
Phake::verify($this->key)->parse($key, $index); |
|
|
|
|
|
|
83
|
|
|
Phake::verify($this->store)->keyExists($parsedKey, $parsedIndex); |
|
|
|
|
|
|
84
|
|
|
Phake::verify($this->key)->build($parsedKey, $parsedIndex); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function testStoredValueIsReturned() { |
|
89
|
|
|
$key = '10th Thing'; |
|
90
|
|
|
$index = null; |
|
91
|
|
|
$parsedKey = 'Thing'; |
|
92
|
|
|
$parsedIndex = 9; |
|
93
|
|
|
$value = 'Some Value'; |
|
94
|
|
|
|
|
95
|
|
|
/** @noinspection PhpUndefinedMethodInspection */ |
|
96
|
|
|
{ |
|
97
|
|
|
Phake::when($this->key)->parse($key, $index)->thenReturn([$parsedKey, $parsedIndex]); |
|
|
|
|
|
|
98
|
|
|
Phake::when($this->store)->keyExists($parsedKey, $parsedIndex)->thenReturn(true); |
|
|
|
|
|
|
99
|
|
|
Phake::when($this->store)->get($parsedKey, $parsedIndex)->thenReturn($value); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
$this->assertEquals($value, $this->assert->keyExists($key, $index)); |
|
103
|
|
|
|
|
104
|
|
|
/** @noinspection PhpUndefinedMethodInspection */ |
|
105
|
|
|
{ |
|
106
|
|
|
Phake::verify($this->key)->parse($key, $index); |
|
|
|
|
|
|
107
|
|
|
Phake::verify($this->store)->keyExists($parsedKey, $parsedIndex); |
|
|
|
|
|
|
108
|
|
|
Phake::verify($this->store)->get($parsedKey, $parsedIndex); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|