Passed
Push — prophecy ( fe525a...e65554 )
by Donald
07:25
created

KeyValueIsTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 2
nc 1
nop 0
1
<?php namespace Chekote\NounStore\Assert;
2
3
use InvalidArgumentException;
4
use OutOfBoundsException;
5
use RuntimeException;
6
7
/**
8
 * @covers \Chekote\NounStore\Assert::keyValueIs()
9
 */
10
class KeyValueIsTest extends AssertTest
11
{
12
    public function testKeyIsParsedAndParsedValuesAreUsed()
13
    {
14
        $key = '10th Thing';
15
        $index = null;
16
        $parsedKey = 'Thing';
17
        $parsedIndex = 9;
18
        $value = 'Some Value';
19
20
        /* @noinspection PhpUndefinedMethodInspection */
21
        {
22
            $this->key->parse($key, $index)->willReturn([$parsedKey, $parsedIndex])->shouldBeCalledTimes(1);
23
24
            // mock out behavior of Assert::keyExists
25
            $this->key->parse($parsedKey, $parsedIndex)->willReturn([$parsedKey, $parsedIndex])->shouldBeCalledTimes(1);
26
            $this->store->keyExists($parsedKey, $parsedIndex)->willReturn(true)->shouldBeCalledTimes(1);
27
28
            $this->store->get($parsedKey, $parsedIndex)->willReturn($value)->shouldBeCalledTimes(2);
29
        }
30
31
        $this->assert->keyValueIs($key, $value, $index);
32
    }
33
34
    public function testInvalidArgumentExceptionBubblesUpFromParse()
35
    {
36
        $key = '10th Thing';
37
        $index = 5;
38
        $value = 'Some Value';
39
        $exception = new InvalidArgumentException(
40
            "$index was provided for index param when key '$key' contains an nth value, but they do not match"
41
        );
42
43
        /* @noinspection PhpUndefinedMethodInspection */
44
        $this->key->parse($key, $index)->willThrow($exception)->shouldBeCalledTimes(1);
45
46
        $this->expectException(get_class($exception));
47
        $this->expectExceptionMessage($exception->getMessage());
48
49
        $this->assert->keyValueIs($key, $value, $index);
50
    }
51
52
    public function testMissingKeyThrowsOutOfBoundsException()
53
    {
54
        $key = '10th Thing';
55
        $index = null;
56
        $parsedKey = 'Thing';
57
        $parsedIndex = 9;
58
        $value = 'Some Value';
59
        $exception = new OutOfBoundsException("Entry '$key' was not found in the store.");
60
61
        /* @noinspection PhpUndefinedMethodInspection */
62
        {
63
            $this->key->parse($key, $index)->willReturn([$parsedKey, $parsedIndex])->shouldBeCalledTimes(1);
64
65
            // mock out behavior of Assert::keyExists
66
            $this->key->parse($parsedKey, $parsedIndex)->willReturn([$parsedKey, $parsedIndex])->shouldBeCalledTimes(1);
67
            $this->store->keyExists($parsedKey, $parsedIndex)->willReturn(false)->shouldBeCalledTimes(1);
68
            $this->key->build($parsedKey, $parsedIndex)->willReturn($key)->shouldBeCalledTimes(1);
69
        }
70
71
        $this->expectException(get_class($exception));
72
        $this->expectExceptionMessage($exception->getMessage());
73
74
        $this->assert->keyValueIs($key, $value, $index);
75
    }
76
77
    public function testFailedMatchThrowsRuntimeException()
78
    {
79
        $key = '10th Thing';
80
        $index = null;
81
        $parsedKey = 'Thing';
82
        $parsedIndex = 9;
83
        $value = 'Some Value';
84
        $exception = new RuntimeException("Entry '$key' does not match '$value'");
85
86
        /* @noinspection PhpUndefinedMethodInspection */
87
        {
88
            $this->key->parse($key, $index)->willReturn([$parsedKey, $parsedIndex])->shouldBeCalledTimes(1);
89
90
            // mock out behavior of Assert::keyExists
91
            $this->key->parse($parsedKey, $parsedIndex)->willReturn([$parsedKey, $parsedIndex])->shouldBeCalledTimes(1);
92
            $this->store->keyExists($parsedKey, $parsedIndex)->willReturn(true)->shouldBeCalledTimes(1);
93
94
            $this->store->get($parsedKey, $parsedIndex)->willReturn('Some Other Value')->shouldBeCalledTimes(2);
95
            $this->key->build($parsedKey, $parsedIndex)->willReturn($key)->shouldBeCalledTimes(1);
96
        }
97
98
        $this->expectException(get_class($exception));
99
        $this->expectExceptionMessage($exception->getMessage());
100
101
        $this->assert->keyValueIs($key, $value, $index);
102
    }
103
104
    public function testSuccessfulMatchThrowsNoException()
105
    {
106
        $key = '10th Thing';
107
        $index = null;
108
        $parsedKey = 'Thing';
109
        $parsedIndex = 9;
110
        $value = 'Some Value';
111
112
        /* @noinspection PhpUndefinedMethodInspection */
113
        {
114
            $this->key->parse($key, $index)->willReturn([$parsedKey, $parsedIndex])->shouldBeCalledTimes(1);
115
116
            // mock out behavior of Assert::keyExists
117
            $this->key->parse($parsedKey, $parsedIndex)->willReturn([$parsedKey, $parsedIndex])->shouldBeCalledTimes(1);
118
            $this->store->keyExists($parsedKey, $parsedIndex)->willReturn(true)->shouldBeCalledTimes(1);
119
120
            $this->store->get($parsedKey, $parsedIndex)->willReturn($value)->shouldBeCalledTimes(2);
121
        }
122
123
        $this->assert->keyValueIs($key, $value, $index);
124
    }
125
}
126