Skip to content

Commit 34ef832

Browse files
committed
more accurate generation tuning
1 parent 3d98ef6 commit 34ef832

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

Command/FixtureGenerateCommand.php

+17-9
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,10 @@ protected function execute(InputInterface $input = null, OutputInterface $output
7878

7979
$configPath = $this->getContainer()->getParameter('gamma_fixtures_generator.fixture_references_file_name');
8080
if ($configPath == 'fixtureReferences.txt') {
81-
$configPath = $this->getContainer()->get('kernel')->getRootDir().'/../'.$configPath;
81+
$configPath = $this->getContainer()->get('kernel')->getRootDir().'/../app/cache/dev/'.$configPath;
82+
}
83+
if (!file_exists($configPath)) {
84+
touch($configPath);
8285
}
8386
$fixtureReferencesFileName = $configPath;
8487

@@ -309,9 +312,12 @@ private function writeEntityProperties($variable, $entity, $metadata)
309312

310313
/* @var $getterName string */
311314
$getterName = 'get'.$fieldNameCapitalized;
315+
unset($fieldValue);
312316

313317
/* @var $associationEntity mixed|null */
314-
$fieldValue = $entity->$getterName();
318+
if (method_exists($entity, $getterName)) {
319+
$fieldValue = $entity->$getterName();
320+
}
315321

316322
/* @var $fieldMapping array */
317323
$fieldMapping = $metadata->getFieldMapping($fieldName);
@@ -332,7 +338,7 @@ private function writeEntityProperties($variable, $entity, $metadata)
332338
$fieldValueStr = 'null';
333339

334340
// todo: representing field value in a string should be refactored
335-
if ($fieldValue) {
341+
if (isset($fieldValue)) {
336342
if (\in_array($fieldType, array('string', 'text'))) {
337343
if (\is_string($fieldValue)) {
338344
$fieldValueStr = "'".\addslashes($fieldValue)."'";
@@ -341,12 +347,12 @@ private function writeEntityProperties($variable, $entity, $metadata)
341347
}
342348
} elseif ($fieldType === 'boolean') {
343349
$fieldValueStr = $fieldValue ? 'true' : 'false';
344-
} elseif ($fieldType === 'datetime') {
345-
$fieldValueStr = "new \DateTime('".$fieldValue->format('Y-m-d H:i:s')."')";
346-
} elseif ($fieldType === 'date') {
350+
} elseif ($fieldValue instanceof \DateTime) {
351+
$fieldValueStr = $fieldValue ? "new \DateTime('".$fieldValue->format('Y-m-d H:i:s')."')" : '';
352+
} elseif ($fieldValue instanceof \Date) {
347353
$fieldValueStr = "new \DateTime('".$fieldValue->format('Y-m-d')."')";
348-
} elseif ($fieldType == 'array') {
349-
$fieldValueStr = "'".\addslashes(json_encode($fieldValue))."'";
354+
} elseif (is_array($fieldValue)) {
355+
$fieldValueStr = json_encode($fieldValue);
350356
} else {
351357
$fieldValueStr = $fieldValue;
352358
}
@@ -367,7 +373,9 @@ private function writeEntityProperties($variable, $entity, $metadata)
367373
/* @var $setterMethod string */
368374
$setterMethod = 'set'.$fieldNameCapitalized;
369375

370-
$this->writeLine('$'.$variable.'->'.$setterMethod.'('.$fieldValueStr.');');
376+
if (method_exists($entity, $setterMethod)) {
377+
$this->writeLine('$'.$variable.'->'.$setterMethod.'('.$fieldValueStr.');');
378+
}
371379
}
372380
}
373381
}

0 commit comments

Comments
 (0)