1
|
|
|
<?php namespace Unit\Chekote\NounStore\Assert; |
2
|
|
|
|
3
|
|
|
use InvalidArgumentException; |
4
|
|
|
use OutOfBoundsException; |
5
|
|
|
use Unit\Chekote\NounStore\Key\KeyTestCase; |
6
|
|
|
use Unit\Chekote\Phake\Phake; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @covers \Chekote\NounStore\Assert::keyExists() |
10
|
|
|
*/ |
11
|
|
|
class KeyExistsTest extends AssertTestCase |
12
|
|
|
{ |
13
|
|
|
public function setUp(): void |
14
|
|
|
{ |
15
|
|
|
parent::setUp(); |
16
|
|
|
|
17
|
|
|
/* @noinspection PhpUndefinedMethodInspection */ |
18
|
|
|
Phake::when($this->assert)->keyExists(Phake::anyParameters())->thenCallParent(); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function testKeyIsParsedAndParsedValuesAreUsed(): void |
22
|
|
|
{ |
23
|
|
|
$key = '10th Thing'; |
24
|
|
|
|
25
|
|
|
/* @noinspection PhpUndefinedMethodInspection */ |
26
|
|
|
{ |
27
|
|
|
Phake::expect($this->store, 1)->keyExists($key)->thenReturn(true); |
|
|
|
|
28
|
|
|
Phake::expect($this->store, 1)->get($key)->thenReturn('something'); |
|
|
|
|
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
$this->assert->keyExists($key); |
|
|
|
|
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function testInvalidArgumentExceptionBubblesUpFromKeyExists(): void |
35
|
|
|
{ |
36
|
|
|
$exception = new InvalidArgumentException('Key syntax is invalid'); |
37
|
|
|
|
38
|
|
|
/* @noinspection PhpUndefinedMethodInspection */ |
39
|
|
|
Phake::expect($this->store, 1)->keyExists(KeyTestCase::INVALID_KEY)->thenThrow($exception); |
40
|
|
|
|
41
|
|
|
$this->expectException(get_class($exception)); |
42
|
|
|
$this->expectExceptionMessage($exception->getMessage()); |
43
|
|
|
|
44
|
|
|
$this->assert->keyExists(KeyTestCase::INVALID_KEY); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
// An invalid key should not get past keyExists(), so this should never actually be possible. But we test |
48
|
|
|
// the behavior here to ensure that our method behaves correctly should the impossible ever occur. |
49
|
|
|
public function testInvalidArgumentExceptionBubblesUpFromGet(): void |
50
|
|
|
{ |
51
|
|
|
$exception = new InvalidArgumentException('Key syntax is invalid'); |
52
|
|
|
|
53
|
|
|
/* @noinspection PhpUndefinedMethodInspection */ |
54
|
|
|
{ |
55
|
|
|
Phake::expect($this->store, 1)->keyExists(KeyTestCase::INVALID_KEY)->thenReturn(true); |
56
|
|
|
Phake::expect($this->store, 1)->get(KeyTestCase::INVALID_KEY)->thenThrow($exception); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$this->expectException(get_class($exception)); |
60
|
|
|
$this->expectExceptionMessage($exception->getMessage()); |
61
|
|
|
|
62
|
|
|
$this->assert->keyExists(KeyTestCase::INVALID_KEY); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function testMissingKeyThrowsOutOfBoundsException(): void |
66
|
|
|
{ |
67
|
|
|
$key = '11th Thing'; |
68
|
|
|
|
69
|
|
|
$exception = new OutOfBoundsException("Entry '$key' was not found in the store."); |
70
|
|
|
|
71
|
|
|
/* @noinspection PhpUndefinedMethodInspection */ |
72
|
|
|
Phake::expect($this->store, 1)->keyExists($key)->thenReturn(false); |
73
|
|
|
|
74
|
|
|
$this->expectException(get_class($exception)); |
75
|
|
|
$this->expectExceptionMessage($exception->getMessage()); |
76
|
|
|
|
77
|
|
|
$this->assert->keyExists($key); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function testStoredValueIsReturned(): void |
81
|
|
|
{ |
82
|
|
|
$key = '12th Thing'; |
83
|
|
|
$value = 'Apple'; |
84
|
|
|
|
85
|
|
|
/* @noinspection PhpUndefinedMethodInspection */ |
86
|
|
|
{ |
87
|
|
|
Phake::expect($this->store, 1)->keyExists($key)->thenReturn(true); |
88
|
|
|
Phake::expect($this->store, 1)->get($key)->thenReturn($value); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$this->assertEquals($value, $this->assert->keyExists($key)); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|