-
Notifications
You must be signed in to change notification settings - Fork 1
/
php-unit-default-adapter.js
302 lines (269 loc) · 10.3 KB
/
php-unit-default-adapter.js
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
/** @babel */
import Path from 'path'
import {removeLeadingChars, offsetRange, offsetPoint} from '../util/php-unit-utils'
/**
* An encapsulation of how and where to create the test files/classes/methods
*/
export default class DefaultAdapter
{
/**
* Creates a description of the target class/method based on the source
*
* The given object is a clone and safe to modify and return.
*
* @param {PhpUnitScope} scope - A description of the source
*
* @return {PhpUnitScope} - The translated scope
*/
describeTarget (scope) {
if (scope.isTestFile()) {
throw new Error('An invalid source was passed to describeTarget')
}
const sourceClassName = scope.getShortClassName()
const sourceNamespace = scope.getNamespace()
const sourceFileName = Path.basename(scope.getPath())
const sourcePath = scope.getPath()
const sourceMethodName = scope.getMethodName()
const sourceDirectoryName = this.getSourceDirectoryName()
const leadingSegments = sourceNamespace.slice(1).split('\\')
const trailingSegments = []
const targetClassName = this.getTargetClassName(sourceClassName)
let targetPath, targetNamespace, targetMethod
let rootPath = Path.dirname(sourcePath)
if (sourcePath !== rootPath) {
while (Path.basename(rootPath) === leadingSegments.slice(-1)) {
rootPath = Path.dirname(rootPath)
trailingSegments.unshift(leadingSegments.pop())
}
if (sourceDirectoryName === Path.basename(rootPath)) {
rootPath = Path.dirname(rootPath)
const targetFileName = this.getTargetFileName({
sourceFileName,
sourceClassName,
targetClassName
})
targetPath = this.getTargetPath({
sourcePath,
rootPath,
trailingSegments,
targetFileName
})
targetNamespace = this.getTargetNamespaceName({
sourceNamespace,
leadingSegments,
trailingSegments
})
}
}
if (!targetPath || !targetNamespace) {
return null
}
if (sourceMethodName) {
targetMethod = this.getTargetMethodName(sourceMethodName)
}
return scope.update({
name: targetClassName,
namespace: targetNamespace,
path: targetPath,
method: targetMethod
})
}
/**
* Returns the directory name under which source files are stored
*
* @return {String}
*/
getSourceDirectoryName () {
return 'src'
}
/**
* Returns the directory name under which the test files are stored
*
* @return {String}
*/
getTargetDirectoryName () {
return 'tests'
}
/**
* Returns the namespace segment for the target test directory
*
* @return {String}
*/
getTargetNamespaceSegment () {
return 'Test'
}
/**
* Returns the short class name for the target test class
*
* @param {String} sourceClassName - The short class name of the source class
*
* @return {String}
*/
getTargetClassName (sourceClassName) {
return sourceClassName + 'Test'
}
/**
* Returns the namespace under which the target class will be created
*
* @param {Object} params - A collection of parameters which may help to build the string
* @param {String} params.sourceNamespace - The full namespace under which the source class was declared
* @param {Array<String>} params.leadingSegments - The namespace segments up to the source directory (exclusive)
* @param {Array<String>} params.trailingSegments - The namespace segments from the source directory (inclusive)
*
* @return {String} - The full namespace, with leading slashes
*/
getTargetNamespaceName ({sourceNamespace, leadingSegments, trailingSegments}) { // eslint-disable-line no-unused-vars
return '\\' + leadingSegments.concat(this.getTargetNamespaceSegment(), trailingSegments).join('\\')
}
/**
* Returns the name of the file containing the test class
*
* @param {Object} params - A collection of parameters which may help to build the string
* @param {String} params.sourceFileName - The name of the file containing the source class
* @param {String} params.sourceClassName - The short class name of the source class
* @param {String} params.targetClassName - The modified short name of the target class
*
* @return {String} - The file name with extension
*/
getTargetFileName ({sourceFileName, sourceClassName, targetClassName}) { // eslint-disable-line no-unused-vars
return targetClassName + '.php'
}
/**
* Returns the directory path where the target file resides
*
* @param {Object} params - A collection of parameters which may help to build the string
* @param {String} params.sourcePath - The full path to the source directory containing the source class
* @param {String} params.rootPath - The full path to the directory containing both source and target directories
* @param {Array<String>} params.trailingSegments - The namespace segments from the source directory (inclusive)
* @param {String} params.targetFileName - The translated target file name to append
*
* @return {String} - The full path to the target file
*/
getTargetPath ({sourcePath, rootPath, trailingSegments, targetFileName}) { // eslint-disable-line no-unused-vars
return Path.join(rootPath, this.getTargetDirectoryName(), ...trailingSegments, targetFileName)
}
/**
* Returns the method name within the target class
*
* @param {String} sourceMethodName - The source method being tested
*
* @return {String} - The test method name
*/
getTargetMethodName (sourceMethodName) {
return 'test' + sourceMethodName.charAt(0).toUpperCase() + sourceMethodName.slice(1)
}
/**
* Creates the PHP code for the test suite class
*
* @param {Object} source - A description of the source class
* @param {Object} target - A description of the target class
*
* @return {String|{text: string, cursor: Point=, select: Range=}}
*/
createTestSuite (source, target) {
const className = source.getShortClassName()
const propName = className.charAt(0).toLowerCase() + className.slice(1)
const tab = this.getIndent()
let setup = ''
switch (target.type) {
case 'class':
setup = `new ${className}`
break
case 'trait':
setup = `$this->getMockForTrait(${className}::class)`
break
case 'abstract':
setup = `$this->getMockForAbstractClass(${className}::class)`
break
}
const code = [
'<?php',
'declare(strict_types=1);',
'',
`namespace ${removeLeadingChars('\\', target.getNamespace())};`,
'',
'use PHPUnit\\Framework\\TestCase;',
`use ${removeLeadingChars('\\', source.getFullClassName())};`,
'',
'/**',
` * Test Case for the ${className} ${target.type === 'trait' ? 'trait' : 'class'}.`,
' */',
`class ${target.getShortClassName()} extends TestCase`,
'{',
tab + '/**',
tab + ` * @var ${className}`,
tab + ' */',
tab + `protected $${propName};`,
'',
tab + '/**',
tab + ' * This method is called before each test.',
tab + ' */',
tab + 'protected function setUp()',
tab + '{',
tab + tab + `$this->${propName} = ${setup};`,
tab + '}',
'',
tab + '/**',
tab + ' * This method is called after each test.',
tab + ' */',
tab + 'protected function tearDown()',
tab + '{',
tab + '}',
'',
]
const meta = this.createTestCase(source, target)
var method, cursor, select
if (meta && meta.text) {
const ins = code.length - 1
cursor = offsetPoint([ins, 0], meta.cursor)
select = offsetRange([[ins, 0], [ins, 0]], meta.select)
method = meta.text
} else {
method = ''
cursor = [8, 0]
}
return {
text: code.join('\n') + method + '}\n',
cursor,
select
}
}
/**
* Creates the PHP function to add to the main test suite
*
* @param {Object} source - A description of the source method
* @param {Object} target - A description of the target method
*
* @return {String|{text: string, cursor: Point=, select: Range=}}
*/
createTestCase (source, target) {
const tab = this.getIndent()
if (target.hasMethod() && source.hasMethod()) {
const code = [
'',
tab + '/**',
tab + ` * @covers ${removeLeadingChars('\\', source.getFullClassName())}::${source.getMethodName()}`,
tab + ' */',
tab + `public function ${target.getMethodName()}()`,
tab + '{',
tab + tab + '$this->markTestIncomplete("This test has not yet been written.");',
tab + '}',
'',
]
return {
text: code.join('\n'),
cursor: [6, tab.length * 2],
select: [[6, tab.length * 2], [6, 65 + (tab.length * 2)]]
}
}
return ''
}
/**
* Returns the character(s) required to indent a line
*
* @return {String} - A single indentation
*/
getIndent () {
return ' '
}
}