1
|
|
|
<?php namespace Integration\Chekote\NounStore\Store; |
|
|
|
|
2
|
|
|
|
3
|
|
|
use Chekote\NounStore\Store; |
4
|
|
|
use Integration\Chekote\NounStore\TestCase; |
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
|
7
|
|
|
class GetTest extends TestCase |
8
|
|
|
{ |
9
|
|
|
/** @var Store */ |
10
|
|
|
protected static $store; |
11
|
|
|
|
12
|
|
|
/** @var string */ |
13
|
|
|
const NOUN = 'Person'; |
14
|
|
|
|
15
|
|
|
/** @var Car */ |
16
|
|
|
protected static $chevy; |
17
|
|
|
|
18
|
|
|
/** @var Car */ |
19
|
|
|
protected static $ford; |
20
|
|
|
|
21
|
|
|
/** @var Car */ |
22
|
|
|
protected static $kia; |
23
|
|
|
|
24
|
|
|
/** @var Car */ |
25
|
|
|
protected static $toyota; |
26
|
|
|
|
27
|
|
|
/** @var Person */ |
28
|
|
|
protected static $alice; |
29
|
|
|
|
30
|
|
|
/** @var Person */ |
31
|
|
|
protected static $bob; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Sets up the classes initial static state. |
35
|
|
|
*/ |
36
|
|
|
public static function initialize(): void |
37
|
|
|
{ |
38
|
|
|
self::$store = new Store(); |
39
|
|
|
|
40
|
|
|
self::$kia = new Car(); |
41
|
|
|
self::$kia->make = 'Kia'; |
42
|
|
|
|
43
|
|
|
self::$toyota = new Car(); |
44
|
|
|
self::$toyota->make = 'Toyota'; |
45
|
|
|
|
46
|
|
|
self::$bob = new Person(); |
47
|
|
|
self::$bob->name = 'Bob'; |
48
|
|
|
self::$bob->car = [self::$kia, self::$toyota]; |
49
|
|
|
|
50
|
|
|
self::$ford = new Car(); |
51
|
|
|
self::$ford->make = 'Ford'; |
52
|
|
|
|
53
|
|
|
self::$chevy = new Car(); |
54
|
|
|
self::$chevy->make = 'Chevrolet'; |
55
|
|
|
|
56
|
|
|
self::$alice = new Person(); |
57
|
|
|
self::$alice->name = 'Alice'; |
58
|
|
|
self::$alice->car = [self::$ford, self::$chevy]; |
59
|
|
|
|
60
|
|
|
self::$store->set(self::NOUN, self::$bob); |
61
|
|
|
self::$store->set(self::NOUN, self::$alice); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @dataProvider happyPathDataProvider |
66
|
|
|
* @param string $key the key to fetch the value for. |
67
|
|
|
* @param mixed $expectedValue the expected value for the key. |
68
|
|
|
* @param string $message the message to pass to the assertion, for reporting if the test fails. |
69
|
|
|
*/ |
70
|
|
|
public function testHappyPath(string $key, mixed $expectedValue, string $message): void |
71
|
|
|
{ |
72
|
|
|
$this->assertSame($expectedValue, self::$store->get($key), $message); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public static function happyPathDataProvider(): array |
76
|
|
|
{ |
77
|
|
|
return [ |
78
|
|
|
'Noun' => ['Person', self::$alice, 'Alice is the most recent Person in the store'], |
79
|
|
|
'Nth Noun' => ['1st Person', self::$bob, 'Bob is the 1st Person in the store'], |
80
|
|
|
'Related Noun' => ["Person's car", [self::$ford, self::$chevy], 'Alice is the most recent Person in the store, and cars are Ford and Chevrolet'], |
81
|
|
|
'Related Nth Noun' => ["Person's 1st car", self::$ford, 'Alice is the most recent Person in the store, and her 1st car is the Ford'], |
82
|
|
|
'Nth Nouns Related Noun' => ["1st Person's car", [self::$kia, self::$toyota], 'Bob is the 1st Person in the store, and his cars are Kia and Toyota'], |
83
|
|
|
'Nth Nouns Related Nth Noun' => ["1st Person's 1st car", self::$kia, 'Bob is the 1st Person in the store, and his 1st car is the Kia'], |
84
|
|
|
'Missing Noun' => ['Dog', null, 'Dog does not exist in the store'], |
85
|
|
|
'Missing Nth Noun' => ['3rd Person', null, '3rd Person does not exist in the store'], |
86
|
|
|
'Missing Related Noun' => ["Person's dog", null, 'Alice is the most recent Person in the store, but has no dog'], |
87
|
|
|
]; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function testInvalidKeyThrowsInvalidArgumentException(): void |
91
|
|
|
{ |
92
|
|
|
$invalidKey = "Customer's's Car"; |
93
|
|
|
|
94
|
|
|
$this->expectException(InvalidArgumentException::class); |
95
|
|
|
$this->expectExceptionMessage('Key syntax is invalid'); |
96
|
|
|
|
97
|
|
|
self::$store->get($invalidKey); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
class Car |
|
|
|
|
102
|
|
|
{ |
103
|
|
|
/** @var string */ |
104
|
|
|
public $make; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
class Person |
|
|
|
|
108
|
|
|
{ |
109
|
|
|
/** @var Car[] */ |
110
|
|
|
public $car; |
111
|
|
|
|
112
|
|
|
/** @var string */ |
113
|
|
|
public $name; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
GetTest::initialize(); |
117
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.