Passed
Pull Request — master (#12)
by Donald
02:34
created

Assert::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
ccs 0
cts 0
cp 0
crap 2
rs 10
c 0
b 0
f 0
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 13
    public function keyExists($key, $index = null)
44
    {
45 13
        list($key, $index) = $this->keyService->parse($key, $index);
46
47 12
        if (!$this->store->keyExists($key, $index)) {
48 3
            throw new OutOfBoundsException(
49 3
                "Entry '" . $this->keyService->build($key, $index) . "' was not found in the store."
50
            );
51
        }
52
53 9
        return $this->store->get($key, $index);
54
    }
55
56
    /**
57
     * Asserts that the key's value matches the specified value.
58
     *
59
     * @param  string                   $key   The key to check. @see self::get() for formatting options.
60
     * @param  mixed                    $value The expected 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 the value does not match.
67
     */
68 8
    public function keyValueIs($key, $value, $index = null)
69
    {
70 8
        list($key, $index) = $this->keyService->parse($key, $index);
71
72 7
        if ($this->keyExists($key, $index) != $value) {
73 1
            throw new RuntimeException(
74 1
                "Entry '" . $this->keyService->build($key, $index) . "' does not match '" . print_r($value, true) . "'"
75
            );
76
        }
77 5
    }
78
}
79