Passed
Pull Request — 2.0 (#36)
by Donald
02:59 queued 01:27
created

testInvalidArgumentExceptionBubblesUpFromGet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 14
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php namespace Chekote\NounStore\Assert;
2
3
use Chekote\NounStore\Key\KeyTest;
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 testKeyIsParsedAndParsedValuesAreUsed()
22
    {
23
        $key = '10th Thing';
24
25
        /* @noinspection PhpUndefinedMethodInspection */
26
        {
27
            Phake::expect($this->store, 1)->keyExists($key)->thenReturn(true);
0 ignored issues
show
Bug introduced by
The method keyExists() does not exist on Chekote\Phake\Expectation. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

27
            Phake::expect($this->store, 1)->/** @scrutinizer ignore-call */ keyExists($key)->thenReturn(true);
Loading history...
Bug introduced by
It seems like $this->store can also be of type Chekote\NounStore\Store; however, parameter $mock of Chekote\Phake\Phake::expect() 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::expect(/** @scrutinizer ignore-type */ $this->store, 1)->keyExists($key)->thenReturn(true);
Loading history...
28
            Phake::expect($this->store, 1)->get($key)->thenReturn('something');
0 ignored issues
show
Bug introduced by
The method get() does not exist on Chekote\Phake\Expectation. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

28
            Phake::expect($this->store, 1)->/** @scrutinizer ignore-call */ get($key)->thenReturn('something');
Loading history...
29
        }
30
31
        $this->assert->keyExists($key);
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

31
        $this->assert->/** @scrutinizer ignore-call */ 
32
                       keyExists($key);

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...
32
    }
33
34
    public function testInvalidArgumentExceptionBubblesUpFromKeyExists()
35
    {
36
        $exception = new InvalidArgumentException('Key syntax is invalid');
37
38
        /* @noinspection PhpUndefinedMethodInspection */
39
        Phake::expect($this->store, 1)->keyExists(KeyTest::INVALID_KEY)->thenThrow($exception);
0 ignored issues
show
Bug introduced by
It seems like $this->store can also be of type Chekote\NounStore\Store; however, parameter $mock of Chekote\Phake\Phake::expect() 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

39
        Phake::expect(/** @scrutinizer ignore-type */ $this->store, 1)->keyExists(KeyTest::INVALID_KEY)->thenThrow($exception);
Loading history...
40
41
        $this->expectException(get_class($exception));
42
        $this->expectExceptionMessage($exception->getMessage());
43
44
        $this->assert->keyExists(KeyTest::INVALID_KEY);
45
    }
46
47
    // An invalid key should not get past keyExists(), so this should never actually be possible. But we test
48
    // the behavior here to ensure that our method behaves correctly should the impossible ever occur.
49
    public function testInvalidArgumentExceptionBubblesUpFromGet()
50
    {
51
        $exception = new InvalidArgumentException('Key syntax is invalid');
52
53
        /* @noinspection PhpUndefinedMethodInspection */
54
        {
55
            Phake::expect($this->store, 1)->keyExists(KeyTest::INVALID_KEY)->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 Chekote\Phake\Phake::expect() 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

55
            Phake::expect(/** @scrutinizer ignore-type */ $this->store, 1)->keyExists(KeyTest::INVALID_KEY)->thenReturn(true);
Loading history...
56
            Phake::expect($this->store, 1)->get(KeyTest::INVALID_KEY)->thenThrow($exception);
57
        }
58
59
        $this->expectException(get_class($exception));
60
        $this->expectExceptionMessage($exception->getMessage());
61
62
        $this->assert->keyExists(KeyTest::INVALID_KEY);
63
    }
64
65
    public function testMissingKeyThrowsOutOfBoundsException()
66
    {
67
        $key = '10th Thing';
68
69
        $exception = new OutOfBoundsException("Entry '$key' was not found in the store.");
70
71
        /* @noinspection PhpUndefinedMethodInspection */
72
        Phake::expect($this->store, 1)->keyExists($key)->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 Chekote\Phake\Phake::expect() 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

72
        Phake::expect(/** @scrutinizer ignore-type */ $this->store, 1)->keyExists($key)->thenReturn(false);
Loading history...
73
74
        $this->expectException(get_class($exception));
75
        $this->expectExceptionMessage($exception->getMessage());
76
77
        $this->assert->keyExists($key);
78
    }
79
80
    public function testStoredValueIsReturned()
81
    {
82
        $key = '10th Thing';
83
        $value = 'Some Value';
84
85
        /* @noinspection PhpUndefinedMethodInspection */
86
        {
87
            Phake::expect($this->store, 1)->keyExists($key)->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 Chekote\Phake\Phake::expect() 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::expect(/** @scrutinizer ignore-type */ $this->store, 1)->keyExists($key)->thenReturn(true);
Loading history...
88
            Phake::expect($this->store, 1)->get($key)->thenReturn($value);
89
        }
90
91
        $this->assertEquals($value, $this->assert->keyExists($key));
92
    }
93
}
94