-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathDatabaseCheckTest.php
More file actions
334 lines (276 loc) · 11.2 KB
/
DatabaseCheckTest.php
File metadata and controls
334 lines (276 loc) · 11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
<?php
namespace PhpSchool\PhpWorkshopTest\Check;
use DI\ContainerBuilder;
use PDO;
use PhpSchool\PhpWorkshop\Check\CheckRepository;
use PhpSchool\PhpWorkshop\Check\DatabaseCheck;
use PhpSchool\PhpWorkshop\Event\EventDispatcher;
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
use PhpSchool\PhpWorkshop\Exercise\ExerciseType;
use PhpSchool\PhpWorkshop\ExerciseCheck\DatabaseExerciseCheck;
use PhpSchool\PhpWorkshop\ExerciseDispatcher;
use PhpSchool\PhpWorkshop\ExerciseRunner\CliRunner;
use PhpSchool\PhpWorkshop\ExerciseRunner\RunnerManager;
use PhpSchool\PhpWorkshop\Input\Input;
use PhpSchool\PhpWorkshop\Output\OutputInterface;
use PhpSchool\PhpWorkshop\ResultAggregator;
use PhpSchool\PhpWorkshop\Solution\SingleFileSolution;
use PhpSchool\PhpWorkshopTest\Asset\DatabaseExerciseInterface;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use ReflectionProperty;
use RuntimeException;
class DatabaseCheckTest extends TestCase
{
/**
* @var DatabaseCheck
*/
private $check;
/**
* @var CheckRepository
*/
private $checkRepository;
/**
* @var ExerciseInterface
*/
private $exercise;
/**
* @var string
*/
private $dbDir;
public function setUp(): void
{
$containerBuilder = new ContainerBuilder();
$containerBuilder->addDefinitions(__DIR__ . '/../../app/config.php');
$container = $containerBuilder->build();
$this->checkRepository = $container->get(CheckRepository::class);
$this->check = new DatabaseCheck();
$this->exercise = $this->createMock(DatabaseExerciseInterface::class);
$this->exercise->method('getType')->willReturn(ExerciseType::CLI());
$this->dbDir = sprintf(
'%s/php-school/PhpSchool_PhpWorkshop_Check_DatabaseCheck',
str_replace('\\', '/', realpath(sys_get_temp_dir()))
);
$this->assertEquals('Database Verification Check', $this->check->getName());
$this->assertEquals(DatabaseExerciseCheck::class, $this->check->getExerciseInterface());
}
private function getRunnerManager(ExerciseInterface $exercise, EventDispatcher $eventDispatcher): MockObject
{
$runner = $this->getMockBuilder(CliRunner::class)
->setConstructorArgs([$exercise, $eventDispatcher])
->setMethods(['configure', 'getRequiredChecks'])
->getMock();
$runner
->method('getRequiredChecks')
->willReturn([]);
$runnerManager = $this->createMock(RunnerManager::class);
$runnerManager
->expects($this->once())
->method('getRunner')
->willReturn($runner);
return $runnerManager;
}
public function testIfDatabaseFolderExistsExceptionIsThrown(): void
{
$eventDispatcher = new EventDispatcher(new ResultAggregator());
mkdir($this->dbDir, 0777, true);
try {
$this->check->attach($eventDispatcher);
$this->fail('Exception was not thrown');
} catch (RuntimeException $e) {
$this->assertEquals(sprintf('Database directory: "%s" already exists', $this->dbDir), $e->getMessage());
rmdir($this->dbDir);
}
}
/**
* If an exception is thrown from PDO, check that the check can be run straight away
* Previously files were not cleaned up that caused exceptions.
*/
public function testIfPDOThrowsExceptionItCleansUp(): void
{
$eventDispatcher = new EventDispatcher(new ResultAggregator());
$refProp = new ReflectionProperty(DatabaseCheck::class, 'userDsn');
$refProp->setAccessible(true);
$refProp->setValue($this->check, 'notvaliddsn');
try {
$this->check->attach($eventDispatcher);
$this->fail('Exception was not thrown');
} catch (\PDOException $e) {
}
//try to run the check as usual
$this->check = new DatabaseCheck();
$solution = SingleFileSolution::fromFile(realpath(__DIR__ . '/../res/database/solution.php'));
$this->exercise
->expects($this->once())
->method('getSolution')
->willReturn($solution);
$this->exercise
->expects($this->once())
->method('getArgs')
->willReturn([1, 2, 3]);
$this->exercise
->expects($this->once())
->method('getRequiredChecks')
->willReturn([DatabaseCheck::class]);
$this->exercise
->expects($this->once())
->method('verify')
->with($this->isInstanceOf(PDO::class))
->willReturn(true);
$this->checkRepository->registerCheck($this->check);
$results = new ResultAggregator();
$eventDispatcher = new EventDispatcher($results);
$dispatcher = new ExerciseDispatcher(
$this->getRunnerManager($this->exercise, $eventDispatcher),
$results,
$eventDispatcher,
$this->checkRepository
);
$dispatcher->verify($this->exercise, new Input('app', ['program' => __DIR__ . '/../res/database/user.php']));
$this->assertTrue($results->isSuccessful());
}
public function testSuccessIsReturnedIfDatabaseVerificationPassed(): void
{
$solution = SingleFileSolution::fromFile(realpath(__DIR__ . '/../res/database/solution.php'));
$this->exercise
->expects($this->once())
->method('getSolution')
->willReturn($solution);
$this->exercise
->expects($this->once())
->method('getArgs')
->willReturn([[1, 2, 3]]);
$this->exercise
->expects($this->once())
->method('getRequiredChecks')
->willReturn([DatabaseCheck::class]);
$this->exercise
->expects($this->once())
->method('verify')
->with($this->isInstanceOf(PDO::class))
->willReturn(true);
$this->checkRepository->registerCheck($this->check);
$results = new ResultAggregator();
$eventDispatcher = new EventDispatcher($results);
$dispatcher = new ExerciseDispatcher(
$this->getRunnerManager($this->exercise, $eventDispatcher),
$results,
$eventDispatcher,
$this->checkRepository
);
$dispatcher->verify($this->exercise, new Input('app', ['program' => __DIR__ . '/../res/database/user.php']));
$this->assertTrue($results->isSuccessful());
}
public function testRunExercise(): void
{
$this->exercise
->expects($this->once())
->method('getArgs')
->willReturn([]);
$this->checkRepository->registerCheck($this->check);
$results = new ResultAggregator();
$eventDispatcher = new EventDispatcher($results);
$dispatcher = new ExerciseDispatcher(
$this->getRunnerManager($this->exercise, $eventDispatcher),
$results,
$eventDispatcher,
$this->checkRepository
);
$dispatcher->run(
$this->exercise,
new Input('app', ['program' => __DIR__ . '/../res/database/user-solution-alter-db.php']),
$this->createMock(OutputInterface::class)
);
}
public function testFailureIsReturnedIfDatabaseVerificationFails(): void
{
$solution = SingleFileSolution::fromFile(realpath(__DIR__ . '/../res/database/solution.php'));
$this->exercise
->expects($this->once())
->method('getSolution')
->willReturn($solution);
$this->exercise
->expects($this->once())
->method('getArgs')
->willReturn([1, 2, 3]);
$this->exercise
->expects($this->once())
->method('getRequiredChecks')
->willReturn([DatabaseCheck::class]);
$this->exercise
->expects($this->once())
->method('verify')
->with($this->isInstanceOf(PDO::class))
->willReturn(false);
$this->checkRepository->registerCheck($this->check);
$results = new ResultAggregator();
$eventDispatcher = new EventDispatcher($results);
$dispatcher = new ExerciseDispatcher(
$this->getRunnerManager($this->exercise, $eventDispatcher),
$results,
$eventDispatcher,
$this->checkRepository
);
$dispatcher->verify($this->exercise, new Input('app', ['program' => __DIR__ . '/../res/database/user.php']));
$this->assertFalse($results->isSuccessful());
$results = iterator_to_array($results);
$this->assertSame('Database verification failed', $results[1]->getReason());
}
public function testAlteringDatabaseInSolutionDoesNotEffectDatabaseInUserSolution(): void
{
$solution = SingleFileSolution::fromFile(realpath(__DIR__ . '/../res/database/solution-alter-db.php'));
$this->exercise
->expects($this->once())
->method('getSolution')
->willReturn($solution);
$this->exercise
->method('getArgs')
->willReturn([]);
$this->exercise
->method('verify')
->willReturn(true);
$this->exercise
->expects($this->once())
->method('getRequiredChecks')
->willReturn([DatabaseCheck::class]);
$this->exercise
->expects($this->once())
->method('seed')
->with($this->isInstanceOf(PDO::class))
->willReturnCallback(function (PDO $db) {
$db->exec(
'CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT, age INTEGER, gender TEXT)'
);
$stmt = $db->prepare('INSERT into users (name, age, gender) VALUES (:name, :age, :gender)');
$stmt->execute([':name' => 'Jimi Hendrix', ':age' => 27, ':gender' => 'Male']);
});
$this->exercise
->expects($this->once())
->method('verify')
->with($this->isInstanceOf(PDO::class))
->willReturnCallback(function (PDO $db) {
$users = $db->query('SELECT * FROM users');
$users = $users->fetchAll(PDO::FETCH_ASSOC);
$this->assertEquals(
[
['id' => 1, 'name' => 'Jimi Hendrix', 'age' => '27', 'gender' => 'Male'],
['id' => 2, 'name' => 'Kurt Cobain', 'age' => '27', 'gender' => 'Male'],
],
$users
);
});
$this->checkRepository->registerCheck($this->check);
$results = new ResultAggregator();
$eventDispatcher = new EventDispatcher($results);
$dispatcher = new ExerciseDispatcher(
$this->getRunnerManager($this->exercise, $eventDispatcher),
$results,
$eventDispatcher,
$this->checkRepository
);
$dispatcher->verify(
$this->exercise,
new Input('app', ['program' => __DIR__ . '/../res/database/user-solution-alter-db.php'])
);
}
}