1
|
|
|
<?php namespace Chekote\NounStore\Key; |
2
|
|
|
|
3
|
|
|
use Chekote\Phake\Phake; |
4
|
|
|
use InvalidArgumentException; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* @covers \Chekote\NounStore\Key::processMatches() |
8
|
|
|
*/ |
9
|
|
|
class ProcessMatchesTest extends KeyTest |
10
|
|
|
{ |
11
|
|
|
const MATCHES_NO_NTH = ["thing", '', '', 'thing']; |
12
|
|
|
const MATCHES_NO_NTH_RELATIONSHIP = ["thing's address", '', '', 'thing', "'s address", 'address']; |
13
|
|
|
const MATCHES_NTH = ["15th thing", '15th ', '15', 'thing']; |
14
|
|
|
const MATCHES_NTH_AND_RELATION = ["15th thing's address", '15th ', '15', 'thing', "'s address", 'address']; |
15
|
|
|
|
16
|
|
|
public function setUp() |
17
|
|
|
{ |
18
|
|
|
parent::setUp(); |
19
|
|
|
|
20
|
|
|
/* @noinspection PhpUndefinedMethodInspection */ |
21
|
|
|
Phake::when($this->key)->processMatches(Phake::anyParameters())->thenCallParent(); |
|
|
|
|
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function successDataProvider() { |
25
|
|
|
return [ |
26
|
|
|
// index, matches, resolvedIndex, expectedNth, expectedKey, expectedProperty |
27
|
|
|
[null, self::MATCHES_NO_NTH, null, null, 'thing', null ], |
28
|
|
|
[ 1, self::MATCHES_NO_NTH, 1, null, 'thing', null ], |
29
|
|
|
[null, self::MATCHES_NO_NTH_RELATIONSHIP, null, null, 'thing', 'address'], |
30
|
|
|
[ 1, self::MATCHES_NO_NTH_RELATIONSHIP, 1, null, 'thing', 'address'], |
31
|
|
|
[null, self::MATCHES_NTH, 14, 15, 'thing', null ], |
32
|
|
|
[ 14, self::MATCHES_NTH, 14, 15, 'thing', null ], |
33
|
|
|
[null, self::MATCHES_NTH_AND_RELATION, 14, 15, 'thing', 'address'], |
34
|
|
|
[ 14, self::MATCHES_NTH_AND_RELATION, 14, 15, 'thing', 'address'], |
35
|
|
|
]; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @dataProvider successDataProvider() |
40
|
|
|
* @param int|null $index the index to pass to the method. |
41
|
|
|
* @param array $matches the regex matches to pass to the method. |
42
|
|
|
* @param int|null $resolvedIndex the index that mocked resolveIndex() should return. |
43
|
|
|
* @param string $expectedKey the expected key to be returned from the method. |
44
|
|
|
* @param int|null $expectedNth the nth expected to be pulled from the $matches array. |
45
|
|
|
* @param string|null $expectedProperty the expected property to be returned from the method. |
46
|
|
|
*/ |
47
|
|
|
public function testSuccess($index, array $matches, $resolvedIndex, $expectedNth, $expectedKey, $expectedProperty) { |
48
|
|
|
/* @noinspection PhpUndefinedMethodInspection */ |
49
|
|
|
Phake::expect($this->key, 1)->resolveIndex($index, $expectedNth)->thenReturn($resolvedIndex); |
|
|
|
|
50
|
|
|
|
51
|
|
|
/* @noinspection PhpUndefinedMethodInspection */ |
52
|
|
|
$this->assertEquals( |
53
|
|
|
[$expectedKey, $resolvedIndex, $expectedProperty], |
54
|
|
|
Phake::makeVisible($this->key)->processMatches($index, $matches) |
|
|
|
|
55
|
|
|
); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function testExceptionBubblesUpFromResolveIndex() { |
59
|
|
|
$exception = new InvalidArgumentException('nth must be equal to or larger than 1'); |
60
|
|
|
|
61
|
|
|
/* @noinspection PhpUndefinedMethodInspection */ |
62
|
|
|
Phake::expect($this->key, 1)->resolveIndex(Phake::anyParameters())->thenThrow($exception); |
|
|
|
|
63
|
|
|
|
64
|
|
|
$this->expectException(get_class($exception)); |
65
|
|
|
$this->expectExceptionMessage($exception->getMessage()); |
66
|
|
|
|
67
|
|
|
/* @noinspection PhpUndefinedMethodInspection */ |
68
|
|
|
Phake::makeVisible($this->key)->processMatches(null, ["0th thing", '0th ', '0', 'thing']); |
|
|
|
|
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|