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 successScenarioDataProvider() |
25
|
|
|
{ |
26
|
|
|
return [ |
27
|
|
|
// key parsedKey, parsedIndex |
28
|
|
|
['Thing', 'Thing', null], |
29
|
|
|
['1st Thing', 'Thing', 0], |
30
|
|
|
['2nd Thing', 'Thing', 1], |
31
|
|
|
['3rd Thing', 'Thing', 2], |
32
|
|
|
['4th Thing', 'Thing', 3], |
33
|
|
|
['478th Thing', 'Thing', 477], |
34
|
|
|
]; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Tests that calling Key::parse with valid key works correctly. |
39
|
|
|
* |
40
|
|
|
* @dataProvider successScenarioDataProvider |
41
|
|
|
* @param string $key the key to parse |
42
|
|
|
* @param string $parsedKey the expected resulting parsed key |
43
|
|
|
* @param int $parsedIndex the expected resulting parsed index |
44
|
|
|
*/ |
45
|
|
|
public function testSuccessScenario($key, $parsedKey, $parsedIndex) |
46
|
|
|
{ |
47
|
|
|
$this->assertEquals([$parsedKey, $parsedIndex], $this->key->parse($key)); |
|
|
|
|
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Provides examples of invalid keys. |
52
|
|
|
* |
53
|
|
|
* @return array |
54
|
|
|
*/ |
55
|
|
|
public function invalidKeyDataProvider() |
56
|
|
|
{ |
57
|
|
|
return [ |
58
|
|
|
["Thing's stuff"], |
59
|
|
|
["1st Thing's thingamajig"], |
60
|
|
|
]; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Tests that calling Key::parse with an invalid key throws an exception. |
65
|
|
|
* |
66
|
|
|
* @dataProvider invalidKeyDataProvider |
67
|
|
|
* @param string $key the key to parse |
68
|
|
|
*/ |
69
|
|
|
public function testParseKeyThrowsExceptionIfKeyAndIndexMismatch($key) |
70
|
|
|
{ |
71
|
|
|
$this->expectException(InvalidArgumentException::class); |
72
|
|
|
$this->expectExceptionMessage('Key syntax is invalid'); |
73
|
|
|
|
74
|
|
|
$this->key->parse($key); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|