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

KeyValueContainsTest::testKeyValueContainsExceptionScenario()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 7
nc 1
nop 8

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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
 */
10
class KeyValueContainsTest 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
            $this->assert->keyExists($parsedKey, $parsedIndex)->willReturn(null)->shouldBeCalledTimes(1);
24
            $this->store->keyValueContains($parsedKey, $value, $parsedIndex)->willReturn(true)->shouldBeCalledTimes(1);
25
        }
26
27
        $this->assert->keyValueContains($key, $value, $index);
28
    }
29
30
    public function testInvalidArgumentExceptionBubblesUpFromParse()
31
    {
32
        $key = '10th Thing';
33
        $index = 5;
34
        $value = 'Some Value';
35
        $exception = new InvalidArgumentException(
36
            "$index was provided for index param when key '$key' contains an nth value, but they do not match"
37
        );
38
39
        /* @noinspection PhpUndefinedMethodInspection */
40
        $this->key->parse($key, $index)->willThrow($exception)->shouldBeCalledTimes(1);
41
42
        $this->expectException(get_class($exception));
43
        $this->expectExceptionMessage($exception->getMessage());
44
45
        $this->assert->keyValueContains($key, $value, $index);
46
    }
47
48
    public function testMissingKeyThrowsOutOfBoundsException()
49
    {
50
        $key = '10th Thing';
51
        $index = null;
52
        $parsedKey = 'Thing';
53
        $parsedIndex = 9;
54
        $value = 'Some Value';
55
        $exception = new OutOfBoundsException("Entry '$key' was not found in the store.");
56
57
        /* @noinspection PhpUndefinedMethodInspection */
58
        {
59
            $this->key->parse($key, $index)->willReturn([$parsedKey, $parsedIndex])->shouldBeCalledTimes(1);
60
            $this->assert->keyExists($parsedKey, $parsedIndex)->willThrow($exception)->shouldBeCalledTimes(1);
61
        }
62
63
        $this->expectException(get_class($exception));
64
        $this->expectExceptionMessage($exception->getMessage());
65
66
        $this->assert->keyValueContains($key, $value, $index);
67
    }
68
69
    public function testFailedMatchThrowsRuntimeException()
70
    {
71
        $key = '10th Thing';
72
        $index = null;
73
        $parsedKey = 'Thing';
74
        $parsedIndex = 9;
75
        $value = 'Some Value';
76
        $exception = new RuntimeException("Entry '$key' does not contain '$value'");
77
78
        /* @noinspection PhpUndefinedMethodInspection */
79
        {
80
            $this->key->parse($key, $index)->willReturn([$parsedKey, $parsedIndex])->shouldBeCalledTimes(1);
81
            $this->assert->keyExists($parsedKey, $parsedIndex)->willReturn(null)->shouldBeCalledTimes(1);
82
            $this->store->keyValueContains($parsedKey, $value, $parsedIndex)->willReturn(false)->shouldBeCalledTimes(1);
83
            $this->key->build($parsedKey, $parsedIndex)->willReturn($key)->shouldBeCalledTimes(1);
84
        }
85
86
        $this->expectException(get_class($exception));
87
        $this->expectExceptionMessage($exception->getMessage());
88
89
        $this->assert->keyValueContains($key, $value, $index);
90
    }
91
92
    public function testSuccessfulMatchThrowsNoException()
93
    {
94
        $key = '10th Thing';
95
        $index = null;
96
        $parsedKey = 'Thing';
97
        $parsedIndex = 9;
98
        $value = 'Some Value';
99
100
        /* @noinspection PhpUndefinedMethodInspection */
101
        {
102
            $this->key->parse($key, $index)->willReturn([$parsedKey, $parsedIndex])->shouldBeCalledTimes(1);
103
            $this->assert->keyExists($parsedKey, $parsedIndex)->willReturn(null)->shouldBeCalledTimes(1);
104
            $this->store->keyValueContains($parsedKey, $value, $parsedIndex)->willReturn(true)->shouldBeCalledTimes(1);
105
        }
106
107
        $this->assert->keyValueContains($key, $value, $index);
108
    }
109
}
110