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