|
1
|
|
|
<?php namespace Chekote\NounStore; |
|
2
|
|
|
|
|
3
|
|
|
use InvalidArgumentException; |
|
4
|
|
|
use OutOfBoundsException; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* Makes assertions regarding store data. |
|
8
|
|
|
*/ |
|
9
|
|
|
class Assert |
|
10
|
|
|
{ |
|
11
|
|
|
protected Store $store; |
|
12
|
|
|
|
|
13
|
|
|
protected Key $keyService; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Assert constructor. |
|
17
|
|
|
* |
|
18
|
|
|
* @param Store $store the store to assert against. |
|
19
|
|
|
* @param Key|null $keyService the keyService to use for key parsing/building. Will use the default Key service |
|
20
|
|
|
* if not specified. |
|
21
|
|
|
* |
|
22
|
|
|
* @codeCoverageIgnore |
|
23
|
|
|
*/ |
|
24
|
|
|
public function __construct(Store $store, Key $keyService = null) |
|
25
|
|
|
{ |
|
26
|
|
|
$this->store = $store; |
|
27
|
|
|
$this->keyService = $keyService ?: Key::getInstance(); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Asserts that a value has been stored for the specified key. |
|
32
|
|
|
* |
|
33
|
|
|
* @see Key::build() |
|
34
|
|
|
* @see Key::parseNoun() |
|
35
|
|
|
* @param string $key The key to check. Supports nth notation. |
|
36
|
|
|
* @throws OutOfBoundsException if a value has not been stored for the specified key. |
|
37
|
|
|
* @throws InvalidArgumentException if both an $index and $key are provided, but the $key contains an nth value |
|
38
|
|
|
* that does not match the index. |
|
39
|
|
|
* @return mixed The value. |
|
40
|
|
|
*/ |
|
41
|
|
|
public function keyExists($key): mixed |
|
42
|
|
|
{ |
|
43
|
4 |
|
if (!$this->store->keyExists($key)) { |
|
44
|
|
|
throw new OutOfBoundsException("Entry '$key' was not found in the store."); |
|
45
|
4 |
|
} |
|
46
|
|
|
|
|
47
|
3 |
|
return $this->store->get($key); |
|
48
|
1 |
|
} |
|
49
|
1 |
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Asserts that the key's value contains the specified string. |
|
52
|
|
|
* |
|
53
|
2 |
|
* @see Key::build() |
|
54
|
|
|
* @see Key::parseNoun() |
|
55
|
|
|
* @param string $key The key to check. Supports nth notation. |
|
56
|
|
|
* @param string $needle The value expected to be contained within the key's value. |
|
57
|
|
|
* @throws AssertionFailedException If the key's value does not contain the specified string. |
|
58
|
|
|
* @throws OutOfBoundsException If a value has not been stored for the specified key. |
|
59
|
|
|
* @throws InvalidArgumentException if both an $index and $key are provided, but the $key contains an nth value |
|
60
|
|
|
* that does not match the index. |
|
61
|
|
|
* @return mixed The key's value. |
|
62
|
|
|
*/ |
|
63
|
|
|
public function keyValueContains($key, $needle): mixed |
|
64
|
|
|
{ |
|
65
|
|
|
$haystack = $this->keyExists($key); |
|
66
|
|
|
|
|
67
|
5 |
|
if (!$this->store->keyValueContains($key, $needle)) { |
|
68
|
|
|
throw new AssertionFailedException("Entry '$key' does not contain '$needle'"); |
|
69
|
5 |
|
} |
|
70
|
|
|
|
|
71
|
4 |
|
return $haystack; |
|
72
|
|
|
} |
|
73
|
3 |
|
|
|
74
|
1 |
|
/** |
|
75
|
1 |
|
* Asserts that the key's value matches the specified value. |
|
76
|
|
|
* |
|
77
|
|
|
* @see Key::build() |
|
78
|
2 |
|
* @see Key::parseNoun() |
|
79
|
|
|
* @param string $key The key to check. Supports nth notation. |
|
80
|
|
|
* @param mixed $value The expected value. |
|
81
|
|
|
* @throws AssertionFailedException If the key's value does not match the specified value. |
|
82
|
|
|
* @throws OutOfBoundsException If a value has not been stored for the specified key. |
|
83
|
|
|
* @throws InvalidArgumentException if both an $index and $key are provided, but the $key contains an nth value |
|
84
|
|
|
* that does not match the index. |
|
85
|
|
|
*/ |
|
86
|
|
|
public function keyValueIs($key, $value): void |
|
87
|
|
|
{ |
|
88
|
|
|
if ($this->keyExists($key) != $value) { |
|
89
|
|
|
throw new AssertionFailedException("Entry '$key' does not match '" . print_r($value, true) . "'"); |
|
|
|
|
|
|
90
|
|
|
} |
|
91
|
5 |
|
} |
|
92
|
|
|
|
|
93
|
5 |
|
/** |
|
94
|
|
|
* Asserts that the key's value matches the specified class instance. |
|
95
|
4 |
|
* |
|
96
|
|
|
* @template T |
|
97
|
3 |
|
* @see Key::build() |
|
98
|
1 |
|
* @see Key::parseNoun() |
|
99
|
1 |
|
* @param string $key The key to check. Supports nth notation. |
|
100
|
|
|
* @param class-string<T> $class The expected class instance. |
|
|
|
|
|
|
101
|
|
|
* @throws AssertionFailedException If the key's value is not an instance of the specified class. |
|
102
|
2 |
|
* @throws OutOfBoundsException If a value has not been stored for the specified key. |
|
103
|
|
|
* @return T The key's value. |
|
104
|
|
|
*/ |
|
105
|
|
|
public function keyIsClass(string $key, string $class): mixed |
|
106
|
|
|
{ |
|
107
|
|
|
if ($this->keyExists($key) && !$this->store->keyIsClass($key, $class)) { |
|
108
|
|
|
throw new AssertionFailedException("Entry '$key' does not match instance of '$class'"); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
return $this->store->get($key); |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|