Passed
Branch 2.0 (d24509)
by Donald
02:04
created

Assert::keyExists()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 2
dl 0
loc 11
ccs 6
cts 6
cp 1
crap 2
rs 9.4285
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 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
     */
67 5
    public function keyValueContains($key, $value, $index = null)
68
    {
69 5
        list($key, $index) = $this->keyService->parse($key, $index);
70
71 4
        $this->keyExists($key, $index);
72
73 3
        if (!$this->store->keyValueContains($key, $value, $index)) {
74 1
            throw new RuntimeException(
75 1
                "Entry '" . $this->keyService->build($key, $index) . "' does not contain '$value'"
76
            );
77
        }
78 2
    }
79
80
    /**
81
     * Asserts that the key's value matches the specified value.
82
     *
83
     * @param  string                   $key   The key to check. @see self::get() for formatting options.
84
     * @param  mixed                    $value The expected value.
85
     * @param  int                      $index [optional] The index of the key entry to retrieve. If not specified, the
86
     *                                         method will check the most recent value stored under the key.
87
     * @throws OutOfBoundsException     If a value has not been stored for the specified key.
88
     * @throws InvalidArgumentException if both an $index and $key are provided, but the $key contains an nth value
89
     *                                        that does not match the index.
90
     */
91 5
    public function keyValueIs($key, $value, $index = null)
92
    {
93 5
        list($key, $index) = $this->keyService->parse($key, $index);
94
95 4
        $this->keyExists($key, $index);
96
97 3
        if ($this->store->get($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 2
    }
103
}
104