Skip to content

Commit

Permalink
fixup! Add test to characterize previous behaviour and avoid regression
Browse files Browse the repository at this point in the history
  • Loading branch information
alquerci committed Apr 16, 2024
1 parent 7f0cf6c commit 606ef02
Showing 1 changed file with 55 additions and 91 deletions.
146 changes: 55 additions & 91 deletions tests/Ticket/GH134TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
}
}

0 comments on commit 606ef02

Please sign in to comment.