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(); |
|
|
|
|
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); |
|
|
|
|
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); |
|
|
|
|
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|