Passed
Push — master ( 639b92...a2ec98 )
by Donald
13:52
created

ParseKeyTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 77
rs 10
c 0
b 0
f 0
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A validKeyAndNthCombinationsDataProvider() 0 13 1
A mismatchedKeyAndNthDataProvider() 0 8 1
A testParseKeyParsesValidKeysAndNthCombinations() 0 9 1
A testParseKeyThrowsExceptionIfKeyAndNthMismatch() 0 11 1
1
<?php namespace Chekote\NounStore\Store;
2
3
use Chekote\NounStore\Store;
4
use InvalidArgumentException;
5
use ReflectionClass;
6
7
/**
8
 * @covers Store::parseKey()
9
 */
10
class ParseKeyTest extends StoreTest
11
{
12
    /**
13
     * Provides examples of valid key and nth pairs with expected parse results.
14
     *
15
     * @return array
16
     */
17
    public function validKeyAndNthCombinationsDataProvider()
18
    {
19
        return [
20
        //   key             nth   parseKey parseNth
21
            ['Thing',       null, 'Thing',     null], // no nth in key or nth param
22
            ['1st Thing',   null, 'Thing',        0], // 1st in key with no nth param
23
            ['1st Thing',      0, 'Thing',        0], // nth in key with matching nth param
24
            ['2nd Thing',   null, 'Thing',        1], // 2nd in key with no nth param
25
            ['3rd Thing',   null, 'Thing',        2], // 3rd in key with no nth param
26
            ['4th Thing',   null, 'Thing',        3], // 3th in key with no nth param
27
            ['478th Thing', null, 'Thing',      477], // high nth in key with no nth param
28
            ['Thing',          0, 'Thing',        0], // no nth in key with 0 nth param
29
            ['Thing',         49, 'Thing',       49], // no nth in key with high nth param
30
        ];
31
    }
32
33
    /**
34
     * Tests that calling Store::parseKey with valid key and nth combinations works correctly.
35
     *
36
     * @dataProvider validKeyAndNthCombinationsDataProvider
37
     * @param string $key       the key to parse
38
     * @param int    $nth       the nth to pass along with the key
39
     * @param string $parsedKey the expected resulting parsed key
40
     * @param int    $parsedNth the expected resulting parsed nth
41
     */
42
    public function testParseKeyParsesValidKeysAndNthCombinations($key, $nth, $parsedKey, $parsedNth)
43
    {
44
        $parseKey = (new ReflectionClass(Store::class))->getMethod('parseKey');
45
        $parseKey->setAccessible(true);
46
47
        list($actualKey, $actualNth) = $parseKey->invoke($this->store, $key, $nth);
48
49
        $this->assertEquals($parsedKey, $actualKey);
50
        $this->assertEquals($parsedNth, $actualNth);
51
    }
52
53
    /**
54
     * Provides examples of mismatched key & nth pairs.
55
     *
56
     * @return array
57
     */
58
    public function mismatchedKeyAndNthDataProvider()
59
    {
60
        return [
61
            ['1st Thing', 1],
62
            ['1st Thing', 2],
63
            ['4th Person', 0],
64
            ['4th Person', 4],
65
            ['4th Person', 10],
66
        ];
67
    }
68
69
    /**
70
     * Tests that calling Store::parseKey with mismatched key and nth param throws an exception.
71
     *
72
     * @dataProvider mismatchedKeyAndNthDataProvider
73
     * @param string $key the key to parse
74
     * @param string $nth the mismatched nth to pass along with the key
75
     */
76
    public function testParseKeyThrowsExceptionIfKeyAndNthMismatch($key, $nth)
77
    {
78
        $this->expectException(InvalidArgumentException::class);
79
        $this->expectExceptionMessage(
80
            "$nth was provided for nth param when key '$key' contains an nth value, but they do not match"
81
        );
82
83
        $parseKey = (new ReflectionClass(Store::class))->getMethod('parseKey');
84
        $parseKey->setAccessible(true);
85
86
        $parseKey->invoke($this->store, $key, $nth);
87
    }
88
}
89