-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathPHPUnitTest.php
More file actions
240 lines (216 loc) · 6.6 KB
/
PHPUnitTest.php
File metadata and controls
240 lines (216 loc) · 6.6 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
<?php
namespace DrupalCodeBuilder\Generator;
use CaseConverter\CaseString;
/**
* Component generator: PHPUnit test class.
*/
class PHPUnitTest extends PHPClassFile {
/**
* {@inheritdoc}
*/
public static function componentDataDefinition() {
// 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.
'test_namespace' => [
'value' => 'Unit',
],
'parent_class_name' => [
'value' => '\Drupal\Tests\UnitTestCase',
],
],
],
],
'kernel' => [
'label' => 'Kernel test',
'data' => [
'force' => [
'test_namespace' => [
'value' => 'Kernel',
],
'parent_class_name' => [
'value' => '\Drupal\KernelTests\KernelTestBase',
],
],
],
],
'kernel_entity' => [
'label' => 'Kernel test with entity support',
'data' => [
'force' => [
'test_namespace' => [
'value' => 'Kernel',
],
'parent_class_name' => [
'value' => '\Drupal\KernelTests\Core\Entity\EntityKernelTestBase',
],
],
],
],
'browser' => [
'label' => 'Browser test',
'data' => [
'force' => [
'test_namespace' => [
'value' => 'Functional',
],
'parent_class_name' => [
'value' => '\Drupal\Tests\BrowserTestBase',
],
],
],
],
'javascript' => [
'label' => 'Javascript test',
'data' => [
'force' => [
'test_namespace' => [
'value' => 'FunctionalJavascript',
],
'parent_class_name' => [
'value' => '\Drupal\FunctionalJavascriptTests\JavascriptTestBase',
],
],
],
],
];
$data_definition = [
'test_type' => [
'label' => 'Test type',
'presets' => $test_type_presets,
],
'test_namespace' => [
'label' => "The namespace piece above the class name, e.g. 'Kernel'",
'internal' => TRUE,
],
'test_class_name' => [
'label' => 'Test class name',
'description' => "The short class name of the test.",
'process_default' => TRUE,
],
'module_dependencies' => [
'acquired' => TRUE,
],
'test_modules' => [
'label' => 'Test modules',
'format' => 'compound',
'component_type' => 'TestModule',
'default' => function($component_data) {
return [
0 => [
// Create a default module name from the requesting test class name.
'root_name' => CaseString::pascal($component_data['test_class_name'])->snake(),
],
];
},
],
];
// Put the parent definitions after ours.
$data_definition += parent::componentDataDefinition();
// 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_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'],
];
}
};
$data_definition['qualified_class_name_pieces']['default'] = function ($component_data) {
$class_name_pieces = array_merge([
'Drupal',
'Tests',
'%module',
], $component_data['relative_class_name']);
return $class_name_pieces;
};
$data_definition['path']['default'] = function($component_data) {
// Lop off the initial Drupal\Tests\module and the final class name to
// build the path.
$path_pieces = array_slice($component_data['qualified_class_name_pieces'], 3, -1);
// Add the initial tests/src to the front.
array_unshift($path_pieces, 'tests/src');
return implode('/', $path_pieces);
};
$data_definition['docblock_first_line']['default'] = function ($component_data) {
return "Test case class TODO.";
};
return $data_definition;
}
/**
* {@inheritdoc}
*/
public function requiredComponents() {
// We have no subcomponents.
return [];
}
/**
* {@inheritdoc}
*/
protected function getClassDocBlockLines() {
$docblock_lines = parent::getClassDocBlockLines();
$docblock_lines[] = '';
$docblock_lines[] = '@group %module';
return $docblock_lines;
}
/**
* {@inheritdoc}
*/
protected function collectSectionBlocks() {
// Set up properties and methods.
// Create the array of modules to install in the test.
$test_install_modules = array_merge(
// Some general defaults.
[
'system',
'user',
],
// The generated module's dependencies.
$this->component_data['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'];
}
}
$this->properties[] = $this->createPropertyBlock(
'modules',
'array',
[
'docblock_first_line' => 'The modules to enable.',
'prefixes' => ['public', 'static'],
'default' => $test_install_modules,
'break_array_value' => TRUE,
]
);
$setup_lines = $this->buildMethodHeader('setUp', [], ['inheritdoc' => TRUE, 'prefixes' => ['protected']]);
$setup_lines[] = ' parent::setUp();';
$setup_lines[] = ' // TODO: setup tasks here.';
$setup_lines[] = '}';
$this->functions[] = $setup_lines;
$test_method_lines = $this->buildMethodHeader('testMyTest', [], ['docblock_first_line' => 'Tests the TODO.', 'prefixes' => ['public']]);
$test_method_lines[] = ' // TODO: test code here.';
$test_method_lines[] = '}';
$this->functions[] = $test_method_lines;
}
}