forked from doctrine/doctrine1
-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixup! Add test to characterize previous behaviour and avoid regression
- Loading branch information
Showing
1 changed file
with
55 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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' => '[email protected]', | ||
'Email' => array ( | ||
'id' => 1, | ||
'aliasAddress' => '[email protected]', | ||
), | ||
); | ||
|
||
$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' => '[email protected]', | ||
); | ||
|
||
$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' => '[email protected]', | ||
); | ||
|
||
$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' => '[email protected]', | ||
'Email' => array ( | ||
'id' => 1, | ||
'aliasAddress' => '[email protected]', | ||
), | ||
), | ||
]; | ||
|
||
$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' => '[email protected]', | ||
), | ||
]; | ||
|
||
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' => '[email protected]', | ||
), | ||
]; | ||
} | ||
|
||
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']; | ||
} | ||
} |