From aa1454562c8cba01c3c45b496a7da84870567ad0 Mon Sep 17 00:00:00 2001 From: Paulo de Freitas Date: Wed, 3 Dec 2014 19:51:51 -0200 Subject: [PATCH 1/3] Fix sequence-generator in MetaData exporter for XML Driver. --- .../ORM/Tools/Export/Driver/XmlExporter.php | 6 ++++ .../Export/XmlClassMetadataExporterTest.php | 32 +++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/lib/Doctrine/ORM/Tools/Export/Driver/XmlExporter.php b/lib/Doctrine/ORM/Tools/Export/Driver/XmlExporter.php index 14ff8322d..29bb57bac 100644 --- a/lib/Doctrine/ORM/Tools/Export/Driver/XmlExporter.php +++ b/lib/Doctrine/ORM/Tools/Export/Driver/XmlExporter.php @@ -171,6 +171,12 @@ class XmlExporter extends AbstractExporter if ($idGeneratorType = $this->_getIdGeneratorTypeString($metadata->generatorType)) { $generatorXml = $idXml->addChild('generator'); $generatorXml->addAttribute('strategy', $idGeneratorType); + if($metadata->generatorType === ClassMetadataInfo::GENERATOR_TYPE_SEQUENCE && $metadata->sequenceGeneratorDefinition) { + $sequenceGeneratorXml = $idXml->addChild('sequence-generator'); + $sequenceGeneratorXml->addAttribute('sequence-name', $metadata->sequenceGeneratorDefinition['sequenceName']); + $sequenceGeneratorXml->addAttribute('allocation-size', $metadata->sequenceGeneratorDefinition['allocationSize']); + $sequenceGeneratorXml->addAttribute('initial-value', $metadata->sequenceGeneratorDefinition['initialValue']); + } } } } diff --git a/tests/Doctrine/Tests/ORM/Tools/Export/XmlClassMetadataExporterTest.php b/tests/Doctrine/Tests/ORM/Tools/Export/XmlClassMetadataExporterTest.php index e5392ddcc..a5e44dccb 100644 --- a/tests/Doctrine/Tests/ORM/Tools/Export/XmlClassMetadataExporterTest.php +++ b/tests/Doctrine/Tests/ORM/Tools/Export/XmlClassMetadataExporterTest.php @@ -37,4 +37,36 @@ class XmlClassMetadataExporterTest extends AbstractClassMetadataExporterTest { return 'xml'; } + + + public function testSequenceGenerator() { + $exporter = new \Doctrine\ORM\Tools\Export\Driver\XmlExporter(); + $metadata = new \Doctrine\ORM\Mapping\ClassMetadata('entityTest'); + $metadata->mapField(array( + "fieldName" => 'id', + "type" => 'integer', + "columnName" => 'id', + "id" => true, + )); + + $metadata->setIdGeneratorType(\Doctrine\ORM\Mapping\ClassMetadataInfo::GENERATOR_TYPE_SEQUENCE); + $metadata->setSequenceGeneratorDefinition(array( + 'sequenceName' => 'seq_entity_test_id', + 'allocationSize' => 5, + 'initialValue' => 1 + )); + + + $fileContent = ''."\n". +''."\n". +' '."\n". +' '."\n". +' '."\n". +' '."\n". +' '."\n". +' '."\n". +''."\n"; + $exportClassMetadata = $exporter->exportClassMetadata($metadata); + $this->assertEquals($fileContent, $exportClassMetadata); + } } From ce308dc47647929d8ebcd52539fb97ea68a1110e Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Thu, 4 Dec 2014 13:21:10 +0100 Subject: [PATCH 2/3] #1204 - refactored sequence-generator metadata exporting into own private method for simplicity --- .../ORM/Tools/Export/Driver/XmlExporter.php | 31 +++++++++++++++---- 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/lib/Doctrine/ORM/Tools/Export/Driver/XmlExporter.php b/lib/Doctrine/ORM/Tools/Export/Driver/XmlExporter.php index 29bb57bac..1d539c9f2 100644 --- a/lib/Doctrine/ORM/Tools/Export/Driver/XmlExporter.php +++ b/lib/Doctrine/ORM/Tools/Export/Driver/XmlExporter.php @@ -171,12 +171,8 @@ class XmlExporter extends AbstractExporter if ($idGeneratorType = $this->_getIdGeneratorTypeString($metadata->generatorType)) { $generatorXml = $idXml->addChild('generator'); $generatorXml->addAttribute('strategy', $idGeneratorType); - if($metadata->generatorType === ClassMetadataInfo::GENERATOR_TYPE_SEQUENCE && $metadata->sequenceGeneratorDefinition) { - $sequenceGeneratorXml = $idXml->addChild('sequence-generator'); - $sequenceGeneratorXml->addAttribute('sequence-name', $metadata->sequenceGeneratorDefinition['sequenceName']); - $sequenceGeneratorXml->addAttribute('allocation-size', $metadata->sequenceGeneratorDefinition['allocationSize']); - $sequenceGeneratorXml->addAttribute('initial-value', $metadata->sequenceGeneratorDefinition['initialValue']); - } + + $this->exportSequenceInformation($idXml, $metadata); } } } @@ -417,6 +413,29 @@ class XmlExporter extends AbstractExporter } } + /** + * Export sequence information (if available/configured) into the current identifier XML node + * + * @param \SimpleXMLElement $identifierXmlNode + * @param ClassMetadataInfo $metadata + * + * @return void + */ + private function exportSequenceInformation(\SimpleXMLElement $identifierXmlNode, ClassMetadataInfo $metadata) + { + $sequenceDefinition = $metadata->sequenceGeneratorDefinition; + + if (! ($metadata->generatorType === ClassMetadataInfo::GENERATOR_TYPE_SEQUENCE && $sequenceDefinition)) { + return; + } + + $sequenceGeneratorXml = $identifierXmlNode->addChild('sequence-generator'); + + $sequenceGeneratorXml->addAttribute('sequence-name', $sequenceDefinition['sequenceName']); + $sequenceGeneratorXml->addAttribute('allocation-size', $sequenceDefinition['allocationSize']); + $sequenceGeneratorXml->addAttribute('initial-value', $sequenceDefinition['initialValue']); + } + /** * @param \SimpleXMLElement $simpleXml * From c35f1310bc9a64973f64e7eaedccab888357028b Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Thu, 4 Dec 2014 13:29:09 +0100 Subject: [PATCH 3/3] #1204 - using specific XML assertions to build a less-fragile test (asserting on the entire string may cause failures if the XML structure changes) --- .../Export/XmlClassMetadataExporterTest.php | 41 ++++++++++++------- 1 file changed, 26 insertions(+), 15 deletions(-) diff --git a/tests/Doctrine/Tests/ORM/Tools/Export/XmlClassMetadataExporterTest.php b/tests/Doctrine/Tests/ORM/Tools/Export/XmlClassMetadataExporterTest.php index a5e44dccb..0fe58b4ec 100644 --- a/tests/Doctrine/Tests/ORM/Tools/Export/XmlClassMetadataExporterTest.php +++ b/tests/Doctrine/Tests/ORM/Tools/Export/XmlClassMetadataExporterTest.php @@ -20,6 +20,9 @@ */ namespace Doctrine\Tests\ORM\Tools\Export; +use Doctrine\ORM\Mapping\ClassMetadataInfo; +use Doctrine\ORM\Mapping\ClassMetadata; +use Doctrine\ORM\Tools\Export\Driver\XmlExporter; /** * Test case for XmlClassMetadataExporterTest @@ -38,10 +41,13 @@ class XmlClassMetadataExporterTest extends AbstractClassMetadataExporterTest return 'xml'; } - + /** + * @group DDC-3428 + */ public function testSequenceGenerator() { - $exporter = new \Doctrine\ORM\Tools\Export\Driver\XmlExporter(); - $metadata = new \Doctrine\ORM\Mapping\ClassMetadata('entityTest'); + $exporter = new XmlExporter(); + $metadata = new ClassMetadata('entityTest'); + $metadata->mapField(array( "fieldName" => 'id', "type" => 'integer', @@ -49,24 +55,29 @@ class XmlClassMetadataExporterTest extends AbstractClassMetadataExporterTest "id" => true, )); - $metadata->setIdGeneratorType(\Doctrine\ORM\Mapping\ClassMetadataInfo::GENERATOR_TYPE_SEQUENCE); + $metadata->setIdGeneratorType(ClassMetadataInfo::GENERATOR_TYPE_SEQUENCE); $metadata->setSequenceGeneratorDefinition(array( 'sequenceName' => 'seq_entity_test_id', 'allocationSize' => 5, 'initialValue' => 1 )); + $expectedFileContent = <<<'XML' + + + + + + + + + +XML; - $fileContent = ''."\n". -''."\n". -' '."\n". -' '."\n". -' '."\n". -' '."\n". -' '."\n". -' '."\n". -''."\n"; - $exportClassMetadata = $exporter->exportClassMetadata($metadata); - $this->assertEquals($fileContent, $exportClassMetadata); + $this->assertXmlStringEqualsXmlString($expectedFileContent, $exporter->exportClassMetadata($metadata)); } }