Passed
Pull Request — master (#16)
by Donald
10:46
created

testInvalidArgumentExceptionBubblesUpFromParse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 0
1
<?php namespace Chekote\NounStore\Assert;
2
3
use InvalidArgumentException;
4
use OutOfBoundsException;
5
6
/**
7
 * @covers \Chekote\NounStore\Assert::keyExists()
8
 */
9
class KeyExistsTest extends AssertTest
10
{
11
    public function testKeyIsParsedAndParsedValuesAreUsed()
12
    {
13
        $key = '10th Thing';
14
        $index = null;
15
        $parsedKey = 'Thing';
16
        $parsedIndex = 9;
17
18
        /* @noinspection PhpUndefinedMethodInspection */
19
        {
20
            $this->key->parse($key, $index)->willReturn([$parsedKey, $parsedIndex])->shouldBeCalledTimes(1);
21
            $this->store->keyExists($parsedKey, $parsedIndex)->willReturn(true)->shouldBeCalledTimes(1);
22
            $this->store->get($parsedKey, $parsedIndex)->willReturn('something')->shouldBeCalledTimes(1);
23
        }
24
25
        $this->assert->keyExists($key, $index);
26
    }
27
28
    public function testInvalidArgumentExceptionBubblesUpFromParse()
29
    {
30
        $key = '10th Thing';
31
        $index = 5;
32
        $exception = new InvalidArgumentException(
33
            "$index was provided for index param when key '$key' contains an nth value, but they do not match"
34
        );
35
36
        /* @noinspection PhpUndefinedMethodInspection */
37
        $this->key->parse($key, $index)->willThrow($exception)->shouldBeCalledTimes(1);
38
39
        $this->expectException(get_class($exception));
40
        $this->expectExceptionMessage($exception->getMessage());
41
42
        $this->assert->keyExists($key, $index);
43
    }
44
45
    public function testMissingKeyThrowsOutOfBoundsException()
46
    {
47
        $key = '10th Thing';
48
        $index = null;
49
        $parsedKey = 'Thing';
50
        $parsedIndex = 9;
51
52
        $exception = new OutOfBoundsException("Entry '$key' was not found in the store.");
53
54
        /* @noinspection PhpUndefinedMethodInspection */
55
        {
56
            $this->key->parse($key, $index)->willReturn([$parsedKey, $parsedIndex])->shouldBeCalledTimes(1);
57
            $this->store->keyExists($parsedKey, $parsedIndex)->willReturn(false)->shouldBeCalledTimes(1);
58
            $this->key->build($parsedKey, $parsedIndex)->willReturn($key)->shouldBeCalledTimes(1);
59
        }
60
61
        $this->expectException(get_class($exception));
62
        $this->expectExceptionMessage($exception->getMessage());
63
64
        $this->assert->keyExists($key, $index);
65
    }
66
67
    public function testStoredValueIsReturned()
68
    {
69
        $key = '10th Thing';
70
        $index = null;
71
        $parsedKey = 'Thing';
72
        $parsedIndex = 9;
73
        $value = 'Some Value';
74
75
        /* @noinspection PhpUndefinedMethodInspection */
76
        {
77
            $this->key->parse($key, $index)->willReturn([$parsedKey, $parsedIndex])->shouldBeCalledTimes(1);
78
            $this->store->keyExists($parsedKey, $parsedIndex)->willReturn(true)->shouldBeCalledTimes(1);
79
            $this->store->get($parsedKey, $parsedIndex)->willReturn($value)->shouldBeCalledTimes(1);
80
        }
81
82
        $this->assertEquals($value, $this->assert->keyExists($key, $index));
83
    }
84
}
85