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

KeyValueContainsTest::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::keyValueContains()
9
 * @covers \Chekote\NounStore\Assert::keyExists()
10
 */
11
class KeyValueContainsTest extends AssertTest
12
{
13
    public function testKeyIsParsedAndParsedValuesAreUsed()
14
    {
15
        $key = '10th Thing';
16
        $index = null;
17
        $parsedKey = 'Thing';
18
        $parsedIndex = 9;
19
        $value = 'Some Value';
20
21
        /* @noinspection PhpUndefinedMethodInspection */
22
        {
23
            $this->key->parse($key, $index)->willReturn([$parsedKey, $parsedIndex])->shouldBeCalledTimes(1);
24
25
            /* should not be necessary, but we can't mock out Assert::keyExists with Prophecy */
26
            $this->key->parse($parsedKey, $parsedIndex)->willReturn([$parsedKey, $parsedIndex])->shouldBeCalledTimes(1);
27
            $this->store->keyExists($parsedKey, $parsedIndex)->willReturn(true)->shouldBeCalledTimes(1);
28
            $this->store->get($parsedKey, $parsedIndex)->willReturn($value)->shouldBeCalledTimes(1);
29
30
            $this->store->keyValueContains($parsedKey, $value, $parsedIndex)->willReturn(true)->shouldBeCalledTimes(1);
31
        }
32
33
        $this->assert->keyValueContains($key, $value, $index);
34
    }
35
36
    public function testInvalidArgumentExceptionBubblesUpFromParse()
37
    {
38
        $key = '10th Thing';
39
        $index = 5;
40
        $value = 'Some Value';
41
        $exception = new InvalidArgumentException(
42
            "$index was provided for index param when key '$key' contains an nth value, but they do not match"
43
        );
44
45
        /* @noinspection PhpUndefinedMethodInspection */
46
        $this->key->parse($key, $index)->willThrow($exception)->shouldBeCalledTimes(1);
47
48
        $this->expectException(get_class($exception));
49
        $this->expectExceptionMessage($exception->getMessage());
50
51
        $this->assert->keyValueContains($key, $value, $index);
52
    }
53
54
    public function testMissingKeyThrowsOutOfBoundsException()
55
    {
56
        $key = '10th Thing';
57
        $index = null;
58
        $parsedKey = 'Thing';
59
        $parsedIndex = 9;
60
        $value = 'Some Value';
61
        $exception = new OutOfBoundsException("Entry '$key' was not found in the store.");
62
63
        /* @noinspection PhpUndefinedMethodInspection */
64
        {
65
            $this->key->parse($key, $index)->willReturn([$parsedKey, $parsedIndex]);
66
67
            /* should not be necessary, but we can't mock out Assert::keyExists with Prophecy */
68
            $this->key->parse($parsedKey, $parsedIndex)->willReturn([$parsedKey, $parsedIndex]);
69
            $this->store->keyExists($parsedKey, $parsedIndex)->willReturn(false);
70
            $this->key->build($parsedKey, $parsedIndex)->willReturn($key);
71
        }
72
73
        $this->expectException(get_class($exception));
74
        $this->expectExceptionMessage($exception->getMessage());
75
76
        $this->assert->keyValueContains($key, $value, $index);
77
    }
78
79
    public function testFailedMatchThrowsRuntimeException()
80
    {
81
        $key = '10th Thing';
82
        $index = null;
83
        $parsedKey = 'Thing';
84
        $parsedIndex = 9;
85
        $expectedValue = 'Some Value';
86
        $storedValue = 'Something completely different';
87
        $exception = new RuntimeException("Entry '$key' does not contain '$expectedValue'");
88
89
        /* @noinspection PhpUndefinedMethodInspection */
90
        {
91
            $this->key->parse($key, $index)->willReturn([$parsedKey, $parsedIndex])->shouldBeCalledTimes(1);
92
93
            // mock out behavior of Assert::keyExists
94
            $this->key->parse($parsedKey, $parsedIndex)->willReturn([$parsedKey, $parsedIndex])->shouldBeCalledTimes(1);
95
            $this->store->keyExists($parsedKey, $parsedIndex)->willReturn(true)->shouldBeCalledTimes(1);
96
            $this->store->get($parsedKey, $parsedIndex)->willReturn($storedValue)->shouldBeCalledTimes(1);
97
98
            $this->store->keyValueContains($parsedKey, $expectedValue, $parsedIndex)->willReturn(false)->shouldBeCalledTimes(1);
99
            $this->key->build($parsedKey, $parsedIndex)->willReturn($key)->shouldBeCalledTimes(1);
100
        }
101
102
        $this->expectException(get_class($exception));
103
        $this->expectExceptionMessage($exception->getMessage());
104
105
        $this->assert->keyValueContains($key, $expectedValue, $index);
106
    }
107
108
    public function testSuccessfulMatchThrowsNoException()
109
    {
110
        $key = '10th Thing';
111
        $index = null;
112
        $parsedKey = 'Thing';
113
        $parsedIndex = 9;
114
        $storedValue = 'This contains Some Value. Yes it does';
115
        $expectedValue = 'Some Value';
116
117
        /* @noinspection PhpUndefinedMethodInspection */
118
        {
119
            $this->key->parse($key, $index)->willReturn([$parsedKey, $parsedIndex])->shouldBeCalledTimes(1);
120
121
            // mock out behavior of Assert::keyExists
122
            $this->key->parse($parsedKey, $parsedIndex)->willReturn([$parsedKey, $parsedIndex])->shouldBeCalledTimes(1);
123
            $this->store->keyExists($parsedKey, $parsedIndex)->willReturn(true)->shouldBeCalledTimes(1);
124
            $this->store->get($parsedKey, $parsedIndex)->willReturn($storedValue)->shouldBeCalledTimes(1);
125
126
            $this->store->keyValueContains($parsedKey, $expectedValue, $parsedIndex)->willReturn(true)->shouldBeCalledTimes(1);
127
        }
128
129
        $this->assert->keyValueContains($key, $expectedValue, $index);
130
    }
131
}
132