KeyExistsTest   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 29
dl 0
loc 81
rs 10
c 0
b 0
f 0
wmc 6

6 Methods

Rating   Name   Duplication   Size   Complexity  
A testInvalidArgumentExceptionBubblesUpFromKeyExists() 0 11 1
A testStoredValueIsReturned() 0 12 1
A setUp() 0 6 1
A testInvalidArgumentExceptionBubblesUpFromGet() 0 14 1
A testKeyIsParsedAndParsedValuesAreUsed() 0 11 1
A testMissingKeyThrowsOutOfBoundsException() 0 13 1
1
<?php namespace Unit\Chekote\NounStore\Assert;
2
3
use InvalidArgumentException;
4
use OutOfBoundsException;
5
use Unit\Chekote\NounStore\Key\KeyTestCase;
6
use Unit\Chekote\Phake\Phake;
7
8
/**
9
 * @covers \Chekote\NounStore\Assert::keyExists()
10
 */
11
class KeyExistsTest extends AssertTestCase
12
{
13
    public function setUp(): void
14
    {
15
        parent::setUp();
16
17
        /* @noinspection PhpUndefinedMethodInspection */
18
        Phake::when($this->assert)->keyExists(Phake::anyParameters())->thenCallParent();
19
    }
20
21
    public function testKeyIsParsedAndParsedValuesAreUsed(): void
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 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

27
            Phake::expect($this->store, 1)->/** @scrutinizer ignore-call */ 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 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->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 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

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...
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(): void
35
    {
36
        $exception = new InvalidArgumentException('Key syntax is invalid');
37
38
        /* @noinspection PhpUndefinedMethodInspection */
39
        Phake::expect($this->store, 1)->keyExists(KeyTestCase::INVALID_KEY)->thenThrow($exception);
40
41
        $this->expectException(get_class($exception));
42
        $this->expectExceptionMessage($exception->getMessage());
43
44
        $this->assert->keyExists(KeyTestCase::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(): void
50
    {
51
        $exception = new InvalidArgumentException('Key syntax is invalid');
52
53
        /* @noinspection PhpUndefinedMethodInspection */
54
        {
55
            Phake::expect($this->store, 1)->keyExists(KeyTestCase::INVALID_KEY)->thenReturn(true);
56
            Phake::expect($this->store, 1)->get(KeyTestCase::INVALID_KEY)->thenThrow($exception);
57
        }
58
59
        $this->expectException(get_class($exception));
60
        $this->expectExceptionMessage($exception->getMessage());
61
62
        $this->assert->keyExists(KeyTestCase::INVALID_KEY);
63
    }
64
65
    public function testMissingKeyThrowsOutOfBoundsException(): void
66
    {
67
        $key = '11th 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);
73
74
        $this->expectException(get_class($exception));
75
        $this->expectExceptionMessage($exception->getMessage());
76
77
        $this->assert->keyExists($key);
78
    }
79
80
    public function testStoredValueIsReturned(): void
81
    {
82
        $key = '12th Thing';
83
        $value = 'Apple';
84
85
        /* @noinspection PhpUndefinedMethodInspection */
86
        {
87
            Phake::expect($this->store, 1)->keyExists($key)->thenReturn(true);
88
            Phake::expect($this->store, 1)->get($key)->thenReturn($value);
89
        }
90
91
        $this->assertEquals($value, $this->assert->keyExists($key));
92
    }
93
}
94