|
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 $keyService the keyService to use for key parsing/building. |
|
23
|
|
|
* |
|
24
|
|
|
* @codeCoverageIgnore |
|
25
|
|
|
*/ |
|
26
|
|
|
public function __construct(Store $store, Key $keyService) |
|
27
|
|
|
{ |
|
28
|
|
|
$this->store = $store; |
|
29
|
|
|
$this->keyService = $keyService; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Asserts that a value has been stored for the specified key. |
|
34
|
|
|
* |
|
35
|
|
|
* @param string $key The key to check. @see self::get() for formatting options. |
|
36
|
|
|
* @param int $index [optional] The index of the key entry to check. If not specified, the |
|
37
|
|
|
* method will ensure that at least one item is stored for the key. |
|
38
|
|
|
* @throws OutOfBoundsException if a value has not been stored for the specified key. |
|
39
|
|
|
* @throws InvalidArgumentException if both an $index and $key are provided, but the $key contains an nth value |
|
40
|
|
|
* that does not match the index. |
|
41
|
|
|
* @return mixed The value. |
|
42
|
|
|
*/ |
|
43
|
4 |
|
public function keyExists($key, $index = null) |
|
44
|
|
|
{ |
|
45
|
4 |
|
list($key, $index) = $this->keyService->parse($key, $index); |
|
46
|
|
|
|
|
47
|
3 |
|
if (!$this->store->keyExists($key, $index)) { |
|
48
|
1 |
|
throw new OutOfBoundsException( |
|
49
|
1 |
|
"Entry '" . $this->keyService->build($key, $index) . "' was not found in the store." |
|
50
|
|
|
); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
2 |
|
return $this->store->get($key, $index); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Asserts that the key's value contains the specified string. |
|
58
|
|
|
* |
|
59
|
|
|
* @param string $key The key to check. @see self::get() for formatting options. |
|
60
|
|
|
* @param string $value The value expected to be contained within the key's value. |
|
61
|
|
|
* @param int $index [optional] The index of the key entry to retrieve. If not specified, the |
|
62
|
|
|
* method will check the most recent value stored under the key. |
|
63
|
|
|
* @throws OutOfBoundsException If a value has not been stored for the specified key. |
|
64
|
|
|
* @throws InvalidArgumentException if both an $index and $key are provided, but the $key contains an nth value |
|
65
|
|
|
* that does not match the index. |
|
66
|
|
|
* @throws RuntimeException If the key exists in the store, but does not contain the specified value. |
|
67
|
|
|
*/ |
|
68
|
|
|
public function keyValueContains($key, $value, $index = null) |
|
69
|
|
|
{ |
|
70
|
|
|
list($key, $index) = $this->keyService->parse($key, $index); |
|
71
|
|
|
|
|
72
|
|
|
$this->keyExists($key, $index); |
|
73
|
|
|
|
|
74
|
|
|
if (!$this->store->keyValueContains($key, $value, $index)) { |
|
75
|
|
|
throw new RuntimeException( |
|
76
|
|
|
"Entry '" . $this->keyService->build($key, $index) . "' does not contain '$value'" |
|
77
|
|
|
); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Asserts that the key's value matches the specified value. |
|
83
|
|
|
* |
|
84
|
|
|
* @param string $key The key to check. @see self::get() for formatting options. |
|
85
|
|
|
* @param mixed $value The expected value. |
|
86
|
|
|
* @param int $index [optional] The index of the key entry to retrieve. If not specified, the |
|
87
|
|
|
* method will check the most recent value stored under the key. |
|
88
|
|
|
* @throws OutOfBoundsException If a value has not been stored for the specified key. |
|
89
|
|
|
* @throws InvalidArgumentException if both an $index and $key are provided, but the $key contains an nth value |
|
90
|
|
|
* that does not match the index. |
|
91
|
|
|
* @throws RuntimeException If the key exists in the store, but the value does not match. |
|
92
|
|
|
*/ |
|
93
|
8 |
|
public function keyValueIs($key, $value, $index = null) |
|
94
|
|
|
{ |
|
95
|
8 |
|
list($key, $index) = $this->keyService->parse($key, $index); |
|
96
|
|
|
|
|
97
|
7 |
|
if ($this->keyExists($key, $index) != $value) { |
|
98
|
1 |
|
throw new RuntimeException( |
|
99
|
1 |
|
"Entry '" . $this->keyService->build($key, $index) . "' does not match '" . print_r($value, true) . "'" |
|
100
|
|
|
); |
|
101
|
|
|
} |
|
102
|
5 |
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|