Skip to content

Commit

Permalink
#16 : DoctrineEntityExtractor handle many to many relations
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-bastien committed Dec 20, 2018
1 parent 76a51b7 commit 5a7809e
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 5 deletions.
12 changes: 11 additions & 1 deletion src/Extractor/DoctrineEntityExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,16 @@ public function extract()
throw new EntityTypeNotHandledException(get_class($value));
}
$entityData[$property] = '@' . $value->getImportId();
} elseif ($value instanceof \Traversable) {
$entityData[$property] = [];
foreach ($value as $k => $v) {
if ($this->isEntityRelation($v)) {
if (!$v instanceof ImportableInterface) {
throw new EntityTypeNotHandledException(get_class($v));
}
$entityData[$property][$k] = '@' . $v->getImportId();
}
}
} else {
$entityData[$property] = $value;
}
Expand All @@ -125,6 +135,6 @@ public function extract()
*/
protected function isEntityRelation($propertyValue)
{
return (is_object($propertyValue) && !($propertyValue instanceof \DateTime));
return (is_object($propertyValue) && !($propertyValue instanceof \DateTime) && !($propertyValue instanceof \Traversable));
}
}
12 changes: 12 additions & 0 deletions tests/Entity/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,16 @@ public function setCode($code)
{
$this->code = $code;
}

/**
* @return string
*/
public function getImportId()
{
if (is_null($this->importId)) {
return $this->getCode();
}

return $this->importId;
}
}
4 changes: 2 additions & 2 deletions tests/Extractor/CsvEntityExtractorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ public function testExtractEntities()
$extractor = new CsvEntityExtractor();
$extractor->setFolderToExtract(__DIR__ . '/../fixtures/entity-csv');
$extractor
->addEntityToProcess('project', 'Smart\EtlBundle\Tests\Model\Project', function ($e) {
->addEntityToProcess('project', Project::class, function ($e) {
return $e->getCode();
})
->addEntityToProcess('task', 'Smart\EtlBundle\Tests\Model\Task', function ($e) {
->addEntityToProcess('task', Task::class, function ($e) {
return 'task' . $e->getProject()->getCode() . '-' . substr(md5($e->getName()), 0, 5);
})
;
Expand Down
28 changes: 26 additions & 2 deletions tests/Extractor/DoctrineEntityExtractorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use Doctrine\ORM\Tools\SchemaTool;
use Liip\FunctionalTestBundle\Test\WebTestCase;
use Smart\EtlBundle\Extractor\DoctrineEntityExtractor;
use Smart\EtlBundle\Tests\Entity\Project;
use Smart\EtlBundle\Tests\Entity\Task;

/**
* vendor/bin/phpunit tests/Extractor/DoctrineEntityExtractorTest.php
Expand All @@ -19,7 +21,7 @@ public function testExtractEntities()
//Initialise database
/** @var EntityManager $em */
$em = $this->getContainer()->get('doctrine')->getManager('default');
$metadatas = $em->getMetadataFactory()->getMetadataFor('Smart\EtlBundle\Tests\Entity\Project');
$metadatas = $em->getMetadataFactory()->getMetadataFor(Project::class);

$schemaTool = new SchemaTool($em);
$schemaTool->dropDatabase();
Expand All @@ -28,11 +30,12 @@ public function testExtractEntities()
$this->loadFixtureFiles([
__DIR__ . '/../fixtures/doctrine-loader/organisation.yml',
__DIR__ . '/../fixtures/doctrine-loader/project.yml',
__DIR__ . '/../fixtures/doctrine-loader/tag.yml',
__DIR__ . '/../fixtures/doctrine-loader/task.yml',
]);

$extractor = new DoctrineEntityExtractor($em);
$extractor->setEntityToExtract('Smart\EtlBundle\Tests\Entity\Project', ['organisation', 'name']);
$extractor->setEntityToExtract(Project::class, ['organisation', 'name']);
$qbExtractor = $extractor->getQueryBuilder();
//We agree that you should not make where like query if you want reasonable performance
$qbExtractor->andWhere(
Expand All @@ -46,5 +49,26 @@ public function testExtractEntities()
$this->assertEquals([
'etl-bundle' => ['organisation' => '@smartbooster', 'name' => 'ETL Bundle']
], $entities);


$extractor->setEntityToExtract(Task::class, ['code', 'project', 'name', 'tags']);
$entities = $extractor->extract();

$this->assertEquals(2, count($entities));

$this->assertEquals([
'etl-bundle-setup' => [
'code' => 'etl-bundle-setup',
'project' => '@etl-bundle',
'name' => 'Bundle setup',
'tags' => ['@doing', '@easy']
],
'etl-bundle-loadyml' => [
'code' => 'etl-bundle-loadyml',
'project' => '@etl-bundle',
'name' => 'Load yml entity file into database',
'tags' => ['@todo', '@hard']
]
], $entities);
}
}

0 comments on commit 5a7809e

Please # to comment.