1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\GeneratedCode; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Inflector\Inflector; |
6
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\AbstractTest; |
7
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Generator\AbstractGenerator; |
8
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Generator\FieldGenerator; |
9
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Generator\RelationsGenerator; |
10
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Config; |
11
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\ConfigInterface; |
12
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException; |
13
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\MappingHelper; |
14
|
|
|
use EdmondsCommerce\PHPQA\Constants; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class GeneratedCodeTest |
18
|
|
|
* @package EdmondsCommerce\DoctrineStaticMeta\GeneratedCode |
19
|
|
|
* @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
20
|
|
|
*/ |
21
|
|
|
class GeneratedCodeTest extends AbstractTest |
22
|
|
|
{ |
23
|
|
|
public const TEST_PROJECT_ROOT_NAMESPACE = 'My\\GeneratedCodeTest\\Project'; |
24
|
|
|
|
25
|
|
|
public const TEST_ENTITY_NAMESPACE_BASE = self::TEST_PROJECT_ROOT_NAMESPACE |
26
|
|
|
.'\\'.AbstractGenerator::ENTITIES_FOLDER_NAME; |
27
|
|
|
|
28
|
|
|
public const TEST_FIELD_TRAIT_NAMESPACE = self::TEST_FIELD_NAMESPACE_BASE.'\\Traits\\'; |
29
|
|
|
|
30
|
|
|
public const TEST_ENTITY_PERSON = self::TEST_ENTITY_NAMESPACE_BASE.'\\Person'; |
31
|
|
|
public const TEST_ENTITY_ADDRESS = self::TEST_ENTITY_NAMESPACE_BASE.'\\Attributes\\Address'; |
32
|
|
|
public const TEST_ENTITY_EMAIL = self::TEST_ENTITY_NAMESPACE_BASE.'\\Attributes\\Email'; |
33
|
|
|
public const TEST_ENTITY_COMPANY = self::TEST_ENTITY_NAMESPACE_BASE.'\\Company'; |
34
|
|
|
public const TEST_ENTITY_DIRECTOR = self::TEST_ENTITY_NAMESPACE_BASE.'\\Company\\Director'; |
35
|
|
|
public const TEST_ENTITY_ORDER = self::TEST_ENTITY_NAMESPACE_BASE.'\\Order'; |
36
|
|
|
public const TEST_ENTITY_ORDER_ADDRESS = self::TEST_ENTITY_NAMESPACE_BASE.'\\Order\\Address'; |
37
|
|
|
|
38
|
|
|
public const TEST_ENTITY_NAME_SPACING_COMPANY = self::TEST_ENTITY_NAMESPACE_BASE.'\\Company'; |
39
|
|
|
public const TEST_ENTITY_NAME_SPACING_SOME_CLIENT = self::TEST_ENTITY_NAMESPACE_BASE.'\\Some\\Client'; |
40
|
|
|
public const TEST_ENTITY_NAME_SPACING_ANOTHER_CLIENT = self::TEST_ENTITY_NAMESPACE_BASE.'\\Another\\Client'; |
41
|
|
|
|
42
|
|
|
public const TEST_ENTITIES = [ |
43
|
|
|
self::TEST_ENTITY_PERSON, |
44
|
|
|
self::TEST_ENTITY_ADDRESS, |
45
|
|
|
self::TEST_ENTITY_EMAIL, |
46
|
|
|
self::TEST_ENTITY_COMPANY, |
47
|
|
|
self::TEST_ENTITY_DIRECTOR, |
48
|
|
|
self::TEST_ENTITY_ORDER, |
49
|
|
|
self::TEST_ENTITY_ORDER_ADDRESS, |
50
|
|
|
self::TEST_ENTITY_NAME_SPACING_COMPANY, |
51
|
|
|
self::TEST_ENTITY_NAME_SPACING_SOME_CLIENT, |
52
|
|
|
self::TEST_ENTITY_NAME_SPACING_ANOTHER_CLIENT |
53
|
|
|
]; |
54
|
|
|
|
55
|
|
|
public const TEST_RELATIONS = [ |
56
|
|
|
[self::TEST_ENTITY_PERSON, RelationsGenerator::HAS_UNIDIRECTIONAL_MANY_TO_ONE, self::TEST_ENTITY_ADDRESS], |
57
|
|
|
[self::TEST_ENTITY_PERSON, RelationsGenerator::HAS_ONE_TO_MANY, self::TEST_ENTITY_EMAIL], |
58
|
|
|
[self::TEST_ENTITY_COMPANY, RelationsGenerator::HAS_MANY_TO_MANY, self::TEST_ENTITY_DIRECTOR], |
59
|
|
|
[self::TEST_ENTITY_COMPANY, RelationsGenerator::HAS_ONE_TO_MANY, self::TEST_ENTITY_ADDRESS], |
60
|
|
|
[self::TEST_ENTITY_COMPANY, RelationsGenerator::HAS_UNIDIRECTIONAL_ONE_TO_MANY, self::TEST_ENTITY_EMAIL], |
61
|
|
|
[self::TEST_ENTITY_DIRECTOR, RelationsGenerator::HAS_ONE_TO_ONE, self::TEST_ENTITY_PERSON], |
62
|
|
|
[self::TEST_ENTITY_ORDER, RelationsGenerator::HAS_MANY_TO_ONE, self::TEST_ENTITY_PERSON], |
63
|
|
|
[self::TEST_ENTITY_ORDER, RelationsGenerator::HAS_ONE_TO_MANY, self::TEST_ENTITY_ORDER_ADDRESS], |
64
|
|
|
[self::TEST_ENTITY_ORDER_ADDRESS, RelationsGenerator::HAS_UNIDIRECTIONAL_ONE_TO_ONE, self::TEST_ENTITY_ADDRESS], |
65
|
|
|
[ |
66
|
|
|
self::TEST_ENTITY_NAME_SPACING_COMPANY, |
67
|
|
|
RelationsGenerator::HAS_ONE_TO_MANY, |
68
|
|
|
self::TEST_ENTITY_NAME_SPACING_SOME_CLIENT |
69
|
|
|
], |
70
|
|
|
[ |
71
|
|
|
self::TEST_ENTITY_NAME_SPACING_COMPANY, |
72
|
|
|
RelationsGenerator::HAS_ONE_TO_MANY, |
73
|
|
|
self::TEST_ENTITY_NAME_SPACING_ANOTHER_CLIENT |
74
|
|
|
], |
75
|
|
|
]; |
76
|
|
|
|
77
|
|
|
public const TEST_FIELD_NAMESPACE_BASE = self::TEST_PROJECT_ROOT_NAMESPACE.'\\Entity\\Fields'; |
78
|
|
|
|
79
|
|
|
protected function assertWeCheckAllPossibleRelationTypes() |
80
|
|
|
{ |
81
|
|
|
$included = $toTest = []; |
82
|
|
|
foreach (RelationsGenerator::HAS_TYPES as $hasType) { |
83
|
|
|
if (0 === \strpos($hasType, RelationsGenerator::PREFIX_INVERSE)) { |
84
|
|
|
continue; |
85
|
|
|
} |
86
|
|
|
$toTest[$hasType] = true; |
87
|
|
|
} |
88
|
|
|
\ksort($toTest); |
89
|
|
|
foreach (self::TEST_RELATIONS as $relation) { |
90
|
|
|
$included[$relation[1]] = true; |
91
|
|
|
} |
92
|
|
|
\ksort($included); |
93
|
|
|
$missing = \array_diff(\array_keys($toTest), \array_keys($included)); |
94
|
|
|
$this->assertEmpty( |
95
|
|
|
$missing, |
96
|
|
|
'We are not testing all relation types - ' |
97
|
|
|
.'these ones have not been included: ' |
98
|
|
|
.print_r($missing, true) |
99
|
|
|
); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public const BASH_PHPNOXDEBUG_FUNCTION = <<<'BASH' |
103
|
|
|
function phpNoXdebug { |
104
|
|
|
debugMode="off" |
105
|
|
|
if [[ "$-" == *x* ]] |
106
|
|
|
then |
107
|
|
|
debugMode='on' |
108
|
|
|
fi |
109
|
|
|
if [[ "$debugMode" == "on" ]] |
110
|
|
|
then |
111
|
|
|
set +x |
112
|
|
|
fi |
113
|
|
|
local returnCode; |
114
|
|
|
local temporaryPath="$(mktemp -t php.XXXX).ini" |
115
|
|
|
# Using awk to ensure that files ending without newlines do not lead to configuration error |
116
|
|
|
/usr/bin/php -i | grep "\.ini" | grep -o -e '\(/[a-z0-9._-]\+\)\+\.ini' | grep -v xdebug \ |
117
|
|
|
| xargs awk 'FNR==1{print ""}1' > "$temporaryPath" |
118
|
|
|
#Run PHP with temp config with no xdebug, display errors on stderr |
119
|
|
|
set +e |
120
|
|
|
/usr/bin/php -n -c "$temporaryPath" "$@" |
121
|
|
|
returnCode=$? |
122
|
|
|
set -e |
123
|
|
|
rm -f "$temporaryPath" |
124
|
|
|
if [[ "$debugMode" == "on" ]] |
125
|
|
|
then |
126
|
|
|
set -x |
127
|
|
|
fi |
128
|
|
|
return ${returnCode}; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
BASH; |
132
|
|
|
/** |
133
|
|
|
* @var string |
134
|
|
|
*/ |
135
|
|
|
private $workDir; |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @throws \Exception |
139
|
|
|
* @throws \Psr\Container\ContainerExceptionInterface |
140
|
|
|
* @throws \Psr\Container\NotFoundExceptionInterface |
141
|
|
|
* @SuppressWarnings(PHPMD.Superglobals) |
142
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
143
|
|
|
*/ |
144
|
|
|
public function setup() |
145
|
|
|
{ |
146
|
|
|
if (isset($_SERVER[Constants::QA_QUICK_TESTS_KEY]) |
147
|
|
|
&& (int)$_SERVER[Constants::QA_QUICK_TESTS_KEY] === Constants::QA_QUICK_TESTS_ENABLED |
148
|
|
|
) { |
149
|
|
|
return; |
150
|
|
|
} |
151
|
|
|
$this->workDir = $this->isTravis() ? |
152
|
|
|
AbstractTest::VAR_PATH.'/GeneratedCodeTest' |
153
|
|
|
: sys_get_temp_dir().'/dsm/test-project'; |
154
|
|
|
$this->entitiesPath = $this->workDir.'/src/Entities'; |
155
|
|
|
$this->getFileSystem()->mkdir($this->workDir); |
156
|
|
|
$this->emptyDirectory($this->workDir); |
157
|
|
|
$this->getFileSystem()->mkdir($this->entitiesPath); |
158
|
|
|
$this->setupContainer(); |
159
|
|
|
$this->initRebuildFile(); |
160
|
|
|
$this->setupGeneratedDb(); |
161
|
|
|
$this->initComposerAndInstall(); |
162
|
|
|
$fileSystem = $this->getFileSystem(); |
163
|
|
|
$fileSystem->mkdir( |
164
|
|
|
[ |
165
|
|
|
$this->workDir.'/tests/', |
166
|
|
|
$this->workDir.'/cache/Proxies', |
167
|
|
|
$this->workDir.'/cache/qa', |
168
|
|
|
$this->workDir.'/qaConfig', |
169
|
|
|
] |
170
|
|
|
); |
171
|
|
|
$fileSystem->copy( |
172
|
|
|
__DIR__.'/../../qaConfig/phpunit.xml', |
173
|
|
|
$this->workDir.'/qaConfig/phpunit.xml' |
174
|
|
|
); |
175
|
|
|
$fileSystem->copy( |
176
|
|
|
__DIR__.'/../../qaConfig/phpunit-with-coverage.xml', |
177
|
|
|
$this->workDir.'/qaConfig/phpunit-with-coverage.xml' |
178
|
|
|
); |
179
|
|
|
$fileSystem->copy(__DIR__.'/../../cli-config.php', $this->workDir.'/cli-config.php'); |
180
|
|
|
file_put_contents($this->workDir.'/README.md', '#Generated Code'); |
181
|
|
|
|
182
|
|
|
$this->addToRebuildFile(self::BASH_PHPNOXDEBUG_FUNCTION); |
183
|
|
|
|
184
|
|
|
$entities = $this->generateEntities(); |
185
|
|
|
$standardFieldEntity = $this->generateStandardFieldEntity(); |
186
|
|
|
$this->generateRelations(); |
187
|
|
|
$this->generateFields(); |
188
|
|
|
$this->setFields( |
189
|
|
|
$entities, |
190
|
|
|
$this->getFieldFqns() |
191
|
|
|
); |
192
|
|
|
$this->setFields( |
193
|
|
|
[$standardFieldEntity], |
194
|
|
|
FieldGenerator::STANDARD_FIELDS |
195
|
|
|
); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
protected function initRebuildFile() |
199
|
|
|
{ |
200
|
|
|
$bash = |
201
|
|
|
<<<'BASH' |
202
|
|
|
#!/usr/bin/env bash |
203
|
|
|
readonly DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"; |
204
|
|
|
cd "$DIR"; |
205
|
|
|
set -e |
206
|
|
|
set -u |
207
|
|
|
set -o pipefail |
208
|
|
|
standardIFS="$IFS" |
209
|
|
|
IFS=$'\n\t' |
210
|
|
|
echo " |
211
|
|
|
=========================================== |
212
|
|
|
$(hostname) $0 $@ |
213
|
|
|
=========================================== |
214
|
|
|
" |
215
|
|
|
# Error Handling |
216
|
|
|
backTraceExit () { |
217
|
|
|
local err=$? |
218
|
|
|
set +o xtrace |
219
|
|
|
local code="${1:-1}" |
220
|
|
|
printf "\n\nError in ${BASH_SOURCE[1]}:${BASH_LINENO[0]}. '${BASH_COMMAND}'\n\n exited with status: \n\n$err\n\n" |
221
|
|
|
# Print out the stack trace described by $function_stack |
222
|
|
|
if [ ${#FUNCNAME[@]} -gt 2 ] |
223
|
|
|
then |
224
|
|
|
echo "Call tree:" |
225
|
|
|
for ((i=1;i<${#FUNCNAME[@]}-1;i++)) |
226
|
|
|
do |
227
|
|
|
echo " $i: ${BASH_SOURCE[$i+1]}:${BASH_LINENO[$i]} ${FUNCNAME[$i]}(...)" |
228
|
|
|
done |
229
|
|
|
fi |
230
|
|
|
echo "Exiting with status ${code}" |
231
|
|
|
exit "${code}" |
232
|
|
|
} |
233
|
|
|
trap 'backTraceExit' ERR |
234
|
|
|
set -o errtrace |
235
|
|
|
# Error Handling Ends |
236
|
|
|
|
237
|
|
|
echo "clearing out generated code" |
238
|
|
|
rm -rf src/* tests/* |
239
|
|
|
|
240
|
|
|
echo "preparing empty Entities directory" |
241
|
|
|
mkdir src/Entities |
242
|
|
|
|
243
|
|
|
echo "making sure we have the latest version of code" |
244
|
|
|
(cd vendor/edmondscommerce/doctrine-static-meta && git pull) |
245
|
|
|
|
246
|
|
|
BASH; |
247
|
|
|
if (!$this->isTravis()) { |
248
|
|
|
$bash .= self::BASH_PHPNOXDEBUG_FUNCTION; |
249
|
|
|
} |
250
|
|
|
file_put_contents( |
251
|
|
|
$this->workDir.'/rebuild.bash', |
252
|
|
|
"\n\n".$bash |
253
|
|
|
); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* @return bool |
258
|
|
|
* @SuppressWarnings(PHPMD.Superglobals) |
259
|
|
|
*/ |
260
|
|
|
protected function isTravis(): bool |
261
|
|
|
{ |
262
|
|
|
return isset($_SERVER['TRAVIS']); |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* @return string Generated Database Name |
267
|
|
|
* @throws \Exception |
268
|
|
|
* @throws \Psr\Container\ContainerExceptionInterface |
269
|
|
|
* @throws \Psr\Container\NotFoundExceptionInterface |
270
|
|
|
*/ |
271
|
|
|
protected function setupGeneratedDb(): string |
272
|
|
|
{ |
273
|
|
|
$dbHost = $this->container->get(Config::class)->get(ConfigInterface::PARAM_DB_HOST); |
274
|
|
|
$dbUser = $this->container->get(Config::class)->get(ConfigInterface::PARAM_DB_USER); |
275
|
|
|
$dbPass = $this->container->get(Config::class)->get(ConfigInterface::PARAM_DB_PASS); |
276
|
|
|
$dbName = $this->container->get(Config::class)->get(ConfigInterface::PARAM_DB_NAME); |
277
|
|
|
$link = mysqli_connect($dbHost, $dbUser, $dbPass); |
278
|
|
|
if (!$link) { |
|
|
|
|
279
|
|
|
throw new DoctrineStaticMetaException('Failed getting connection in '.__METHOD__); |
280
|
|
|
} |
281
|
|
|
$generatedDbName = $dbName.'_generated'; |
282
|
|
|
mysqli_query($link, "DROP DATABASE IF EXISTS $generatedDbName"); |
283
|
|
|
mysqli_query($link, "CREATE DATABASE $generatedDbName |
284
|
|
|
CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci"); |
285
|
|
|
mysqli_close($link); |
286
|
|
|
|
287
|
|
|
$rebuildBash = <<<BASH |
288
|
|
|
echo "Dropping and creating the DB $generatedDbName" |
289
|
|
|
mysql -u $dbUser -p$dbPass -h $dbHost -e "DROP DATABASE IF EXISTS $generatedDbName"; |
290
|
|
|
mysql -u $dbUser -p$dbPass -h $dbHost -e "CREATE DATABASE $generatedDbName |
291
|
|
|
CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci"; |
292
|
|
|
BASH; |
293
|
|
|
$this->addToRebuildFile($rebuildBash); |
294
|
|
|
file_put_contents( |
295
|
|
|
$this->workDir.'/.env', |
296
|
|
|
<<<EOF |
297
|
|
|
export dbUser="{$dbUser}" |
298
|
|
|
export dbPass="{$dbPass}" |
299
|
|
|
export dbHost="{$dbHost}" |
300
|
|
|
export dbName="$generatedDbName" |
301
|
|
|
EOF |
302
|
|
|
); |
303
|
|
|
|
304
|
|
|
return $generatedDbName; |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
/** |
308
|
|
|
* @param string $bash |
309
|
|
|
* |
310
|
|
|
* @return bool |
311
|
|
|
* @throws \Exception |
312
|
|
|
*/ |
313
|
|
|
protected function addToRebuildFile(string $bash): bool |
314
|
|
|
{ |
315
|
|
|
$result = file_put_contents( |
316
|
|
|
$this->workDir.'/rebuild.bash', |
317
|
|
|
"\n\n".$bash."\n\n", |
318
|
|
|
FILE_APPEND |
319
|
|
|
); |
320
|
|
|
if (!$result) { |
321
|
|
|
throw new \RuntimeException('Failed writing to rebuild file'); |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
return true; |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
protected function initComposerAndInstall() |
328
|
|
|
{ |
329
|
|
|
$vcsPath = realpath(__DIR__.'/../../'); |
330
|
|
|
|
331
|
|
|
$composerJson = <<<'JSON' |
332
|
|
|
{ |
333
|
|
|
"require": { |
334
|
|
|
"edmondscommerce/doctrine-static-meta": "dev-%s" |
335
|
|
|
}, |
336
|
|
|
"repositories": [ |
337
|
|
|
{ |
338
|
|
|
"type": "vcs", |
339
|
|
|
"url": "%s" |
340
|
|
|
}, |
341
|
|
|
{ |
342
|
|
|
"type": "vcs", |
343
|
|
|
"url": "https://github.com/edmondscommerce/Faker.git" |
344
|
|
|
} |
345
|
|
|
], |
346
|
|
|
"minimum-stability": "dev", |
347
|
|
|
"require-dev": { |
348
|
|
|
"phpunit/phpunit": "^6.3", |
349
|
|
|
"fzaninotto/faker": "^1.7", |
350
|
|
|
"edmondscommerce/phpqa": "dev-master@dev" |
351
|
|
|
}, |
352
|
|
|
"autoload": { |
353
|
|
|
"psr-4": { |
354
|
|
|
"My\\GeneratedCodeTest\\Project\\": [ |
355
|
|
|
"src/" |
356
|
|
|
] |
357
|
|
|
} |
358
|
|
|
}, |
359
|
|
|
"autoload-dev": { |
360
|
|
|
"psr-4": { |
361
|
|
|
"My\\GeneratedCodeTest\\Project\\": [ |
362
|
|
|
"tests/" |
363
|
|
|
] |
364
|
|
|
} |
365
|
|
|
}, |
366
|
|
|
"config": { |
367
|
|
|
"bin-dir": "bin", |
368
|
|
|
"preferred-install": { |
369
|
|
|
"edmondscommerce/*": "source", |
370
|
|
|
"*": "dist" |
371
|
|
|
}, |
372
|
|
|
"optimize-autoloader": true |
373
|
|
|
} |
374
|
|
|
} |
375
|
|
|
JSON; |
376
|
|
|
|
377
|
|
|
$gitCurrentBranchName = trim(shell_exec("git branch | grep '*' | cut -d ' ' -f 2")); |
378
|
|
|
file_put_contents( |
379
|
|
|
$this->workDir.'/composer.json', |
380
|
|
|
sprintf($composerJson, $gitCurrentBranchName, $vcsPath) |
381
|
|
|
); |
382
|
|
|
|
383
|
|
|
$phpCmd = $this->isTravis() ? 'php' : 'phpNoXdebug'; |
384
|
|
|
$bashCmds = <<<BASH |
385
|
|
|
|
386
|
|
|
$phpCmd $(which composer) install \ |
387
|
|
|
--prefer-dist |
388
|
|
|
|
389
|
|
|
$phpCmd $(which composer) dump-autoload --optimize |
390
|
|
|
|
391
|
|
|
BASH; |
392
|
|
|
$this->execBash($bashCmds); |
393
|
|
|
} |
394
|
|
|
|
395
|
|
|
/** |
396
|
|
|
* Runs bash with strict error handling and verbose logging |
397
|
|
|
* |
398
|
|
|
* Will ensure the phpNoXdebugFunction is available and will CD into the correct directory before running commands |
399
|
|
|
* |
400
|
|
|
* Asserts that the command returns with an exit code of 0 |
401
|
|
|
* |
402
|
|
|
* Appends to the rebuild file allowing easy rerunning of the commmands in the test project |
403
|
|
|
* |
404
|
|
|
* @param string $bashCmds |
405
|
|
|
* |
406
|
|
|
* @throws \Exception |
407
|
|
|
*/ |
408
|
|
|
protected function execBash(string $bashCmds) |
409
|
|
|
{ |
410
|
|
|
fwrite(STDERR, "\n\t# Executing:\n$bashCmds"); |
411
|
|
|
$startTime = microtime(true); |
412
|
|
|
|
413
|
|
|
$fullCmds = ''; |
414
|
|
|
if (!$this->isTravis()) { |
415
|
|
|
$fullCmds .= "\n".self::BASH_PHPNOXDEBUG_FUNCTION."\n\n"; |
416
|
|
|
} |
417
|
|
|
$fullCmds .= "cd {$this->workDir};\n"; |
418
|
|
|
$fullCmds .= "set -xe;\n"; |
419
|
|
|
$fullCmds .= "2>&1;\n"; |
420
|
|
|
$fullCmds .= "$bashCmds\n"; |
421
|
|
|
|
422
|
|
|
$output = []; |
423
|
|
|
$exitCode = 0; |
424
|
|
|
exec($fullCmds, $output, $exitCode); |
425
|
|
|
|
426
|
|
|
if (0 !== $exitCode) { |
427
|
|
|
throw new \RuntimeException( |
428
|
|
|
"Error running bash commands:\n\nOutput:\n----------\n\n" |
429
|
|
|
. implode("\n", $output) |
430
|
|
|
."\n\nCommands:\n----------\n" |
431
|
|
|
.str_replace( |
432
|
|
|
"\n", |
433
|
|
|
"\n\t", |
434
|
|
|
"\n$bashCmds" |
435
|
|
|
)."\n\n" |
436
|
|
|
); |
437
|
|
|
} |
438
|
|
|
|
439
|
|
|
$seconds = round(microtime(true) - $startTime, 2); |
440
|
|
|
fwrite(STDERR, "\n\t\t#Completed in $seconds seconds\n"); |
441
|
|
|
} |
442
|
|
|
|
443
|
|
|
protected function generateEntity(string $entityFqn) |
444
|
|
|
{ |
445
|
|
|
$namespace = self::TEST_PROJECT_ROOT_NAMESPACE; |
446
|
|
|
$doctrineCmd = <<<DOCTRINE |
447
|
|
|
dsm:generate:entity \ |
448
|
|
|
--project-root-namespace="{$namespace}" \ |
449
|
|
|
--entity-fully-qualified-name="{$entityFqn}" |
450
|
|
|
DOCTRINE; |
451
|
|
|
$this->execDoctrine($doctrineCmd); |
452
|
|
|
} |
453
|
|
|
|
454
|
|
|
protected function generateUuidEntity(string $entityFqn) |
455
|
|
|
{ |
456
|
|
|
$namespace = self::TEST_PROJECT_ROOT_NAMESPACE; |
457
|
|
|
$doctrineCmd = <<<DOCTRINE |
458
|
|
|
dsm:generate:entity \ |
459
|
|
|
--project-root-namespace="{$namespace}" \ |
460
|
|
|
--entity-fully-qualified-name="{$entityFqn}" \ |
461
|
|
|
--uuid-primary-key |
462
|
|
|
DOCTRINE; |
463
|
|
|
$this->execDoctrine($doctrineCmd); |
464
|
|
|
} |
465
|
|
|
|
466
|
|
|
protected function generateField(string $propertyName, string $type) |
467
|
|
|
{ |
468
|
|
|
$namespace = self::TEST_PROJECT_ROOT_NAMESPACE; |
469
|
|
|
$doctrineCmd = <<<DOCTRINE |
470
|
|
|
dsm:generate:field \ |
471
|
|
|
--project-root-path="{$this->workDir}" \ |
472
|
|
|
--project-root-namespace="{$namespace}" \ |
473
|
|
|
--field-fully-qualified-name="{$propertyName}" \ |
474
|
|
|
--field-property-doctrine-type="{$type}" |
475
|
|
|
DOCTRINE; |
476
|
|
|
$this->execDoctrine($doctrineCmd); |
477
|
|
|
} |
478
|
|
|
|
479
|
|
|
protected function setField(string $entityFqn, string $fieldFqn) |
480
|
|
|
{ |
481
|
|
|
$namespace = self::TEST_PROJECT_ROOT_NAMESPACE; |
482
|
|
|
$doctrineCmd = <<<DOCTRINE |
483
|
|
|
dsm:set:field \ |
484
|
|
|
--project-root-path="{$this->workDir}" \ |
485
|
|
|
--project-root-namespace="{$namespace}" \ |
486
|
|
|
--entity="{$entityFqn}" \ |
487
|
|
|
--field="{$fieldFqn}" |
488
|
|
|
DOCTRINE; |
489
|
|
|
$this->execDoctrine($doctrineCmd); |
490
|
|
|
} |
491
|
|
|
|
492
|
|
|
protected function execDoctrine(string $doctrineCmds) |
493
|
|
|
{ |
494
|
|
|
$phpCmd = $this->isTravis() ? 'php' : 'phpNoXdebug'; |
495
|
|
|
$bash = <<<BASH |
496
|
|
|
$phpCmd bin/doctrine $doctrineCmds |
497
|
|
|
BASH; |
498
|
|
|
$error = false; |
499
|
|
|
$message = ''; |
500
|
|
|
try { |
501
|
|
|
$this->execBash($bash); |
502
|
|
|
} catch (\RuntimeException $e) { |
503
|
|
|
$this->addToRebuildFile("\n\nexit 0;\n\n#The command below failed...\n\n"); |
504
|
|
|
$error = true; |
505
|
|
|
$message = $e->getMessage(); |
506
|
|
|
} |
507
|
|
|
$this->addToRebuildFile($bash); |
508
|
|
|
$this->assertFalse($error, $message); |
509
|
|
|
} |
510
|
|
|
|
511
|
|
|
protected function setRelation(string $entity1, string $type, string $entity2) |
512
|
|
|
{ |
513
|
|
|
$namespace = self::TEST_PROJECT_ROOT_NAMESPACE; |
514
|
|
|
$this->execDoctrine( |
515
|
|
|
<<<DOCTRINE |
516
|
|
|
dsm:set:relation \ |
517
|
|
|
--project-root-path="{$this->workDir}" \ |
518
|
|
|
--project-root-namespace="{$namespace}" \ |
519
|
|
|
--entity1="{$entity1}" \ |
520
|
|
|
--hasType="{$type}" \ |
521
|
|
|
--entity2="{$entity2}" |
522
|
|
|
DOCTRINE |
523
|
|
|
); |
524
|
|
|
} |
525
|
|
|
|
526
|
|
|
/** |
527
|
|
|
* @SuppressWarnings(PHPMD.Superglobals) |
528
|
|
|
* @throws \Exception |
529
|
|
|
*/ |
530
|
|
|
public function testRunTests() |
531
|
|
|
{ |
532
|
|
|
$this->assertWeCheckAllPossibleRelationTypes(); |
533
|
|
|
if (isset($_SERVER[Constants::QA_QUICK_TESTS_KEY]) |
534
|
|
|
&& (int)$_SERVER[Constants::QA_QUICK_TESTS_KEY] === Constants::QA_QUICK_TESTS_ENABLED |
535
|
|
|
) { |
536
|
|
|
$this->markTestSkipped('Quick tests is enabled'); |
537
|
|
|
} |
538
|
|
|
/** @lang bash */ |
539
|
|
|
$bashCmds = <<<BASH |
540
|
|
|
|
541
|
|
|
set +x |
542
|
|
|
|
543
|
|
|
echo " |
544
|
|
|
|
545
|
|
|
-------------------------------------------------- |
546
|
|
|
STARTS Running Tests In {$this->workDir} |
547
|
|
|
-------------------------------------------------- |
548
|
|
|
|
549
|
|
|
" |
550
|
|
|
|
551
|
|
|
bin/qa |
552
|
|
|
|
553
|
|
|
echo " |
554
|
|
|
|
555
|
|
|
-------------------------------------------------- |
556
|
|
|
DONE Running Tests In {$this->workDir} |
557
|
|
|
-------------------------------------------------- |
558
|
|
|
|
559
|
|
|
" |
560
|
|
|
set -x |
561
|
|
|
BASH; |
562
|
|
|
$this->execBash($bashCmds); |
563
|
|
|
} |
564
|
|
|
|
565
|
|
|
/** |
566
|
|
|
* Generate all test entities |
567
|
|
|
* |
568
|
|
|
* @return array |
569
|
|
|
*/ |
570
|
|
|
protected function generateEntities(): array |
571
|
|
|
{ |
572
|
|
|
foreach (self::TEST_ENTITIES as $entityFqn) { |
573
|
|
|
$this->generateEntity($entityFqn); |
574
|
|
|
} |
575
|
|
|
|
576
|
|
|
return self::TEST_ENTITIES; |
577
|
|
|
} |
578
|
|
|
|
579
|
|
|
// protected function fieldFqnToEntityFqn(string $fieldFqn) |
580
|
|
|
// { |
581
|
|
|
// $fieldNameParts = explode('\\', $fieldFqn); |
582
|
|
|
// $fieldName = array_pop($fieldNameParts); |
583
|
|
|
// $entityName = str_replace('FieldTrait', '', $fieldName); |
584
|
|
|
// return self::TEST_ENTITY_NAMESPACE_BASE . '\\Standard\\Field\\' . $entityName; |
585
|
|
|
// } |
586
|
|
|
|
587
|
|
|
/** |
588
|
|
|
* @return string |
589
|
|
|
*/ |
590
|
|
|
protected function generateStandardFieldEntity(): string |
591
|
|
|
{ |
592
|
|
|
$entityFqn = self::TEST_ENTITY_NAMESPACE_BASE . '\\Standard\\Field'; |
593
|
|
|
$this->generateUuidEntity($entityFqn); |
594
|
|
|
return $entityFqn; |
595
|
|
|
} |
596
|
|
|
|
597
|
|
|
/** |
598
|
|
|
* Generate all test relations |
599
|
|
|
* |
600
|
|
|
* @return void |
601
|
|
|
*/ |
602
|
|
|
protected function generateRelations(): void |
603
|
|
|
{ |
604
|
|
|
foreach (self::TEST_RELATIONS as $relation) { |
605
|
|
|
$this->setRelation(...$relation); |
|
|
|
|
606
|
|
|
} |
607
|
|
|
} |
608
|
|
|
|
609
|
|
|
/** |
610
|
|
|
* Generate one field per common type |
611
|
|
|
* |
612
|
|
|
* @return void |
613
|
|
|
*/ |
614
|
|
|
protected function generateFields(): void |
615
|
|
|
{ |
616
|
|
|
foreach (MappingHelper::COMMON_TYPES as $type) { |
617
|
|
|
$fieldFqn = self::TEST_FIELD_TRAIT_NAMESPACE.'\\'.$type; |
618
|
|
|
$this->generateField($fieldFqn, $type); |
619
|
|
|
} |
620
|
|
|
} |
621
|
|
|
|
622
|
|
|
/** |
623
|
|
|
* Set each field type on each entity type |
624
|
|
|
* |
625
|
|
|
* @param array $entities |
626
|
|
|
* @param array $fields |
627
|
|
|
* @return void |
628
|
|
|
*/ |
629
|
|
|
protected function setFields(array $entities, array $fields): void |
630
|
|
|
{ |
631
|
|
|
foreach ($entities as $entityFqn) { |
632
|
|
|
foreach ($fields as $fieldFqn) { |
633
|
|
|
$this->setField($entityFqn, $fieldFqn); |
634
|
|
|
} |
635
|
|
|
} |
636
|
|
|
} |
637
|
|
|
|
638
|
|
|
/** |
639
|
|
|
* @return array |
640
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
641
|
|
|
*/ |
642
|
|
|
protected function getFieldFqns(): array |
643
|
|
|
{ |
644
|
|
|
$fieldFqns = []; |
645
|
|
|
foreach (MappingHelper::COMMON_TYPES as $type) { |
646
|
|
|
$fieldFqns[] = self::TEST_FIELD_TRAIT_NAMESPACE . Inflector::classify($type) . 'FieldTrait'; |
647
|
|
|
} |
648
|
|
|
|
649
|
|
|
return $fieldFqns; |
650
|
|
|
} |
651
|
|
|
} |
652
|
|
|
|