This repository has been archived by the owner on Feb 23, 2021. It is now read-only.
/
ShuntUnishTest.php
380 lines (317 loc) · 16 KB
/
ShuntUnishTest.php
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
<?php
/**
* @file
* Contains \Unish\ShuntUnishTest.
*/
namespace Unish;
if (class_exists('Unish\CommandUnishTestCase')) {
/**
* Unish tests for the Shunt module.
*/
class ShuntUnishTest extends CommandUnishTestCase {
/**
* The sandbox site specification.
*
* @var string
*/
protected $site = '';
/**
* All available shunt IDs.
*
* @var array
*/
protected $allShunts = ['shunt', 'shunt_example'];
/**
* {@inheritdoc}
*/
public function setUp() {
// Install a Drupal 8 sandbox using the testing profile.
$sites = $this->setUpDrupal(1, TRUE, 8, 'testing');
$this->site = $this->webroot() . '#' . key($sites);
// Symlink the Shunt module into the sandbox.
$shunt_directory = dirname(__DIR__);
symlink($shunt_directory, $this->webroot() . '/modules/shunt');
// Enable the Shunt modules.
$this->drush('pm-enable', ['shunt', 'shuntexample'], [
'skip' => NULL,
'yes' => NULL,
], $this->site);
}
/**
* Tests the shunt-reset command.
*/
public function testShuntResetCommand() {
$this->tripShunts($this->allShunts);
$this->drush('shunt-reset', [], [], $this->site);
$this->assertStringStartsWith('There were no shunts that could be reset.', $this->getErrorOutput());
$this->assertShuntIsTripped('shunt', 'No shunts reset without "ids" argument.');
$this->drush('shunt-reset', ['invalid'], [], $this->site);
$this->assertStringStartsWith('No such shunt invalid.', $this->getErrorOutput(), 'Warned about invalid "ids" argument.');
$this->drush('shunt-reset', ['shunt'], ['no' => NULL], $this->site);
$output = $this->getOutputAsList();
$this->assertEquals('The following shunts will be reset: shunt', $output[0]);
$this->assertEquals('Do you want to continue? (y/n): n', $output[1]);
$this->assertStringStartsWith('Aborting.', $this->getErrorOutput());
$this->assertShuntIsTripped('shunt', 'Shunt was not reset with "no" option.');
$this->drush('shunt-reset', ['shunt'], ['yes' => NULL], $this->site);
$this->assertStringStartsWith('Shunt shunt has been reset.', $this->getErrorOutput());
$output = $this->getOutputAsList();
$this->assertEquals('The following shunts will be reset: shunt', $output[0]);
$this->assertEquals('Do you want to continue? (y/n): y', $output[1]);
$this->assertShuntIsNotTripped('shunt', 'Shunt was reset with "yes" option.');
$this->drush('shunt-reset', ['shunt'], ['no' => NULL], $this->site);
$error_output = $this->getErrorOutputAsList();
$this->assertStringStartsWith('Shunt shunt is not tripped.', $error_output[0]);
$this->assertStringStartsWith('There were no shunts that could be reset.', $error_output[1], 'Did not try to reset already shunt that was not tripped.');
$this->tripShunts($this->allShunts);
$this->drush('shunt-reset', $this->allShunts, ['yes' => NULL], $this->site);
$this->assertStringStartsWith('The following shunts will be reset: shunt, shunt_example', $this->getOutput());
$this->assertTrue(!$this->shuntIsTripped('shunt') && !$this->shuntIsTripped('shunt_example'), 'Reset multiple, explicitly named shunts.');
$this->tripShunts($this->allShunts);
$this->drush('shunt-reset', [], ['all' => NULL, 'yes' => NULL], $this->site);
$this->assertStringStartsWith('The following shunts will be reset: shunt, shunt_example', $this->getOutput());
$error_output = $this->getErrorOutputAsList();
$this->assertStringStartsWith('Shunt shunt has been reset.', $error_output[0]);
$this->assertStringStartsWith('Shunt shunt_example has been reset.', $error_output[1]);
$this->assertTrue(!$this->shuntIsTripped('shunt') && !$this->shuntIsTripped('shunt_example'), 'Reset all shunts with "all" option.');
$this->tripShunts($this->allShunts);
$this->drush('shunt-reset', ['*'], ['no' => NULL], $this->site);
$this->assertStringStartsWith('The following shunts will be reset: shunt, shunt_example', $this->getOutput(), 'Correctly expanded bare asterisk "ids" argument.');
$this->drush('shunt-reset', ['shunt*'], ['no' => NULL], $this->site);
$this->assertStringStartsWith('The following shunts will be reset: shunt, shunt_example', $this->getOutput(), 'Correctly expanded "ids" argument with trailing slash and multiple matches.');
$this->drush('shunt-reset', ['shunt_ex*'], ['no' => NULL], $this->site);
$this->assertStringStartsWith('The following shunts will be reset: shunt_example', $this->getOutput(), 'Correctly expanded "ids" argument with trailing slash and single match.');
}
/**
* Tests the shunt-trip command.
*/
public function testShuntTripCommand() {
$this->drush('shunt-trip', [], [], $this->site);
$this->assertStringStartsWith('There were no shunts that could be tripped.', $this->getErrorOutput());
$this->assertShuntIsNotTripped('shunt', 'No shunts tripped without "ids" argument.');
$this->drush('shunt-trip', ['invalid'], [], $this->site);
$this->assertStringStartsWith('No such shunt invalid.', $this->getErrorOutput(), 'Warned about invalid "ids" argument.');
$this->drush('shunt-trip', ['shunt'], ['no' => NULL], $this->site);
$output = $this->getOutputAsList();
$this->assertEquals('The following shunts will be tripped: shunt', $output[0]);
$this->assertEquals('Do you want to continue? (y/n): n', $output[1]);
$this->assertStringStartsWith('Aborting.', $this->getErrorOutput());
$this->assertShuntIsNotTripped('shunt', 'Shunt was not tripped with "no" option.');
$this->drush('shunt-trip', ['shunt'], ['yes' => NULL], $this->site);
$this->assertStringStartsWith('Shunt shunt has been tripped.', $this->getErrorOutput());
$output = $this->getOutputAsList();
$this->assertEquals('The following shunts will be tripped: shunt', $output[0]);
$this->assertEquals('Do you want to continue? (y/n): y', $output[1]);
$this->assertShuntIsTripped('shunt', 'Shunt was tripped with "yes" option.');
$this->drush('shunt-trip', ['shunt'], ['no' => NULL], $this->site);
$error_output = $this->getErrorOutputAsList();
$this->assertStringStartsWith('Shunt shunt is already tripped.', $error_output[0]);
$this->assertStringStartsWith('There were no shunts that could be tripped.', $error_output[1], 'Did not try to trip already tripped shunt.');
$this->resetShunts($this->allShunts);
$this->drush('shunt-trip', $this->allShunts, ['yes' => NULL], $this->site);
$this->assertStringStartsWith('The following shunts will be tripped: shunt, shunt_example', $this->getOutput());
$this->assertTrue($this->shuntIsTripped('shunt') && $this->shuntIsTripped('shunt_example'), 'Tripped multiple, explicitly named shunts.');
$this->resetShunts($this->allShunts);
$this->drush('shunt-trip', [], ['all' => NULL, 'yes' => NULL], $this->site);
$this->assertStringStartsWith('The following shunts will be tripped: shunt, shunt_example', $this->getOutput());
$error_output = $this->getErrorOutputAsList();
$this->assertStringStartsWith('Shunt shunt has been tripped.', $error_output[0]);
$this->assertStringStartsWith('Shunt shunt_example has been tripped.', $error_output[1]);
$this->assertTrue($this->shuntIsTripped('shunt') && $this->shuntIsTripped('shunt_example'), 'Tripped all shunts with "all" option.');
$this->resetShunts($this->allShunts);
$this->drush('shunt-trip', ['*'], ['no' => NULL], $this->site);
$this->assertStringStartsWith('The following shunts will be tripped: shunt, shunt_example', $this->getOutput(), 'Correctly expanded bare asterisk "ids" argument.');
$this->drush('shunt-trip', ['shunt*'], ['no' => NULL], $this->site);
$this->assertStringStartsWith('The following shunts will be tripped: shunt, shunt_example', $this->getOutput(), 'Correctly expanded "ids" argument with trailing slash and multiple matches.');
$this->drush('shunt-trip', ['shunt_ex*'], ['no' => NULL], $this->site);
$this->assertStringStartsWith('The following shunts will be tripped: shunt_example', $this->getOutput(), 'Correctly expanded "ids" argument with trailing slash and single match.');
}
/**
* Tests the shunt-info command.
*/
public function testShuntInfoCommand() {
$this->tripShunts(['shunt']);
$options = ['format' => 'json'];
$this->drush('shunt-info', [], $options, $this->site);
$this->assertEquals($this->infoOutput(), $this->getOutput(), 'Returned all info without "ids" argument.');
$this->drush('shunt-info', ['invalid'], [], $this->site);
$this->assertStringStartsWith('No such shunt invalid.', $this->getErrorOutput(), 'Warned about invalid "ids" argument.');
$this->drush('shunt-info', ['shunt'], $options, $this->site);
$this->assertEquals($this->infoOutput('shunt'), $this->getOutput(), 'Returned info for explicitly named shunt.');
$this->drush('shunt-info', $this->allShunts, $options, $this->site);
$this->assertEquals($this->infoOutput(), $this->getOutput(), 'Returned info for multiple, explicitly named shunts.');
$this->drush('shunt-info', ['*'], $options, $this->site);
$this->assertEquals($this->infoOutput(), $this->getOutput(), 'Correctly expanded bare asterisk "ids" argument.');
$this->drush('shunt-info', ['shunt*'], $options, $this->site);
$this->assertEquals($this->infoOutput(), $this->getOutput(), 'Correctly expanded "ids" argument with trailing slash and multiple matches.');
$this->drush('shunt-info', ['shunt_ex*'], $options, $this->site);
$this->assertEquals($this->infoOutput('shunt_example'), $this->getOutput(), 'Correctly expanded "ids" argument with trailing slash and single match.');
}
/**
* Tests the shunt-list command.
*/
public function testShuntListCommand() {
$this->tripShunts(['shunt']);
$options = ['format' => 'json'];
$this->drush('shunt-list', [], $options, $this->site);
$this->assertEquals($this->listOutput(), $this->getOutput(), 'Returned all info without "status" option.');
$this->drush('shunt-list', [], $options + ['status' => 'tripped'], $this->site);
$this->assertEquals($this->listOutput('shunt'), $this->getOutput(), 'Filtered info to tripped shunts.');
$this->drush('shunt-list', [], $options + ['status' => 'not tripped'], $this->site);
$this->assertEquals($this->listOutput('shunt_example'), $this->getOutput(), 'Filtered info to shunts that are not tripped.');
$this->drush('shunt-list', [], $options + ['status' => 'tripped,not tripped'], $this->site);
$this->assertEquals($this->listOutput(), $this->getOutput(), 'Returned all info with both "status" option values given.');
$this->drush('shunt-list', [], $options + ['status' => 'invalid'], $this->site, NULL, self::EXIT_ERROR);
$this->assertStringStartsWith('invalid is not a valid shunt status.', $this->getErrorOutput(), 'Erred on invalid "status" option.');
$this->tripShunts(['shunt_example']);
$this->drush('shunt-list', [], $options + ['status' => 'not tripped'], $this->site);
$this->assertEquals('', $this->getOutput(), 'Returned empty when filtered to shunts that are not tripped without any available.');
$this->resetShunts($this->allShunts);
$this->drush('shunt-list', [], $options + ['status' => 'tripped'], $this->site);
$this->assertEquals('', $this->getOutput(), 'Returned empty when filtered to tripped shunts without any available.');
}
/**
* Asserts that a given shunt is tripped.
*
* @param string $id
* A shunt ID.
* @param string $message
* The assertion message.
*/
public function assertShuntIsTripped($id, $message = '') {
$this->assertTrue($this->shuntIsTripped($id), $message);
}
/**
* Asserts that a given shunt is not tripped.
*
* @param string $id
* A shunt ID.
* @param string $message
* The assertion message.
*/
public function assertShuntIsNotTripped($id, $message = '') {
$this->assertFalse($this->shuntIsTripped($id), $message);
}
/**
* Determines whether a given shunt is tripped or not.
*
* @param string $id
* The shunt ID.
*
* @return bool
* Returns TRUE if the shunt is tripped or FALSE if not.
*/
protected function shuntIsTripped($id) {
// Access state values directly to avoid using Shunt commands to test
// Shunt commands.
$this->drush('state-get', ["shunt.{$id}"], [], $this->site);
return (bool) $this->getOutput();
}
/**
* Returns the expected output of the shunt-info command, JSON-encoded.
*
* @param string|null $id
* The ID of a shunt to whose info to limit the return set.
*
* @return string
* A pretty-printed, JSON-encoded array of expected command output.
*/
protected function infoOutput($id = NULL) {
$info = [
'shunt' => [
'id' => 'shunt',
'label' => 'Shunt',
'description' => 'Default shunt. No built-in behavior.',
'status' => 'Tripped',
],
'shunt_example' => [
'id' => 'shunt_example',
'label' => 'Shunt example',
'description' => 'Display a fail whale at /shuntexample.',
'status' => 'Not tripped',
],
];
return $this->filterAndEncode($info, $id);
}
/**
* Returns the expected output of the shunt-list command, JSON-encoded.
*
* @param string|null $id
* The ID of a shunt to whose info to limit the return set.
*
* @return string
* A pretty-printed, JSON-encoded array of expected command output.
*/
protected function listOutput($id = NULL) {
$info = [
'shunt' => [
'name' => 'Shunt (shunt)',
'description' => 'Default shunt. No built-in behavior.',
'status' => 'Tripped',
],
'shunt_example' => [
'name' => 'Shunt example (shunt_example)',
'description' => 'Display a fail whale at /shuntexample.',
'status' => 'Not tripped',
],
];
return $this->filterAndEncode($info, $id);
}
/**
* Optionally filters an array of data and JSON encodes it.
*
* @param array $data
* An associative array of data.
* @param string|null $key
* The key of an element to limit the return set to.
*
* @return string
* A pretty-printed, JSON-encoded array of shunt info.
*/
protected function filterAndEncode(array $data, $key) {
// If a shunt ID is provided, return only its subset of data, maintaining
// the same data structure depth.
if ($key) {
return json_encode([$key => $data[$key]], JSON_PRETTY_PRINT);
}
// Otherwise return all data.
else {
return json_encode($data, JSON_PRETTY_PRINT);
}
}
/**
* Trips a given list of shunts.
*
* @param array $ids
* An indexed array of shunt IDs.
*/
protected function tripShunts(array $ids) {
$statuses = array_fill_keys($ids, TRUE);
$this->setShuntStatuses($statuses);
}
/**
* Resets a given list of shunts.
*
* @param array $ids
* An indexed array of shunt IDs.
*/
protected function resetShunts(array $ids) {
$statuses = array_fill_keys($ids, FALSE);
$this->setShuntStatuses($statuses);
}
/**
* Sets the status of a given list of shunts.
*
* @param array $statuses
* An associative array of shunt statuses where each key is a shunt ID and
* its value is the status to set the shunt to.
*/
protected function setShuntStatuses(array $statuses) {
foreach ($statuses as $id => $status) {
// Set state values directly to avoid using Shunt commands to test Shunt
// commands.
$this->drush('state-set', ["shunt.{$id}", $status ? 'true' : 0], [], $this->site);
}
}
}
}