diff --git a/tests/Ticket/GH134TestCase.php b/tests/Ticket/GH134TestCase.php index 21eff8f7..10c250ea 100644 --- a/tests/Ticket/GH134TestCase.php +++ b/tests/Ticket/GH134TestCase.php @@ -2,112 +2,76 @@ class Doctrine_Ticket_GH134_TestCase extends Doctrine_UnitTestCase { - public function test_hydrateArray_identifierOfRelations() + public function test_identifierAndRelation() { - $query = Doctrine_Query::create() - ->select('u.id, e.address as aliasAddress') - ->from('User u') - ->innerJoin('u.Email e') - ; + foreach ($this->provideIdentifierAndRelationData() as [$hydrateType, $expectedSql, $expectedFirstResult]) { + $query = Doctrine_Query::create() + ->select('u.id, e.address as aliasAddress') + ->from('User u') + ->innerJoin('u.Email e') + ; - $results = $query->execute(array(), Doctrine_Core::HYDRATE_ARRAY); + $results = $query->execute(array(), $hydrateType); - $expectedSql = 'SELECT e.id AS e__id, e2.id AS e2__id, e2.address AS e2__0 FROM entity e INNER JOIN email e2 ON e.email_id = e2.id WHERE (e.type = 0)'; + $this->assertEqual($expectedSql, $query->getSqlQuery()); - $this->assertEqual($expectedSql, $query->getSqlQuery()); - - $expected = array ( - 'id' => '4', - 'aliasAddress' => 'zYne@example.com', - 'Email' => array ( - 'id' => 1, - 'aliasAddress' => 'zYne@example.com', - ), - ); - - $this->assertEqual($expected, $results[0]); - $this->assertEqual(count($this->users), count($results)); - } - - public function test_hydrateArrayShallow_identifierOfRelations() - { - $query = Doctrine_Query::create() - ->select('u.id, e.address as aliasAddress') - ->from('User u') - ->innerJoin('u.Email e') - ; - - $results = $query->execute(array(), Doctrine_Core::HYDRATE_ARRAY_SHALLOW); - - $expectedSql = 'SELECT e.id AS e__id, e2.address AS e2__0 FROM entity e INNER JOIN email e2 ON e.email_id = e2.id WHERE (e.type = 0)'; - - $this->assertEqual($expectedSql, $query->getSqlQuery()); - - $expected = array ( - 'id' => '4', - 'aliasAddress' => 'zYne@example.com', - ); - - $this->assertEqual($expected, $results[0]); - $this->assertEqual(count($this->users), count($results)); - } - - public function test_hydrateScalar_identifierOfRelations() - { - $query = Doctrine_Query::create() - ->select('u.id, e.address as aliasAddress') - ->from('User u') - ->innerJoin('u.Email e') - ; - - $results = $query->execute(array(), Doctrine_Core::HYDRATE_SCALAR); - - $expectedSql = 'SELECT e.id AS e__id, e2.address AS e2__0 FROM entity e INNER JOIN email e2 ON e.email_id = e2.id WHERE (e.type = 0)'; - - $this->assertEqual($expectedSql, $query->getSqlQuery()); - - $expected = array ( - 'u_id' => '4', - 'e_aliasAddress' => 'zYne@example.com', - ); - - $this->assertEqual($expected, $results[0]); - $this->assertEqual(count($this->users), count($results)); + $this->assertEqual($expectedFirstResult, $results[0]); + $this->assertEqual(count($this->users), count($results)); + } } - private function doTestAliasWithSameNameAsIdentifiers($hydrateType, $checkArrayIndex) + private function provideIdentifierAndRelationData() { - $query = Doctrine_Query::create() - ->select('99 as id, u.id as aliasId') - ->from('User u') - ; - - $results = $query->execute(array(), $hydrateType); + yield [ + Doctrine_Core::HYDRATE_ARRAY, + 'SELECT e.id AS e__id, e2.id AS e2__id, e2.address AS e2__0 FROM entity e INNER JOIN email e2 ON e.email_id = e2.id WHERE (e.type = 0)', + array ( + 'id' => '4', + 'aliasAddress' => 'zYne@example.com', + 'Email' => array ( + 'id' => 1, + 'aliasAddress' => 'zYne@example.com', + ), + ), + ]; - $this->assertEqual(99, $results[0][$checkArrayIndex]); + yield [ + Doctrine_Core::HYDRATE_ARRAY_SHALLOW, + 'SELECT e.id AS e__id, e2.address AS e2__0 FROM entity e INNER JOIN email e2 ON e.email_id = e2.id WHERE (e.type = 0)', + array ( + 'id' => '4', + 'aliasAddress' => 'zYne@example.com', + ), + ]; + + yield [ + Doctrine_Core::HYDRATE_SCALAR, + 'SELECT e.id AS e__id, e2.address AS e2__0 FROM entity e INNER JOIN email e2 ON e.email_id = e2.id WHERE (e.type = 0)', + array ( + 'u_id' => '4', + 'e_aliasAddress' => 'zYne@example.com', + ), + ]; } - public function test_hydrateScalar_aliasWithSameNameAsIdentifiers_willKeepOverwriteIdentifierByAlias() + public function test_columnAliasWithSameNameAsIdentifier_willKeepOverwriteIdentifierByAlias() { - $hydrateType = Doctrine_Core::HYDRATE_SCALAR; - $checkArrayIndex = 'u_id'; + foreach ($this->provideKeepOverwriteIdentifierWithAliasData() as [$hydrateType, $checkArrayIndex]) { + $query = Doctrine_Query::create() + ->select('99 as id, u.id as aliasId') + ->from('User u') + ; - $this->doTestAliasWithSameNameAsIdentifiers($hydrateType, $checkArrayIndex); - } + $results = $query->execute(array(), $hydrateType); - public function test_hydrateArrayShallow_aliasWithSameNameAsIdentifiers_willKeepOverwriteIdentifierByAlias() - { - $hydrateType = Doctrine_Core::HYDRATE_ARRAY_SHALLOW; - $checkArrayIndex = 'id'; - - $this->doTestAliasWithSameNameAsIdentifiers($hydrateType, $checkArrayIndex); + $this->assertEqual(99, $results[0][$checkArrayIndex]); + } } - public function test_hydrateArray_aliasWithSameNameAsIdentifiers_willKeepOverwriteIdentifierByAlias() + private function provideKeepOverwriteIdentifierWithAliasData() { - $hydrateType = Doctrine_Core::HYDRATE_ARRAY; - $checkArrayIndex = 'id'; - - $this->doTestAliasWithSameNameAsIdentifiers($hydrateType, $checkArrayIndex); + yield [Doctrine_Core::HYDRATE_SCALAR, 'u_id']; + yield [Doctrine_Core::HYDRATE_ARRAY, 'id']; + yield [Doctrine_Core::HYDRATE_ARRAY_SHALLOW, 'id']; } }