1
|
|
|
<?php namespace Chekote\NounStore\Assert; |
2
|
|
|
|
3
|
|
|
use Chekote\NounStore\Store\StoreTest; |
4
|
|
|
use Chekote\Phake\Phake; |
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
use OutOfBoundsException; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @covers \Chekote\NounStore\Assert::keyExists() |
10
|
|
|
*/ |
11
|
|
|
class KeyExistsTest extends AssertTest |
12
|
|
|
{ |
13
|
|
|
public function setUp() |
14
|
|
|
{ |
15
|
|
|
parent::setUp(); |
16
|
|
|
|
17
|
|
|
/* @noinspection PhpUndefinedMethodInspection */ |
18
|
|
|
Phake::when($this->assert)->keyExists(Phake::anyParameters())->thenCallParent(); |
|
|
|
|
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function testMismatchedNthAndIndexAreRejected() |
22
|
|
|
{ |
23
|
|
|
$key = '1st Thing'; |
24
|
|
|
$index = 1; |
25
|
|
|
|
26
|
|
|
/* @noinspection PhpUndefinedMethodInspection */ |
27
|
|
|
Phake::when($this->key)->parse($key, $index)->thenThrow(new InvalidArgumentException()); |
|
|
|
|
28
|
|
|
|
29
|
|
|
$this->expectException(InvalidArgumentException::class); |
30
|
|
|
|
31
|
|
|
/* @noinspection PhpUnhandledExceptionInspection */ |
32
|
|
|
$this->assert->keyExists($key, $index); |
|
|
|
|
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function testKeyWithExistingNthReturnsValue() |
36
|
|
|
{ |
37
|
|
|
$key = '1st ' . StoreTest::KEY; |
38
|
|
|
$parsedKey = StoreTest::KEY; |
39
|
|
|
$parsedIndex = 0; |
40
|
|
|
|
41
|
|
|
/* @noinspection PhpUndefinedMethodInspection */ |
42
|
|
|
{ |
43
|
|
|
Phake::when($this->key)->parse($key, null)->thenReturn([$parsedKey, $parsedIndex]); |
|
|
|
|
44
|
|
|
Phake::when($this->store)->keyExists($parsedKey, $parsedIndex)->thenReturn(true); |
|
|
|
|
45
|
|
|
Phake::when($this->store)->get($parsedKey, $parsedIndex)->thenReturn(StoreTest::FIRST_VALUE); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/* @noinspection PhpUnhandledExceptionInspection */ |
49
|
|
|
$this->assertEquals(StoreTest::FIRST_VALUE, $this->assert->keyExists($key)); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function testKeyWithNonExistingNthThrowsException() |
53
|
|
|
{ |
54
|
|
|
$key = '3rd ' . StoreTest::KEY; |
55
|
|
|
$parsedKey = StoreTest::KEY; |
56
|
|
|
$parsedIndex = 2; |
57
|
|
|
|
58
|
|
|
/* @noinspection PhpUndefinedMethodInspection */ |
59
|
|
|
{ |
60
|
|
|
Phake::when($this->key)->parse($key, null)->thenReturn([$parsedKey, $parsedIndex]); |
|
|
|
|
61
|
|
|
Phake::when($this->store)->keyExists($parsedKey, $parsedIndex)->thenReturn(false); |
|
|
|
|
62
|
|
|
Phake::when($this->key)->build($parsedKey, $parsedIndex)->thenReturn($key); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$this->expectException(OutOfBoundsException::class); |
66
|
|
|
$this->expectExceptionMessage("Entry '$key' was not found in the store."); |
67
|
|
|
|
68
|
|
|
/* @noinspection PhpUnhandledExceptionInspection */ |
69
|
|
|
$this->assert->keyExists($key); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function testExistingIndexReturnsValue() |
73
|
|
|
{ |
74
|
|
|
$key = StoreTest::KEY; |
75
|
|
|
$index = 0; |
76
|
|
|
$parsedKey = StoreTest::KEY; |
77
|
|
|
$parsedIndex = 0; |
78
|
|
|
|
79
|
|
|
/* @noinspection PhpUndefinedMethodInspection */ |
80
|
|
|
{ |
81
|
|
|
Phake::when($this->key)->parse($key, null)->thenReturn([$parsedKey, $parsedIndex]); |
|
|
|
|
82
|
|
|
Phake::when($this->store)->keyExists($parsedKey, $parsedIndex)->thenReturn(true); |
|
|
|
|
83
|
|
|
Phake::when($this->store)->get($parsedKey, $parsedIndex)->thenReturn(StoreTest::FIRST_VALUE); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/* @noinspection PhpUnhandledExceptionInspection */ |
87
|
|
|
$this->assertEquals(StoreTest::FIRST_VALUE, $this->assert->keyExists($key, $index)); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function testNonExistentIndexThrowsException() |
91
|
|
|
{ |
92
|
|
|
$key = StoreTest::KEY; |
93
|
|
|
$index = 2; |
94
|
|
|
$parsedKey = StoreTest::KEY; |
95
|
|
|
$parsedIndex = 2; |
96
|
|
|
$builtKey = '3rd ' . $parsedKey; |
97
|
|
|
|
98
|
|
|
/* @noinspection PhpUndefinedMethodInspection */ |
99
|
|
|
{ |
100
|
|
|
Phake::when($this->key)->parse($key, $index)->thenReturn([$parsedKey, $parsedIndex]); |
|
|
|
|
101
|
|
|
Phake::when($this->store)->keyExists($parsedKey, $parsedIndex)->thenReturn(false); |
|
|
|
|
102
|
|
|
Phake::when($this->key)->build($parsedKey, $parsedIndex)->thenReturn($builtKey); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$this->expectException(OutOfBoundsException::class); |
106
|
|
|
$this->expectExceptionMessage("Entry '$builtKey' was not found in the store."); |
107
|
|
|
|
108
|
|
|
/* @noinspection PhpUnhandledExceptionInspection */ |
109
|
|
|
$this->assert->keyExists($key, $index); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|