Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move duplicate fixture into dedicated file #9789

Merged
merged 1 commit into from
May 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
]
);
}
}