1
|
|
|
<?php namespace Unit\Chekote\NounStore\Store; |
2
|
|
|
|
3
|
|
|
use InvalidArgumentException; |
4
|
|
|
use Unit\Chekote\NounStore\Key\KeyTestCase; |
5
|
|
|
use Unit\Chekote\Phake\Phake; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* @covers \Chekote\NounStore\Store::get() |
9
|
|
|
*/ |
10
|
|
|
class GetTest extends StoreTestCase |
11
|
|
|
{ |
12
|
|
|
public function setUp(): void |
13
|
|
|
{ |
14
|
|
|
parent::setUp(); |
15
|
|
|
|
16
|
|
|
/* @noinspection PhpUndefinedMethodInspection */ |
17
|
|
|
Phake::when($this->store)->get(Phake::anyParameters())->thenCallParent(); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
public function testKeyIsParsedAndParsedValuesAreUsed(): void |
21
|
|
|
{ |
22
|
|
|
$key = '2nd ' . StoreTestCase::KEY; |
23
|
|
|
$parsedKey = StoreTestCase::KEY; |
24
|
|
|
$parsedIndex = 1; |
25
|
|
|
|
26
|
|
|
/* @noinspection PhpUndefinedMethodInspection */ |
27
|
|
|
Phake::expect($this->key, 1)->parse($key)->thenReturn([[$parsedKey, $parsedIndex]]); |
|
|
|
|
28
|
|
|
|
29
|
|
|
$this->assertEquals(StoreTestCase::$secondValue, $this->store->get($key)); |
|
|
|
|
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function testInvalidArgumentExceptionBubblesUpFromParse() |
33
|
|
|
{ |
34
|
|
|
$exception = new InvalidArgumentException('Key syntax is invalid'); |
35
|
|
|
|
36
|
|
|
/* @noinspection PhpUndefinedMethodInspection */ |
37
|
|
|
Phake::expect($this->key, 1)->parse(KeyTestCase::INVALID_KEY)->thenThrow($exception); |
38
|
|
|
|
39
|
|
|
$this->expectException(get_class($exception)); |
40
|
|
|
$this->expectExceptionMessage($exception->getMessage()); |
41
|
|
|
|
42
|
|
|
$this->store->get(KeyTestCase::INVALID_KEY); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @dataProvider happyPathProvider |
47
|
|
|
* @param string $key the key to fetch. |
48
|
|
|
* @param array[] $parsedKey the parsed key. |
49
|
|
|
* @param mixed $expected the expected value. |
50
|
|
|
*/ |
51
|
|
|
public function testHappyPath(string $key, array $parsedKey, mixed $expected): void |
52
|
|
|
{ |
53
|
|
|
/* @noinspection PhpUndefinedMethodInspection */ |
54
|
|
|
Phake::expect($this->key, 1)->parse($key)->thenReturn($parsedKey); |
55
|
|
|
|
56
|
|
|
$this->assertEquals($expected, $this->store->get($key)); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public static function happyPathProvider(): array |
60
|
|
|
{ |
61
|
|
|
return [ |
62
|
|
|
// key parsed key expected |
63
|
|
|
'Noun without index returns most recent noun' => [StoreTestCase::KEY, [[StoreTestCase::KEY, null]], StoreTestCase::$mostRecentValue], |
64
|
|
|
'Noun with index returns specific noun' => ['1st ' . StoreTestCase::KEY, [[StoreTestCase::KEY, 0]], StoreTestCase::$firstValue], |
65
|
|
|
'Non-existent noun returns null' => ['3rd ' . StoreTestCase::KEY, [[StoreTestCase::KEY, 2]], null], |
66
|
|
|
'Possessive noun w/o index string property' => [StoreTestCase::KEY . "'s color", [[StoreTestCase::KEY, null], ['color', null]], 'Blue'], |
67
|
|
|
'Possessive noun with index string property' => ['1st ' . StoreTestCase::KEY . "'s color", [[StoreTestCase::KEY, 0], ['color', null]], 'Red'], |
68
|
|
|
'Possessive noun w/o index collection w/o index' => [StoreTestCase::KEY . "'s option", [[StoreTestCase::KEY, null], ['option', null]], ['Cruise Control', 'Air Conditioning']], |
69
|
|
|
'Possessive noun with index collection w/o index' => ['1st ' . StoreTestCase::KEY . "'s option", [[StoreTestCase::KEY, 0], ['option', null]], ['GPS', 'Heated Seats']], |
70
|
|
|
'Possessive noun w/o index collection with index' => [StoreTestCase::KEY . "'s 1st option", [[StoreTestCase::KEY, null], ['option', 0]], 'Cruise Control'], |
71
|
|
|
'Possessive noun with index collection with index' => ['1st ' . StoreTestCase::KEY . "'s 1st option", [[StoreTestCase::KEY, 0], ['option', 0]], 'GPS'], |
72
|
|
|
]; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|