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

ParseTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
dl 0
loc 79
c 0
b 0
f 0
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A validKeyAndIndexCombinationsDataProvider() 0 13 1
A setUp() 0 6 1
A testParseKeyParsesValidKeysAndNthCombinations() 0 6 1
A mismatchedKeyAndIndexDataProvider() 0 8 1
A testParseKeyThrowsExceptionIfKeyAndIndexMismatch() 0 8 1
1
<?php namespace Chekote\NounStore\Key;
2
3
use Chekote\Phake\Phake;
4
use InvalidArgumentException;
5
6
/**
7
 * @covers \Chekote\NounStore\Key::parse()
8
 */
9
class ParseTest extends KeyTest
10
{
11
    public function setUp()
12
    {
13
        parent::setUp();
14
15
        /* @noinspection PhpUndefinedMethodInspection */
16
        Phake::when($this->key)->parse(Phake::anyParameters())->thenCallParent();
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

16
        Phake::when(/** @scrutinizer ignore-type */ $this->key)->parse(Phake::anyParameters())->thenCallParent();
Loading history...
17
    }
18
19
    /**
20
     * Provides examples of valid key and index pairs with expected parse results.
21
     *
22
     * @return array
23
     */
24
    public function validKeyAndIndexCombinationsDataProvider()
25
    {
26
        return [
27
        //   key           index   parse parseIndex
28
            ['Thing',       null, 'Thing',     null], // no nth in key or index param
29
            ['1st Thing',   null, 'Thing',        0], // 1st in key with no index param
30
            ['1st Thing',      0, 'Thing',        0], // nth in key with matching index param
31
            ['2nd Thing',   null, 'Thing',        1], // 2nd in key with no index param
32
            ['3rd Thing',   null, 'Thing',        2], // 3rd in key with no index param
33
            ['4th Thing',   null, 'Thing',        3], // 3th in key with no index param
34
            ['478th Thing', null, 'Thing',      477], // high nth in key with no index param
35
            ['Thing',          0, 'Thing',        0], // no nth in key with 0 index param
36
            ['Thing',         49, 'Thing',       49], // no nth in key with high index param
37
        ];
38
    }
39
40
    /**
41
     * Tests that calling Key::parse with valid key and index combinations works correctly.
42
     *
43
     * @dataProvider validKeyAndIndexCombinationsDataProvider
44
     * @param string $key         the key to parse
45
     * @param int    $index       the index to pass along with the key
46
     * @param string $parsedKey   the expected resulting parsed key
47
     * @param int    $parsedIndex the expected resulting parsed index
48
     */
49
    public function testParseKeyParsesValidKeysAndNthCombinations($key, $index, $parsedKey, $parsedIndex)
50
    {
51
        list($actualKey, $actualIndex) = $this->key->parse($key, $index);
0 ignored issues
show
Bug introduced by
The method parse() 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

51
        /** @scrutinizer ignore-call */ 
52
        list($actualKey, $actualIndex) = $this->key->parse($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...
52
53
        $this->assertEquals($parsedKey, $actualKey);
54
        $this->assertEquals($parsedIndex, $actualIndex);
55
    }
56
57
    /**
58
     * Provides examples of mismatched key & index pairs.
59
     *
60
     * @return array
61
     */
62
    public function mismatchedKeyAndIndexDataProvider()
63
    {
64
        return [
65
            ['1st Thing', 1],
66
            ['1st Thing', 2],
67
            ['4th Person', 0],
68
            ['4th Person', 4],
69
            ['4th Person', 10],
70
        ];
71
    }
72
73
    /**
74
     * Tests that calling Key::parse with mismatched key and index param throws an exception.
75
     *
76
     * @dataProvider mismatchedKeyAndIndexDataProvider
77
     * @param string $key   the key to parse
78
     * @param string $index the mismatched index to pass along with the key
79
     */
80
    public function testParseKeyThrowsExceptionIfKeyAndIndexMismatch($key, $index)
81
    {
82
        $this->expectException(InvalidArgumentException::class);
83
        $this->expectExceptionMessage(
84
            "$index was provided for index param when key '$key' contains an nth value, but they do not match"
85
        );
86
87
        $this->key->parse($key, $index);
0 ignored issues
show
Bug introduced by
$index of type string is incompatible with the type integer expected by parameter $index of Chekote\NounStore\Key::parse(). ( Ignorable by Annotation )

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

87
        $this->key->parse($key, /** @scrutinizer ignore-type */ $index);
Loading history...
88
    }
89
}
90