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

KeyExistsTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
dl 0
loc 99
c 0
b 0
f 0
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A testKeyWithExistingNthReturnsValue() 0 15 1
A setUp() 0 6 1
A testNonExistentIndexThrowsException() 0 20 1
A testExistingIndexReturnsValue() 0 16 1
A testMismatchedNthAndIndexAreRejected() 0 12 1
A testKeyWithNonExistingNthThrowsException() 0 18 1
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
8
/**
9
 * @covers \Chekote\NounStore\Assert::keyExists()
10
 */
11
class KeyExistsTest extends AssertTest
12
{
13
    public function setUp()
14
    {
15
        parent::setUp();
16
17
        /* @noinspection PhpUndefinedMethodInspection */
18
        Phake::when($this->assert)->keyExists(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

18
        Phake::when(/** @scrutinizer ignore-type */ $this->assert)->keyExists(Phake::anyParameters())->thenCallParent();
Loading history...
19
    }
20
21
    public function testMismatchedNthAndIndexAreRejected()
22
    {
23
        $key = '1st Thing';
24
        $index = 1;
25
26
        /* @noinspection PhpUndefinedMethodInspection */
27
        Phake::when($this->key)->parse($key, $index)->thenThrow(new InvalidArgumentException());
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

27
        Phake::when(/** @scrutinizer ignore-type */ $this->key)->parse($key, $index)->thenThrow(new InvalidArgumentException());
Loading history...
28
29
        $this->expectException(InvalidArgumentException::class);
30
31
        /* @noinspection PhpUnhandledExceptionInspection */
32
        $this->assert->keyExists($key, $index);
0 ignored issues
show
Bug introduced by
The method keyExists() 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

32
        $this->assert->/** @scrutinizer ignore-call */ 
33
                       keyExists($key, $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...
33
    }
34
35
    public function testKeyWithExistingNthReturnsValue()
36
    {
37
        $key = '1st ' . StoreTest::KEY;
38
        $parsedKey = StoreTest::KEY;
39
        $parsedIndex = 0;
40
41
        /* @noinspection PhpUndefinedMethodInspection */
42
        {
43
            Phake::when($this->key)->parse($key, null)->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

43
            Phake::when(/** @scrutinizer ignore-type */ $this->key)->parse($key, null)->thenReturn([$parsedKey, $parsedIndex]);
Loading history...
44
            Phake::when($this->store)->keyExists($parsedKey, $parsedIndex)->thenReturn(true);
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

44
            Phake::when(/** @scrutinizer ignore-type */ $this->store)->keyExists($parsedKey, $parsedIndex)->thenReturn(true);
Loading history...
45
            Phake::when($this->store)->get($parsedKey, $parsedIndex)->thenReturn(StoreTest::FIRST_VALUE);
46
        }
47
48
        /* @noinspection PhpUnhandledExceptionInspection */
49
        $this->assertEquals(StoreTest::FIRST_VALUE, $this->assert->keyExists($key));
50
    }
51
52
    public function testKeyWithNonExistingNthThrowsException()
53
    {
54
        $key = '3rd ' . StoreTest::KEY;
55
        $parsedKey = StoreTest::KEY;
56
        $parsedIndex = 2;
57
58
        /* @noinspection PhpUndefinedMethodInspection */
59
        {
60
            Phake::when($this->key)->parse($key, null)->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

60
            Phake::when(/** @scrutinizer ignore-type */ $this->key)->parse($key, null)->thenReturn([$parsedKey, $parsedIndex]);
Loading history...
61
            Phake::when($this->store)->keyExists($parsedKey, $parsedIndex)->thenReturn(false);
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

61
            Phake::when(/** @scrutinizer ignore-type */ $this->store)->keyExists($parsedKey, $parsedIndex)->thenReturn(false);
Loading history...
62
            Phake::when($this->key)->build($parsedKey, $parsedIndex)->thenReturn($key);
63
        }
64
65
        $this->expectException(OutOfBoundsException::class);
66
        $this->expectExceptionMessage("Entry '$key' was not found in the store.");
67
68
        /* @noinspection PhpUnhandledExceptionInspection */
69
        $this->assert->keyExists($key);
70
    }
71
72
    public function testExistingIndexReturnsValue()
73
    {
74
        $key = StoreTest::KEY;
75
        $index = 0;
76
        $parsedKey = StoreTest::KEY;
77
        $parsedIndex = 0;
78
79
        /* @noinspection PhpUndefinedMethodInspection */
80
        {
81
            Phake::when($this->key)->parse($key, null)->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

81
            Phake::when(/** @scrutinizer ignore-type */ $this->key)->parse($key, null)->thenReturn([$parsedKey, $parsedIndex]);
Loading history...
82
            Phake::when($this->store)->keyExists($parsedKey, $parsedIndex)->thenReturn(true);
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

82
            Phake::when(/** @scrutinizer ignore-type */ $this->store)->keyExists($parsedKey, $parsedIndex)->thenReturn(true);
Loading history...
83
            Phake::when($this->store)->get($parsedKey, $parsedIndex)->thenReturn(StoreTest::FIRST_VALUE);
84
        }
85
86
        /* @noinspection PhpUnhandledExceptionInspection */
87
        $this->assertEquals(StoreTest::FIRST_VALUE, $this->assert->keyExists($key, $index));
88
    }
89
90
    public function testNonExistentIndexThrowsException()
91
    {
92
        $key = StoreTest::KEY;
93
        $index = 2;
94
        $parsedKey = StoreTest::KEY;
95
        $parsedIndex = 2;
96
        $builtKey = '3rd ' . $parsedKey;
97
98
        /* @noinspection PhpUndefinedMethodInspection */
99
        {
100
            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

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

101
            Phake::when(/** @scrutinizer ignore-type */ $this->store)->keyExists($parsedKey, $parsedIndex)->thenReturn(false);
Loading history...
102
            Phake::when($this->key)->build($parsedKey, $parsedIndex)->thenReturn($builtKey);
103
        }
104
105
        $this->expectException(OutOfBoundsException::class);
106
        $this->expectExceptionMessage("Entry '$builtKey' was not found in the store.");
107
108
        /* @noinspection PhpUnhandledExceptionInspection */
109
        $this->assert->keyExists($key, $index);
110
    }
111
}
112