1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Blackmine\Tests\Repository; |
4
|
|
|
|
5
|
|
|
use Blackmine\Client\Client; |
6
|
|
|
use Blackmine\Client\ClientInterface; |
7
|
|
|
use Blackmine\Client\ClientOptions; |
8
|
|
|
use Blackmine\Client\Response\ApiResponse; |
9
|
|
|
use Blackmine\Model\AbstractModel; |
10
|
|
|
use Codeception\Test\Unit; |
11
|
|
|
use Exception; |
12
|
|
|
|
13
|
|
|
abstract class AbstractRepositoryTest extends Unit |
14
|
|
|
{ |
15
|
|
|
protected const REPOSITORY_CLASS = "undefined"; |
16
|
|
|
|
17
|
|
|
protected array $test_data; |
18
|
|
|
|
19
|
|
|
// phpcs:ignore |
20
|
|
|
protected function _setUp(): void |
21
|
|
|
{ |
22
|
|
|
$this->initTestData(); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @throws Exception |
28
|
|
|
*/ |
29
|
|
|
public function testGet(): void |
30
|
|
|
{ |
31
|
|
|
$test_data = $this->test_data["__methods"]["get"]["__success"] ?? null; |
32
|
|
|
|
33
|
|
|
if ($test_data) { |
34
|
|
|
foreach ($test_data as $id => $test_datum) { |
35
|
|
|
$repository_class = static::REPOSITORY_CLASS; |
36
|
|
|
$repository = new $repository_class($this->getClientResponding( |
37
|
|
|
"get", |
38
|
|
|
$this->getSuccessResponse($test_datum["__input"]) |
39
|
|
|
)); |
40
|
|
|
$model = $repository->get($id); |
41
|
|
|
|
42
|
|
|
$this->assertInstanceOf(get_class($test_datum["__output"]), $model); |
43
|
|
|
$this->assertEquals($model, $test_datum["__output"]); |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function testGetThrows(): void |
49
|
|
|
{ |
50
|
|
|
$this->assertThrows("get"); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function testAll(): void |
54
|
|
|
{ |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function testAllThrows(): void |
58
|
|
|
{ |
59
|
|
|
$this->assertThrows("all"); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function testSearch(): void |
63
|
|
|
{ |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function testSearchThrows(): void |
67
|
|
|
{ |
68
|
|
|
$this->assertThrows("search"); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function testCreate(): void |
72
|
|
|
{ |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function testCreateThrows(): void |
76
|
|
|
{ |
77
|
|
|
$this->assertThrows("create"); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function testUpdate(): void |
81
|
|
|
{ |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function testUpdateThrows(): void |
85
|
|
|
{ |
86
|
|
|
$this->assertThrows("update"); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function testDelete(): void |
90
|
|
|
{ |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function testDeleteThrows(): void |
94
|
|
|
{ |
95
|
|
|
$this->assertThrows("delete"); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @throws Exception |
100
|
|
|
*/ |
101
|
|
|
protected function getClientResponding(string $method, ApiResponse $response): ClientInterface |
102
|
|
|
{ |
103
|
|
|
$methods_map = [ |
104
|
|
|
"get" => "get", |
105
|
|
|
"all" => "get", |
106
|
|
|
"search" => "get", |
107
|
|
|
"create" => "post", |
108
|
|
|
"update" => "put", |
109
|
|
|
"delete" => "delete" |
110
|
|
|
]; |
111
|
|
|
|
112
|
|
|
$method = $methods_map[$method]; |
113
|
|
|
|
114
|
|
|
return $this->construct( |
115
|
|
|
Client::class, |
116
|
|
|
[ |
117
|
|
|
"options" => new ClientOptions([ |
118
|
|
|
ClientOptions::CLIENT_OPTION_BASE_URL => "", |
119
|
|
|
ClientOptions::CLIENT_OPTION_API_KEY => "" |
120
|
|
|
]) |
121
|
|
|
], |
122
|
|
|
[ |
123
|
|
|
$method => function () use ($response) { |
124
|
|
|
return $response; |
125
|
|
|
} |
126
|
|
|
] |
127
|
|
|
); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
protected function assertThrows(string $method): void |
131
|
|
|
{ |
132
|
|
|
$test_data = $this->test_data["__methods"][$method]["__error"] ?? null; |
133
|
|
|
if (is_string($test_data)) { |
134
|
|
|
$this->assertMethodThrowsException($method, 500, $test_data); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
if (is_array($test_data)) { |
138
|
|
|
foreach ($test_data as $error_code => $expected_exception) { |
139
|
|
|
$this->assertMethodThrowsException($method, $error_code, $expected_exception); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
protected function assertMethodThrowsException( |
145
|
|
|
string $method, |
146
|
|
|
int $status_code, |
147
|
|
|
string $expected_exception |
148
|
|
|
): void { |
149
|
|
|
$repository_class = static::REPOSITORY_CLASS; |
150
|
|
|
$repository = new $repository_class($this->getClientResponding( |
151
|
|
|
$method, |
152
|
|
|
$this->getErrorRespponse($status_code, []) |
153
|
|
|
)); |
154
|
|
|
|
155
|
|
|
$this->expectException($expected_exception); |
156
|
|
|
|
157
|
|
|
if (in_array($method, ["update", "create", "delete"])) { |
158
|
|
|
$model_class = $repository->getModelClass(); |
159
|
|
|
$model = new $model_class(); |
160
|
|
|
$repository->$method($model); |
161
|
|
|
} else { |
162
|
|
|
$repository->$method(); |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
|
167
|
|
|
protected function initTestData(): void |
168
|
|
|
{ |
169
|
|
|
$data_dir = __DIR__ . "/../data/Repository/"; |
170
|
|
|
$model_data_file = $data_dir . $this->getUnqualifiedClassName() . "Data.php"; |
171
|
|
|
|
172
|
|
|
if (file_exists($model_data_file)) { |
173
|
|
|
$data = (require $model_data_file); |
174
|
|
|
$this->test_data = $data; |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
protected function getUnqualifiedClassName(): string |
179
|
|
|
{ |
180
|
|
|
$path = explode('\\', get_class($this)); |
181
|
|
|
return array_pop($path); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
protected function getSuccessResponse(array $data = []): ApiResponse |
185
|
|
|
{ |
186
|
|
|
return new ApiResponse(200, $data); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
protected function getErrorRespponse(int $error_code, array $data): ApiResponse |
190
|
|
|
{ |
191
|
|
|
return new ApiResponse($error_code, $data); |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|