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