Passed
Branch scrutinizer_new_php_analysis (b739aa)
by Donald
02:21
created

KeyValueIsTest::testKeyValueIsExceptionScenario()   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 Chekote\NounStore\Store\StoreTest;
4
use Chekote\Phake\Phake;
5
use InvalidArgumentException;
6
use OutOfBoundsException;
7
use RuntimeException;
8
9
/**
10
 * @covers \Chekote\NounStore\Assert::keyValueIs()
11
 */
12
class KeyValueIsTest extends AssertTest
13
{
14
    public function setUp()
15
    {
16
        parent::setUp();
17
18
        /* @noinspection PhpUndefinedMethodInspection */
19
        Phake::when($this->assert)->keyValueIs(Phake::anyParameters())->thenCallParent();
0 ignored issues
show
Bug introduced by
It seems like $this->assert can also be of type Chekote\NounStore\Assert; however, parameter $mock of Phake::when() does only seem to accept Phake_IMock, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

19
        Phake::when(/** @scrutinizer ignore-type */ $this->assert)->keyValueIs(Phake::anyParameters())->thenCallParent();
Loading history...
20
    }
21
22
    public function successScenariosDataProvider()
23
    {
24
        return [
25
        //  key,                      index, parsed key,     parsed index, value
26
            ['1st ' . StoreTest::KEY,  null, StoreTest::KEY,            0, StoreTest::FIRST_VALUE       ], // key with nth
27
            ['2nd ' . StoreTest::KEY,  null, StoreTest::KEY,            1, StoreTest::SECOND_VALUE      ], // key with nth
28
            [StoreTest::KEY,           null, StoreTest::KEY,         null, StoreTest::MOST_RECENT_VALUE ], // key without nth
29
            [StoreTest::KEY,              0, StoreTest::KEY,            0, StoreTest::FIRST_VALUE       ], // with index
30
            [StoreTest::KEY,              1, StoreTest::KEY,            1, StoreTest::SECOND_VALUE      ], // with index
31
        ];
32
    }
33
34
    public function parseExceptionDataProvider()
35
    {
36
        return [
37
        //   key,                     index, value, exception class,                 exception message
38
            ['1st ' . StoreTest::KEY,     1, null,  InvalidArgumentException::class, "1 was provided for index param when key '1st " . StoreTest::KEY . "' contains an nth value, but they do not match"],
39
        ];
40
    }
41
42
    public function keyExistsExceptionDataProvider()
43
    {
44
        return [
45
        //   key,                     index, parsed key,     parsed index, value, exception class,             exception message
46
            ['3rd ' . StoreTest::KEY,  null, StoreTest::KEY,            2, null,  OutOfBoundsException::class, "Entry '3rd " . StoreTest::KEY . "' was not found in the store." ],
47
            ['No Such Key',            null, StoreTest::KEY,            0, null,  OutOfBoundsException::class, "Entry 'No Such Key' was not found in the store."                ],
48
        ];
49
    }
50
51
    public function keyValueIsExceptionDataProvider()
52
    {
53
        return [
54
        //   key,                     index, parsed key,     parsed index, built key,               value,                   exception class,         exception message
55
            ['1st ' . StoreTest::KEY,  null, StoreTest::KEY,            0, '1st ' . StoreTest::KEY, StoreTest::SECOND_VALUE, RuntimeException::class, "Entry '1st " . StoreTest::KEY . "' does not match '" . StoreTest::SECOND_VALUE . "'" ],
56
            ['2nd ' . StoreTest::KEY,  null, StoreTest::KEY,            1, '2nd ' . StoreTest::KEY, StoreTest::FIRST_VALUE,  RuntimeException::class, "Entry '2nd " . StoreTest::KEY . "' does not match '" . StoreTest::FIRST_VALUE . "'"  ],
57
        ];
58
    }
59
60
    /**
61
     * Executes a success scenario against the method.
62
     *
63
     * @dataProvider successScenariosDataProvider
64
     * @param string $key         the key to pass to the method.
65
     * @param mixed  $value       the value to pass to the method.
66
     * @param string $parsedKey   the key that the mocked parsed method should return.
67
     * @param int    $parsedIndex the index that the mocked parsed method should return.
68
     * @param int    $index       the index to pass to the method.
69
     */
70
    public function testSuccessScenario($key, $index, $parsedKey, $parsedIndex, $value)
71
    {
72
        /* @noinspection PhpUndefinedMethodInspection */
73
        {
74
            Phake::when($this->key)->parse($key, $index)->thenReturn([$parsedKey, $parsedIndex]);
0 ignored issues
show
Bug introduced by
It seems like $this->key can also be of type Chekote\NounStore\Key; however, parameter $mock of Phake::when() does only seem to accept Phake_IMock, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

74
            Phake::when(/** @scrutinizer ignore-type */ $this->key)->parse($key, $index)->thenReturn([$parsedKey, $parsedIndex]);
Loading history...
75
            Phake::when($this->assert)->keyExists($parsedKey, $parsedIndex)->thenReturn(true);
0 ignored issues
show
Bug introduced by
It seems like $this->assert can also be of type Chekote\NounStore\Assert; however, parameter $mock of Phake::when() does only seem to accept Phake_IMock, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

75
            Phake::when(/** @scrutinizer ignore-type */ $this->assert)->keyExists($parsedKey, $parsedIndex)->thenReturn(true);
Loading history...
76
            Phake::when($this->store)->get($parsedKey, $parsedIndex)->thenReturn($value);
0 ignored issues
show
Bug introduced by
It seems like $this->store can also be of type Chekote\NounStore\Store; however, parameter $mock of Phake::when() does only seem to accept Phake_IMock, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

76
            Phake::when(/** @scrutinizer ignore-type */ $this->store)->get($parsedKey, $parsedIndex)->thenReturn($value);
Loading history...
77
        }
78
79
        $this->assert->KeyValueIs($key, $value, $index);
0 ignored issues
show
Bug introduced by
The method KeyValueIs() does not exist on Phake_IMock. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

79
        $this->assert->/** @scrutinizer ignore-call */ 
80
                       KeyValueIs($key, $value, $index);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
80
    }
81
82
    /**
83
     * Executes a failure scenario caused by the parse method.
84
     *
85
     * @dataProvider parseExceptionDataProvider
86
     * @param string $key              the key to pass to the method.
87
     * @param int    $index            the index to pass to the method.
88
     * @param mixed  $value            the value to pass to the method.
89
     * @param string $exceptionClass   the expected class of the exception.
90
     * @param string $exceptionMessage the expected message of the exception.
91
     */
92
    public function testParseExceptionScenario($key, $index, $value, $exceptionClass, $exceptionMessage)
93
    {
94
        /* @noinspection PhpUndefinedMethodInspection */
95
        {
96
            Phake::when($this->key)->parse($key, $index)->thenThrow(new $exceptionClass($exceptionMessage));
0 ignored issues
show
Bug introduced by
It seems like $this->key can also be of type Chekote\NounStore\Key; however, parameter $mock of Phake::when() does only seem to accept Phake_IMock, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

96
            Phake::when(/** @scrutinizer ignore-type */ $this->key)->parse($key, $index)->thenThrow(new $exceptionClass($exceptionMessage));
Loading history...
97
        }
98
99
        $this->expectException($exceptionClass);
100
        $this->expectExceptionMessage($exceptionMessage);
101
102
        /* @noinspection PhpUnhandledExceptionInspection */
103
        $this->assert->keyValueIs($key, $value, $index);
0 ignored issues
show
Bug introduced by
The method keyValueIs() does not exist on Phake_IMock. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

103
        $this->assert->/** @scrutinizer ignore-call */ 
104
                       keyValueIs($key, $value, $index);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
104
    }
105
106
    /**
107
     * Executes a failure scenario caused by the keyExists method on the store.
108
     *
109
     * @dataProvider keyExistsExceptionDataProvider
110
     * @param string $key              the key to pass to the method.
111
     * @param int    $index            the index to pass to the method.
112
     * @param string $parsedKey        the key that the mocked parsed method should return.
113
     * @param int    $parsedIndex      the index that the mocked parsed method should return.
114
     * @param mixed  $value            the value to pass to the method.
115
     * @param string $exceptionClass   the expected class of the exception.
116
     * @param string $exceptionMessage the expected message of the exception.
117
     */
118
    public function testKeyExistsExceptionScenario($key, $index, $parsedKey, $parsedIndex, $value, $exceptionClass, $exceptionMessage)
119
    {
120
        /* @noinspection PhpUndefinedMethodInspection */
121
        {
122
            Phake::when($this->key)->parse($key, $index)->thenReturn([$parsedKey, $parsedIndex]);
0 ignored issues
show
Bug introduced by
It seems like $this->key can also be of type Chekote\NounStore\Key; however, parameter $mock of Phake::when() does only seem to accept Phake_IMock, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

122
            Phake::when(/** @scrutinizer ignore-type */ $this->key)->parse($key, $index)->thenReturn([$parsedKey, $parsedIndex]);
Loading history...
123
            Phake::when($this->assert)->keyExists($parsedKey, $parsedIndex)->thenThrow(new $exceptionClass($exceptionMessage));
0 ignored issues
show
Bug introduced by
It seems like $this->assert can also be of type Chekote\NounStore\Assert; however, parameter $mock of Phake::when() does only seem to accept Phake_IMock, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

123
            Phake::when(/** @scrutinizer ignore-type */ $this->assert)->keyExists($parsedKey, $parsedIndex)->thenThrow(new $exceptionClass($exceptionMessage));
Loading history...
124
        }
125
126
        $this->expectException($exceptionClass);
127
        $this->expectExceptionMessage($exceptionMessage);
128
129
        /* @noinspection PhpUnhandledExceptionInspection */
130
        $this->assert->keyValueIs($key, $value, $index);
131
    }
132
133
    /**
134
     * Executes a failure scenario caused by the keyExists method on the store.
135
     *
136
     * @dataProvider keyValueIsExceptionDataProvider
137
     * @param string $key              the key to pass to the method.
138
     * @param int    $index            the index to pass to the method.
139
     * @param string $parsedKey        the key that the mocked parsed method should return.
140
     * @param int    $parsedIndex      the index that the mocked parsed method should return.
141
     * @param mixed  $value            the value to pass to the method.
142
     * @param string $exceptionClass   the expected class of the exception.
143
     * @param string $exceptionMessage the expected message of the exception.
144
     */
145
    public function testKeyValueIsExceptionScenario($key, $index, $parsedKey, $parsedIndex, $builtKey, $value, $exceptionClass, $exceptionMessage)
146
    {
147
        /* @noinspection PhpUndefinedMethodInspection */
148
        {
149
            Phake::when($this->key)->parse($key, $index)->thenReturn([$parsedKey, $parsedIndex]);
0 ignored issues
show
Bug introduced by
It seems like $this->key can also be of type Chekote\NounStore\Key; however, parameter $mock of Phake::when() does only seem to accept Phake_IMock, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

149
            Phake::when(/** @scrutinizer ignore-type */ $this->key)->parse($key, $index)->thenReturn([$parsedKey, $parsedIndex]);
Loading history...
150
            Phake::when($this->assert)->keyExists($parsedKey, $parsedIndex)->thenReturn(true);
0 ignored issues
show
Bug introduced by
It seems like $this->assert can also be of type Chekote\NounStore\Assert; however, parameter $mock of Phake::when() does only seem to accept Phake_IMock, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

150
            Phake::when(/** @scrutinizer ignore-type */ $this->assert)->keyExists($parsedKey, $parsedIndex)->thenReturn(true);
Loading history...
151
            Phake::when($this->store)->get($parsedKey, $parsedIndex)->thenReturn('Something other than $value');
0 ignored issues
show
Bug introduced by
It seems like $this->store can also be of type Chekote\NounStore\Store; however, parameter $mock of Phake::when() does only seem to accept Phake_IMock, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

151
            Phake::when(/** @scrutinizer ignore-type */ $this->store)->get($parsedKey, $parsedIndex)->thenReturn('Something other than $value');
Loading history...
152
            Phake::when($this->key)->build($parsedKey, $parsedIndex)->thenReturn($builtKey);
153
        }
154
155
        $this->expectException($exceptionClass);
156
        $this->expectExceptionMessage($exceptionMessage);
157
158
        /* @noinspection PhpUnhandledExceptionInspection */
159
        $this->assert->keyValueIs($key, $value, $index);
160
    }
161
}
162