Passed
Branch scrutinizer_new_php_analysis (b739aa)
by Donald
01:58
created

KeyValueContainsTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
dl 0
loc 105
c 1
b 0
f 0
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A parseExceptionScenariosDataProvider() 0 5 1
A testFailureScenario() 0 9 1
A failureScenariosDataProvider() 0 8 1
A testSuccessScenario() 0 9 1
A testParseExceptionScenario() 0 10 1
A setUp() 0 6 1
A successScenariosDataProvider() 0 9 1
1
<?php namespace Chekote\NounStore\Store;
2
3
use Chekote\Phake\Phake;
4
use InvalidArgumentException;
5
6
/**
7
 * @covers \Chekote\NounStore\Store::keyValueContains()
8
 */
9
class KeyValueContainsTest extends StoreTest
10
{
11
    public function setUp()
12
    {
13
        parent::setUp();
14
15
        /* @noinspection PhpUndefinedMethodInspection */
16
        Phake::when($this->store)->keyValueContains(Phake::anyParameters())->thenCallParent();
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

16
        Phake::when(/** @scrutinizer ignore-type */ $this->store)->keyValueContains(Phake::anyParameters())->thenCallParent();
Loading history...
17
    }
18
19
    public function successScenariosDataProvider()
20
    {
21
        return [
22
        //  key                  index, parsedKey, parsedIndex, storedValue,             checkedValue
23
            ['1st ' . self::KEY,  null, self::KEY,           0, self::FIRST_VALUE,       substr(self::FIRST_VALUE, 0, 2)       ], // key with nth
24
            ['2nd ' . self::KEY,  null, self::KEY,           1, self::SECOND_VALUE,      substr(self::SECOND_VALUE, 0, 2)      ], // key with nth
25
            [self::KEY,           null, self::KEY,        null, self::MOST_RECENT_VALUE, substr(self::MOST_RECENT_VALUE, 0, 2) ], // key without nth
26
            [self::KEY,              0, self::KEY,           0, self::FIRST_VALUE,       substr(self::FIRST_VALUE, 0, 2)       ], // with index
27
            [self::KEY,              1, self::KEY,           1, self::SECOND_VALUE,      substr(self::SECOND_VALUE, 0, 2)      ], // with index
28
        ];
29
    }
30
31
    public function failureScenariosDataProvider()
32
    {
33
        return [
34
        //  key,                 index, parsedKey,     parsedIndex, storedValue,        checkedValue
35
            ['1st ' . self::KEY,  null, self::KEY,               0, self::FIRST_VALUE,  self::SECOND_VALUE ], // value mismatch
36
            ['2nd ' . self::KEY,  null, self::KEY,               1, self::SECOND_VALUE, self::FIRST_VALUE  ], // value mismatch
37
            ['3rd ' . self::KEY,  null, self::KEY,               2, null,               null               ], // no such nth
38
            ['No Such Key',       null, 'No Such Key',        null, null,               null               ], // no such key
39
        ];
40
    }
41
42
    public function parseExceptionScenariosDataProvider()
43
    {
44
        return [
45
        //  key                  value index exception class                  exception message
46
            ['1st ' . self::KEY, null,    1, InvalidArgumentException::class, "1 was provided for index param when key '1st " . self::KEY . "' contains an nth value, but they do not match"],
47
        ];
48
    }
49
50
    /**
51
     * Executes a success scenario against the method.
52
     *
53
     * @dataProvider successScenariosDataProvider
54
     * @param string $key          the key to pass to the keyValueContains() method.
55
     * @param int    $index        the index to pass to the keyValueContains() method.
56
     * @param string $parsedKey    the key that the mocked parse() method should return.
57
     * @param string $parsedIndex  the index that the mocked parse() method should return.
58
     * @param mixed  $storedValue  the value that the mocked get() method should return.
59
     * @param mixed  $checkedValue the value to pass to the keyValueContains() method.
60
     */
61
    public function testSuccessScenario($key, $index, $parsedKey, $parsedIndex, $storedValue, $checkedValue)
62
    {
63
        /* @noinspection PhpUndefinedMethodInspection */
64
        {
65
            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

65
            Phake::when(/** @scrutinizer ignore-type */ $this->key)->parse($key, $index)->thenReturn([$parsedKey, $parsedIndex]);
Loading history...
66
            Phake::when($this->store)->get($parsedKey, $parsedIndex)->thenReturn($storedValue);
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

66
            Phake::when(/** @scrutinizer ignore-type */ $this->store)->get($parsedKey, $parsedIndex)->thenReturn($storedValue);
Loading history...
67
        }
68
69
        $this->assertTrue($this->store->keyValueContains($key, $checkedValue, $index));
0 ignored issues
show
Bug introduced by
The method keyValueContains() 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

69
        $this->assertTrue($this->store->/** @scrutinizer ignore-call */ keyValueContains($key, $checkedValue, $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...
70
    }
71
72
    /**
73
     * Executes a failure scenario against the method.
74
     *
75
     * @dataProvider failureScenariosDataProvider
76
     * @param string $key          the key to pass to the keyValueContains() method.
77
     * @param int    $index        the index to pass to the keyValueContains() method.
78
     * @param string $parsedKey    the key that the mocked parse() method should return.
79
     * @param string $parsedIndex  the index that the mocked parse() method should return.
80
     * @param mixed  $storedValue  the value that the mocked get() method should return.
81
     * @param mixed  $checkedValue the value to pass to the keyValueContains() method.
82
     */
83
    public function testFailureScenario($key, $index, $parsedKey, $parsedIndex, $storedValue, $checkedValue)
84
    {
85
        /* @noinspection PhpUndefinedMethodInspection */
86
        {
87
            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

87
            Phake::when(/** @scrutinizer ignore-type */ $this->key)->parse($key, $index)->thenReturn([$parsedKey, $parsedIndex]);
Loading history...
88
            Phake::when($this->store)->get($parsedKey, $parsedIndex)->thenReturn($storedValue);
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

88
            Phake::when(/** @scrutinizer ignore-type */ $this->store)->get($parsedKey, $parsedIndex)->thenReturn($storedValue);
Loading history...
89
        }
90
91
        $this->assertFalse($this->store->keyValueContains($key, $checkedValue, $index));
92
    }
93
94
    /**
95
     * Executes an exception scenario against the method.
96
     *
97
     * @dataProvider parseExceptionScenariosDataProvider
98
     * @param string $key              the key to pass to the method.
99
     * @param mixed  $value            the value to pass to the method.
100
     * @param int    $index            the index to pass to the method.
101
     * @param string $exceptionClass   the expected class of the exception.
102
     * @param string $exceptionMessage the expected message of the exception.
103
     */
104
    public function testParseExceptionScenario($key, $value, $index, $exceptionClass, $exceptionMessage)
105
    {
106
        /* @noinspection PhpUndefinedMethodInspection */
107
        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

107
        Phake::when(/** @scrutinizer ignore-type */ $this->key)->parse($key, $index)->thenThrow(new $exceptionClass($exceptionMessage));
Loading history...
108
109
        $this->expectException($exceptionClass);
110
        $this->expectExceptionMessage($exceptionMessage);
111
112
        /* @noinspection PhpUnhandledExceptionInspection */
113
        $this->store->keyValueContains($key, $value, $index);
114
    }
115
}
116