From 3b79951824fbbfdaf1ff45560c5a63ae5e0c9d0c Mon Sep 17 00:00:00 2001 From: "Fabio B. Silva" Date: Sun, 26 Feb 2012 21:10:19 -0300 Subject: [PATCH] mapping driver tests --- .../ORM/Mapping/Driver/AnnotationDriver.php | 2 +- .../Doctrine/Tests/Models/CMS/CmsAddress.php | 26 +++++++ .../ORM/Mapping/AbstractMappingDriverTest.php | 77 +++++++++++++++++++ 3 files changed, 104 insertions(+), 1 deletion(-) diff --git a/lib/Doctrine/ORM/Mapping/Driver/AnnotationDriver.php b/lib/Doctrine/ORM/Mapping/Driver/AnnotationDriver.php index 9040b2103..1e6cab682 100644 --- a/lib/Doctrine/ORM/Mapping/Driver/AnnotationDriver.php +++ b/lib/Doctrine/ORM/Mapping/Driver/AnnotationDriver.php @@ -244,7 +244,7 @@ class AnnotationDriver implements Driver $columns = array(); foreach ($resultSetMapping->columns as $columnResultAnnot) { $columns[] = array( - 'name' => $resultSetMapping->name, + 'name' => $columnResultAnnot->name, ); } diff --git a/tests/Doctrine/Tests/Models/CMS/CmsAddress.php b/tests/Doctrine/Tests/Models/CMS/CmsAddress.php index 48084cc86..0b4f1ad9a 100644 --- a/tests/Doctrine/Tests/Models/CMS/CmsAddress.php +++ b/tests/Doctrine/Tests/Models/CMS/CmsAddress.php @@ -14,6 +14,16 @@ namespace Doctrine\Tests\Models\CMS; * name = "find-all", * resultSetMapping = "mapping-find-all", * query = "SELECT id, country, city FROM cms_addresses" + * ), + * @NamedNativeQuery( + * name = "find-by-id", + * resultClass = "CmsAddress", + * query = "SELECT * FROM cms_addresses WHERE id = ?" + * ), + * @NamedNativeQuery( + * name = "count", + * resultSetMapping= "mapping-count", + * query = "SELECT COUNT(*) AS count FROM cms_addresses" * ) * }) * @@ -30,6 +40,22 @@ namespace Doctrine\Tests\Models\CMS; * } * ) * } + * ), + * @SqlResultSetMapping( + * name = "mapping-without-fields", + * entities= { + * @EntityResult( + * entityClass = "__CLASS__" + * ) + * } + * ), + * @SqlResultSetMapping( + * name = "mapping-count", + * columns = { + * @ColumnResult( + * name = "count" + * ) + * } * ) * }) * diff --git a/tests/Doctrine/Tests/ORM/Mapping/AbstractMappingDriverTest.php b/tests/Doctrine/Tests/ORM/Mapping/AbstractMappingDriverTest.php index 3233c3104..0d0db044a 100644 --- a/tests/Doctrine/Tests/ORM/Mapping/AbstractMappingDriverTest.php +++ b/tests/Doctrine/Tests/ORM/Mapping/AbstractMappingDriverTest.php @@ -467,6 +467,7 @@ abstract class AbstractMappingDriverTest extends \Doctrine\Tests\OrmTestCase } /** +<<<<<<< HEAD * @group DDC-889 * @expectedException Doctrine\ORM\Mapping\MappingException * @expectedExceptionMessage Class "Doctrine\Tests\Models\DDC889\DDC889Class" sub class of "Doctrine\Tests\Models\DDC889\DDC889SuperClass" is not a valid entity or mapped super class. @@ -488,6 +489,82 @@ abstract class AbstractMappingDriverTest extends \Doctrine\Tests\OrmTestCase $factory->getMetadataFor('Doctrine\Tests\Models\DDC889\DDC889Entity'); } + + /** + * @group DDC-1663 + */ + public function testNamedNativeQuery() + { + if (!$this instanceof AnnotationDriverTest) { + $this->markTestIncomplete(); + } + + $class = $this->createClassMetadata('Doctrine\Tests\Models\CMS\CmsAddress'); + + $this->assertCount(3, $class->namedNativeQueries); + $this->assertArrayHasKey('find-all', $class->namedNativeQueries); + $this->assertArrayHasKey('find-by-id', $class->namedNativeQueries); + + $findAllQuery = $class->namedNativeQueries['find-all']; + $this->assertEquals('find-all', $findAllQuery['name']); + $this->assertEquals('mapping-find-all', $findAllQuery['resultSetMapping']); + $this->assertEquals('SELECT id, country, city FROM cms_addresses', $findAllQuery['query']); + + $findByIdQuery = $class->namedNativeQueries['find-by-id']; + $this->assertEquals('find-by-id', $findByIdQuery['name']); + $this->assertEquals('Doctrine\Tests\Models\CMS\CmsAddress',$findByIdQuery['resultClass']); + $this->assertEquals('SELECT * FROM cms_addresses WHERE id = ?', $findByIdQuery['query']); + + $countQuery = $class->namedNativeQueries['count']; + $this->assertEquals('count', $countQuery['name']); + $this->assertEquals('mapping-count', $countQuery['resultSetMapping']); + $this->assertEquals('SELECT COUNT(*) AS count FROM cms_addresses', $countQuery['query']); + + } + + /** + * @group DDC-1663 + */ + public function testSqlResultSetMapping() + { + if (!$this instanceof AnnotationDriverTest) { + $this->markTestIncomplete(); + } + + $class = $this->createClassMetadata('Doctrine\Tests\Models\CMS\CmsAddress'); + + $this->assertCount(3, $class->sqlResultSetMappings); + $this->assertArrayHasKey('mapping-find-all', $class->sqlResultSetMappings); + $this->assertArrayHasKey('mapping-without-fields', $class->sqlResultSetMappings); + + // empty fields select all fields + $withoutFieldsMapping = $class->sqlResultSetMappings['mapping-without-fields']; + $this->assertEquals('mapping-without-fields', $withoutFieldsMapping['name']); + $this->assertCount(1, $withoutFieldsMapping['entities']); + $this->assertCount(0, $withoutFieldsMapping['columns']); + $this->assertEquals(array(), $withoutFieldsMapping['entities'][0]['fields']); + $this->assertEquals('Doctrine\Tests\Models\CMS\CmsAddress', $withoutFieldsMapping['entities'][0]['entityClass']); + + + $findAllMapping = $class->sqlResultSetMappings['mapping-find-all']; + $this->assertEquals('mapping-find-all', $findAllMapping['name']); + $this->assertCount(1, $findAllMapping['entities']); + $this->assertCount(0, $findAllMapping['columns']); + $this->assertEquals(array( + array('name'=>'id','column'=>'id'), + array('name'=>'city','column'=>'city'), + array('name'=>'country','column' => 'country') + ), $findAllMapping['entities'][0]['fields']); + $this->assertEquals('Doctrine\Tests\Models\CMS\CmsAddress', $findAllMapping['entities'][0]['entityClass']); + + + $countMapping = $class->sqlResultSetMappings['mapping-count']; + $this->assertEquals('mapping-count', $countMapping['name']); + $this->assertCount(0, $countMapping['entities']); + $this->assertCount(1, $countMapping['columns']); + $this->assertEquals(array(array('name'=>'count')),$countMapping['columns']); + + } } /**