Passed
Pull Request — master (#56)
by Evandro
07:37
created

php$0 ➔ testFailedMatchThrowsRuntimeException()   A

Complexity

Conditions 1

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 17
rs 9.7
c 0
b 0
f 0
1
<?php namespace Unit\Chekote\NounStore\Assert;
2
3
use InvalidArgumentException;
4
use OutOfBoundsException;
5
use RuntimeException;
6
use Unit\Chekote\NounStore\Key\KeyTest;
7
use Unit\Chekote\Phake\Phake;
8
use stdClass;
9
10
/**
11
 * @covers \Chekote\NounStore\Assert::keyIsClass()
12
 */
13
class KeyIsClassTest extends AssertTest
14
{
15
    public function setUp()
16
    {
17
        parent::setUp();
18
19
        /* @noinspection PhpUndefinedMethodInspection */
20
        Phake::when($this->assert)->keyIsClass(Phake::anyParameters())->thenCallParent();
21
    }
22
23
    public function testInvalidArgumentExceptionBubblesUpFromKeyExists()
24
    {
25
        $exception = new InvalidArgumentException('Key syntax is invalid');
26
27
        /* @noinspection PhpUndefinedMethodInspection */
28
        Phake::expect($this->assert, 1)->keyExists(KeyTest::INVALID_KEY)->thenThrow($exception);
0 ignored issues
show
Bug introduced by
The method keyExists() does not exist on Unit\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->assert, 1)->/** @scrutinizer ignore-call */ keyExists(KeyTest::INVALID_KEY)->thenThrow($exception);
Loading history...
29
30
        $this->expectException(get_class($exception));
31
        $this->expectExceptionMessage($exception->getMessage());
32
33
        $this->assert->keyIsClass(KeyTest::INVALID_KEY, stdClass::class);
0 ignored issues
show
Bug introduced by
The method keyIsClass() does not exist on Unit\Chekote\NounStore\Assert\AssertPhake. ( Ignorable by Annotation )

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

33
        $this->assert->/** @scrutinizer ignore-call */ 
34
                       keyIsClass(KeyTest::INVALID_KEY, stdClass::class);

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...
34
    }
35
36
    // An invalid key should not get past keyExists(), so this should never actually be possible. But we test
37
    // the behavior here to ensure that our method behaves correctly should the impossible ever occur.
38
    public function testInvalidArgumentExceptionBubblesUpFromKeyValueContains()
39
    {
40
        $value = stdClass::class;
41
        $exception = new InvalidArgumentException('Key syntax is invalid');
42
43
        /* @noinspection PhpUndefinedMethodInspection */
44
        {
45
            Phake::expect($this->assert, 1)->keyExists(KeyTest::INVALID_KEY)->thenReturn(true);
46
            Phake::expect($this->store, 1)->keyIsClass(KeyTest::INVALID_KEY, $value)->thenThrow($exception);
0 ignored issues
show
Bug introduced by
The method keyIsClass() does not exist on Unit\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

46
            Phake::expect($this->store, 1)->/** @scrutinizer ignore-call */ keyIsClass(KeyTest::INVALID_KEY, $value)->thenThrow($exception);
Loading history...
47
        }
48
49
        $this->expectException(get_class($exception));
50
        $this->expectExceptionMessage($exception->getMessage());
51
52
        $this->assert->keyIsClass(KeyTest::INVALID_KEY, $value);
53
    }
54
55
    public function testMissingKeyThrowsOutOfBoundsException()
56
    {
57
        $key = '13th Thing';
58
        $value = stdClass::class;
59
        $exception = new OutOfBoundsException("Entry '$key' was not found in the store.");
60
61
        /* @noinspection PhpUndefinedMethodInspection */
62
        Phake::expect($this->assert, 1)->keyExists($key)->thenThrow($exception);
63
64
        $this->expectException(get_class($exception));
65
        $this->expectExceptionMessage($exception->getMessage());
66
67
        $this->assert->keyIsClass($key, $value);
68
    }
69
70
    public function testFailedMatchThrowsRuntimeException()
71
    {
72
        $key = '14th Thing';
73
        $value = stdClass::class;
74
        $exception = new RuntimeException("Entry '$key' does not match instance of '$value'");
75
        $randomClass = new class {};
76
77
        /* @noinspection PhpUndefinedMethodInspection */
78
        {
79
            Phake::expect($this->assert, 1)->keyExists($key)->thenReturn($randomClass);
80
            Phake::expect($this->store, 1)->keyIsClass($key, $value)->thenReturn(false);
81
        }
82
83
        $this->expectException(get_class($exception));
84
        $this->expectExceptionMessage($exception->getMessage());
85
86
        $this->assert->keyIsClass($key, $value);
87
    }
88
89
    public function testSuccessfulMatchThrowsNoException()
90
    {
91
        $key = '15th Thing';
92
        $value = stdClass::class;
93
94
        /* @noinspection PhpUndefinedMethodInspection */
95
        {
96
            Phake::expect($this->assert, 1)->keyExists($key)->thenReturn(new $value());
97
            Phake::expect($this->store, 1)->keyIsClass($key, $value)->thenReturn(true);
98
        }
99
100
        $this->assert->keyIsClass($key, $value);
101
    }
102
}
103