Skip to content

Commit

Permalink
Move duplicate fixture into dedicated file
Browse files Browse the repository at this point in the history
  • Loading branch information
derrabus committed May 25, 2022
1 parent 0f6f752 commit a9af5d7
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 92 deletions.
59 changes: 59 additions & 0 deletions tests/Doctrine/Tests/Mocks/CustomTreeWalkerJoin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\Mocks;

use Doctrine\ORM\Query\AST\IdentificationVariableDeclaration;
use Doctrine\ORM\Query\AST\Join;
use Doctrine\ORM\Query\AST\JoinAssociationDeclaration;
use Doctrine\ORM\Query\AST\JoinAssociationPathExpression;
use Doctrine\ORM\Query\AST\SelectExpression;
use Doctrine\ORM\Query\AST\SelectStatement;
use Doctrine\ORM\Query\TreeWalkerAdapter;
use Doctrine\Tests\Models\CMS\CmsAddress;
use Doctrine\Tests\Models\CMS\CmsUser;

class CustomTreeWalkerJoin extends TreeWalkerAdapter
{
public function walkSelectStatement(SelectStatement $selectStatement): void
{
foreach ($selectStatement->fromClause->identificationVariableDeclarations as $identificationVariableDeclaration) {
$rangeVariableDecl = $identificationVariableDeclaration->rangeVariableDeclaration;

if ($rangeVariableDecl->abstractSchemaName !== CmsUser::class) {
continue;
}

$this->modifySelectStatement($selectStatement, $identificationVariableDeclaration);
}
}

private function modifySelectStatement(SelectStatement $selectStatement, IdentificationVariableDeclaration $identificationVariableDecl): void
{
$rangeVariableDecl = $identificationVariableDecl->rangeVariableDeclaration;
$joinAssocPathExpression = new JoinAssociationPathExpression($rangeVariableDecl->aliasIdentificationVariable, 'address');
$joinAssocDeclaration = new JoinAssociationDeclaration($joinAssocPathExpression, $rangeVariableDecl->aliasIdentificationVariable . 'a', null);
$join = new Join(Join::JOIN_TYPE_LEFT, $joinAssocDeclaration);
$selectExpression = new SelectExpression($rangeVariableDecl->aliasIdentificationVariable . 'a', null, false);

$identificationVariableDecl->joins[] = $join;
$selectStatement->selectClause->selectExpressions[] = $selectExpression;

$entityManager = $this->_getQuery()->getEntityManager();
$userMetadata = $entityManager->getClassMetadata(CmsUser::class);
$addressMetadata = $entityManager->getClassMetadata(CmsAddress::class);

$this->setQueryComponent(
$rangeVariableDecl->aliasIdentificationVariable . 'a',
[
'metadata' => $addressMetadata,
'parent' => $rangeVariableDecl->aliasIdentificationVariable,
'relation' => $userMetadata->getAssociationMapping('address'),
'map' => null,
'nestingLevel' => 0,
'token' => null,
]
);
}
}
47 changes: 1 addition & 46 deletions tests/Doctrine/Tests/ORM/Query/CustomTreeWalkersJoinTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@

use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Query;
use Doctrine\Tests\Models\CMS\CmsAddress;
use Doctrine\Tests\Models\CMS\CmsUser;
use Doctrine\Tests\Mocks\CustomTreeWalkerJoin;
use Doctrine\Tests\OrmTestCase;

/**
Expand Down Expand Up @@ -51,47 +50,3 @@ public function testDoesNotAddJoin(): void
);
}
}

class CustomTreeWalkerJoin extends Query\TreeWalkerAdapter
{
public function walkSelectStatement(Query\AST\SelectStatement $selectStatement): void
{
foreach ($selectStatement->fromClause->identificationVariableDeclarations as $identificationVariableDeclaration) {
$rangeVariableDecl = $identificationVariableDeclaration->rangeVariableDeclaration;

if ($rangeVariableDecl->abstractSchemaName !== CmsUser::class) {
continue;
}

$this->modifySelectStatement($selectStatement, $identificationVariableDeclaration);
}
}

private function modifySelectStatement(Query\AST\SelectStatement $selectStatement, $identificationVariableDecl): void
{
$rangeVariableDecl = $identificationVariableDecl->rangeVariableDeclaration;
$joinAssocPathExpression = new Query\AST\JoinAssociationPathExpression($rangeVariableDecl->aliasIdentificationVariable, 'address');
$joinAssocDeclaration = new Query\AST\JoinAssociationDeclaration($joinAssocPathExpression, $rangeVariableDecl->aliasIdentificationVariable . 'a', null);
$join = new Query\AST\Join(Query\AST\Join::JOIN_TYPE_LEFT, $joinAssocDeclaration);
$selectExpression = new Query\AST\SelectExpression($rangeVariableDecl->aliasIdentificationVariable . 'a', null, false);

$identificationVariableDecl->joins[] = $join;
$selectStatement->selectClause->selectExpressions[] = $selectExpression;

$entityManager = $this->_getQuery()->getEntityManager();
$userMetadata = $entityManager->getClassMetadata(CmsUser::class);
$addressMetadata = $entityManager->getClassMetadata(CmsAddress::class);

$this->setQueryComponent(
$rangeVariableDecl->aliasIdentificationVariable . 'a',
[
'metadata' => $addressMetadata,
'parent' => $rangeVariableDecl->aliasIdentificationVariable,
'relation' => $userMetadata->getAssociationMapping('address'),
'map' => null,
'nestingLevel' => 0,
'token' => null,
]
);
}
}
48 changes: 2 additions & 46 deletions tests/Doctrine/Tests/ORM/Query/CustomTreeWalkersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Functional;
namespace Doctrine\Tests\ORM\Query;

use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Query;
use Doctrine\ORM\Query\QueryException;
use Doctrine\ORM\Query\SqlWalker;
use Doctrine\ORM\Query\TreeWalker;
use Doctrine\Tests\Models\CMS\CmsAddress;
use Doctrine\Tests\Mocks\CustomTreeWalkerJoin;
use Doctrine\Tests\Models\CMS\CmsUser;
use Doctrine\Tests\OrmTestCase;

Expand Down Expand Up @@ -196,47 +196,3 @@ public function walkSelectStatement(Query\AST\SelectStatement $selectStatement):
}
}
}

class CustomTreeWalkerJoin extends Query\TreeWalkerAdapter
{
public function walkSelectStatement(Query\AST\SelectStatement $selectStatement): void
{
foreach ($selectStatement->fromClause->identificationVariableDeclarations as $identificationVariableDeclaration) {
$rangeVariableDecl = $identificationVariableDeclaration->rangeVariableDeclaration;

if ($rangeVariableDecl->abstractSchemaName !== CmsUser::class) {
continue;
}

$this->modifySelectStatement($selectStatement, $identificationVariableDeclaration);
}
}

private function modifySelectStatement(Query\AST\SelectStatement $selectStatement, $identificationVariableDecl): void
{
$rangeVariableDecl = $identificationVariableDecl->rangeVariableDeclaration;
$joinAssocPathExpression = new Query\AST\JoinAssociationPathExpression($rangeVariableDecl->aliasIdentificationVariable, 'address');
$joinAssocDeclaration = new Query\AST\JoinAssociationDeclaration($joinAssocPathExpression, $rangeVariableDecl->aliasIdentificationVariable . 'a', null);
$join = new Query\AST\Join(Query\AST\Join::JOIN_TYPE_LEFT, $joinAssocDeclaration);
$selectExpression = new Query\AST\SelectExpression($rangeVariableDecl->aliasIdentificationVariable . 'a', null, false);

$identificationVariableDecl->joins[] = $join;
$selectStatement->selectClause->selectExpressions[] = $selectExpression;

$entityManager = $this->_getQuery()->getEntityManager();
$userMetadata = $entityManager->getClassMetadata(CmsUser::class);
$addressMetadata = $entityManager->getClassMetadata(CmsAddress::class);

$this->setQueryComponent(
$rangeVariableDecl->aliasIdentificationVariable . 'a',
[
'metadata' => $addressMetadata,
'parent' => $rangeVariableDecl->aliasIdentificationVariable,
'relation' => $userMetadata->getAssociationMapping('address'),
'map' => null,
'nestingLevel' => 0,
'token' => null,
]
);
}
}

0 comments on commit a9af5d7

Please sign in to comment.