-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathPHPUnitTest.php
More file actions
435 lines (388 loc) · 13.7 KB
/
PHPUnitTest.php
File metadata and controls
435 lines (388 loc) · 13.7 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
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
<?php
namespace DrupalCodeBuilder\Generator;
use CaseConverter\CaseString;
use DrupalCodeBuilder\Definition\PropertyDefinition;
use DrupalCodeBuilder\Generator\Render\Docblock;
use MutableTypedData\Data\DataItem;
/**
* Component generator: PHPUnit test class.
*/
class PHPUnitTest extends PHPClassFile {
/**
* {@inheritdoc}
*/
protected $functionOrdering = [
// We only generate protected and public methods. The protected setUp()
// should go before the public test method.
'protected',
'public',
];
/**
* {@inheritdoc}
*/
public static function getPropertyDefinition(): PropertyDefinition {
// Presets for the different types of test.
$test_type_presets = [
'unit' => [
// Option label.
'label' => 'Unit test',
'data' => [
// Values that are forced on other properties.
// These are set in the process stage.
'force' => [
// Name of another property => Value for that property.
'relative_namespace' => [
'value' => 'Unit',
],
'parent_class_name' => [
'value' => '\Drupal\Tests\UnitTestCase',
],
'use_module_dependencies' => [
'value' => FALSE,
],
],
],
],
'kernel' => [
'label' => 'Kernel test',
'data' => [
'force' => [
'relative_namespace' => [
'value' => 'Kernel',
],
'parent_class_name' => [
'value' => '\Drupal\KernelTests\KernelTestBase',
],
'use_module_dependencies' => [
'value' => TRUE,
],
],
],
],
'kernel_entity' => [
'label' => 'Kernel test with entity support',
'data' => [
'force' => [
'relative_namespace' => [
'value' => 'Kernel',
],
'parent_class_name' => [
'value' => '\Drupal\KernelTests\Core\Entity\EntityKernelTestBase',
],
'use_module_dependencies' => [
'value' => TRUE,
],
],
],
],
'browser' => [
'label' => 'Browser test',
'data' => [
'force' => [
'relative_namespace' => [
'value' => 'Functional',
],
'parent_class_name' => [
'value' => '\Drupal\Tests\BrowserTestBase',
],
'use_module_dependencies' => [
'value' => TRUE,
],
],
],
],
'javascript' => [
'label' => 'Javascript test',
'data' => [
'force' => [
'relative_namespace' => [
'value' => 'FunctionalJavascript',
],
'parent_class_name' => [
'value' => '\Drupal\FunctionalJavascriptTests\WebDriverTestBase',
],
'use_module_dependencies' => [
'value' => TRUE,
],
],
],
],
'existing_site' => [
'label' => 'Existing site test',
'description' => 'Requires the weitzman/drupal-test-traits package.',
'data' => [
'force' => [
'relative_namespace' => [
'value' => 'ExistingSite',
],
'parent_class_name' => [
'value' => '\weitzman\DrupalTestTraits\ExistingSiteBase',
],
'use_module_dependencies' => [
'value' => FALSE,
],
],
],
],
];
$properties = [
'test_type' => PropertyDefinition::create('string')
->setLabel('Test type')
->setPresets($test_type_presets)
->setRequired(TRUE),
'plain_class_name' => PropertyDefinition::create('string')
->setLabel('Test class name')
->setDescription("The short class name of the test.")
->setRequired(TRUE),
// TODO: class name validation
// if (!preg_match('@^\w+$@', $component_data[$property_name])) {
// TODO: also check camel case.
// return 'Invalid class name.';
// }
// TODO: must also end in 'Test'.
'container_services' => PropertyDefinition::create('string')
->setLabel('Services from the container')
->setDescription("The services that this test class gets from the container, to use normally.")
->setMultiple(TRUE)
->setOptionsProvider(\DrupalCodeBuilder\Factory::getTask('ReportServiceData')),
'mocked_services' => PropertyDefinition::create('string')
->setLabel('Services to mock')
->setDescription("The services that this test class creates mocks for.")
->setMultiple(TRUE)
->setOptionsProvider(\DrupalCodeBuilder\Factory::getTask('ReportServiceData')),
'creation_traits' => PropertyDefinition::create('string')
->setLabel('Creation traits')
->setDescription("Traits which provide useful methods for creating various kinds of test data.")
->setMultiple(TRUE)
->setOptionsProvider(\DrupalCodeBuilder\Factory::getTask('Analyse\TestTraits')),
'module_dependencies' => PropertyDefinition::create('string')
->setMultiple(TRUE)
->setAutoAcquiredFromRequester(),
'use_module_dependencies' => PropertyDefinition::create('boolean')
->setInternal(TRUE),
'test_modules' => static::getLazyDataDefinitionForGeneratorType('TestModule')
->setLabel('Test modules')
->setMultiple(TRUE),
];
// Put the parent definitions after ours.
$definition = parent::getPropertyDefinition();
$properties += $definition->getProperties();
$definition->setProperties($properties);
// Override some parent definitions to provide computed defaults.
// Qualified class names and paths work differently for test classes because
// the namespace above the module is different and the path is different.
// Treat this as relative to the \Drupal\Tests\mymodule namespace.
// $data_definition['relative_namespace']->getDefault()
// ->setCallable(function (DataItem $component_data) {
// $test_data = $component_data->getParent();
// return $test_data->test_namespace->value;
// });
// $data_definition['relative_class_name']['default'] = function ($component_data) {
// if (isset($component_data['test_namespace'])) {
// return [
// $component_data['test_namespace'],
// $component_data['test_class_name'],
// ];
// }
// else {
// return [
// $component_data['test_class_name'],
// ];
// }
// };
$definition->getProperty('relative_class_name')->setInternal(TRUE);
$definition->getProperty('qualified_class_name_pieces')->getDefault()
->setCallable(function (DataItem $component_data) {
$class_name_pieces = array_merge(
[
'Drupal',
'Tests',
'%module',
],
$component_data->getParent()->relative_class_name_pieces->get()
);
return $class_name_pieces;
});
$definition->getProperty('path')->getDefault()
->setCallable(function (DataItem $component_data) {
// Lop off the initial Drupal\Tests\module and the final class name to
// build the path.
$path_pieces = array_slice($component_data->getParent()->qualified_class_name_pieces->value, 3, -1);
// Add the initial tests/src to the front.
array_unshift($path_pieces, 'tests/src');
return implode('/', $path_pieces);
});
$definition->getProperty('docblock_first_line')->setLiteralDefault("Test case class TODO.");
return $definition;
}
/**
* {@inheritdoc}
*/
public function requiredComponents(): array {
$components = parent::requiredComponents();
$components['method_setup'] = [
'component_type' => 'PHPFunction',
'containing_component' => '%requester',
'function_name' => 'setUp',
'docblock_inherit' => TRUE,
'prefixes' => ['protected'],
'return_type' => 'void',
// 'body' is set later, in classCodeBody().
];
$components['method_test'] = [
'component_type' => 'PHPFunction',
'containing_component' => '%requester',
'function_name' => 'testMyTest',
'function_docblock_lines' => [
'Tests the TODO.',
],
'prefixes' => ['public'],
'body' => [
'// TODO: test code here.',
],
];
foreach ($this->component_data['container_services'] as $service_id) {
$components['service_' . $service_id] = [
'component_type' => 'TestContainerService',
'containing_component' => '%requester',
'service_id' => $service_id,
'class_has_constructor' => FALSE,
'class_has_static_factory' => FALSE,
];
}
foreach ($this->component_data['mocked_services'] as $service_id) {
$components['service_' . $service_id] = [
'component_type' => 'TestMockedService',
'containing_component' => '%requester',
'service_id' => $service_id,
'class_has_constructor' => FALSE,
'class_has_static_factory' => FALSE,
];
}
return $components;
}
/**
* {@inheritdoc}
*/
protected function getClassDocBlock(): DocBlock {
$docblock = parent::getClassDocBlock();
$docblock->group('%module');
return $docblock;
}
/**
* {@inheritdoc}
*/
protected function classCodeBody() {
// Quick temporary hack to set the method lines for working with services
// into the setUp() method.
// TODO: change the services to be components that are contained in the
// method_setup function component.
foreach ($this->containedComponents['function'] as $key => $child_item) {
$parts = explode('/', $key);
$local_name = end($parts);
if ($local_name == 'method_setup') {
$setup_method_component = $child_item;
break;
}
}
$setup_method_component->component_data->body = $this->getSetupMethodLines();
return parent::classCodeBody();
}
/**
* {@inheritdoc}
*/
protected function collectSectionBlocks() {
// Set up properties and methods.
if ($this->component_data->use_module_dependencies->value) {
// Get the dependencies of the generated module. These need to be cleaned
// up to remove any 'project:' prefixes.
$module_dependencies = $this->component_data->module_dependencies->values();
array_walk($module_dependencies, function(&$dependency) {
if ($position = strpos($dependency, ':')) {
$dependency = substr($dependency, $position + 1);
}
});
// Create the array of modules to install in the test.
$test_install_modules = array_merge(
// Some general defaults.
[
'system',
'user',
],
$module_dependencies,
// The generated module itself.
[
'%module',
]
);
// Any test modules.
if (!empty($this->component_data['test_modules'])) {
foreach ($this->component_data['test_modules'] as $data) {
$test_install_modules[] = $data['root_name'];
}
}
// Class properties.
// The modules property should come first, as it's the most interesting to
// a developer; others are boilerplate.
$this->properties[] = $this->createPropertyBlock(
'modules',
'array',
[
'docblock_first_line' => 'The modules to enable.',
'prefixes' => ['protected', 'static'],
'default' => $test_install_modules,
'break_array_value' => TRUE,
]
);
}
// Add properties for services obtained from the container.
if (!empty($this->getContentsElement('service_property_container'))) {
// Service class property.
foreach ($this->getContentsElement('service_property_container') as $service_property) {
$docblock = DocBlock::property();
$docblock[] = $service_property['description'] . '.';
$docblock->var($service_property['typehint']);
$property_code = $docblock->render();
$property_code[] = 'protected $' . $service_property['property_name'] . ';';
$this->properties[] = $property_code;
}
}
foreach ($this->component_data['creation_traits'] as $data) {
$this->traits[] = [
"use {$data};",
];
}
parent::collectSectionBlocks();
}
/**
* Gets the code lines for the setUp() method.
*
* @return array
* Array of code lines.
*/
protected function getSetupMethodLines(): array {
$setup_lines = [];
$setup_lines[] = 'parent::setUp();';
$setup_lines[] = '';
// $this->containedComponents->dump();
// Container services setup.
if (!empty($this->getContentsElement('service_container'))) {
// Use the main service infor rather than 'container_extraction', as
// that is intended for use in an array and so has a terminal comma.
// TODO: remove the terminal comma so we can use it here!
foreach ($this->getContentsElement('service_container') as $service_info) {
$setup_lines[] = "£this->{$service_info['property_name']} = £this->container->get('{$service_info['id']}');";
}
$setup_lines[] = '';
}
// Mocked services.
if (!empty($this->getContentsElement('service_mocked'))) {
foreach ($this->getContentsElement('service_mocked') as $service_info) {
$setup_lines[] = "// Mock the {$service_info['label']}.";
$setup_lines[] = "£{$service_info['variable_name']} = £this->prophesize({$service_info['typehint']}::class);";
$setup_lines[] = "£this->container->set('{$service_info['id']}', £{$service_info['variable_name']}->reveal());";
}
}
return $setup_lines ;
}
}