-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bug join sql when change sqlFilter parameters
- Loading branch information
Showing
4 changed files
with
202 additions
and
0 deletions.
There are no files selected for viewing
99 changes: 99 additions & 0 deletions
99
tests/Tests/ORM/Functional/Ticket/SwitchContextWithFilter/ChangeFiltersTest.php
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 |
---|---|---|
@@ -0,0 +1,99 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\Tests\ORM\Functional\Ticket\SwitchContextWithFilter; | ||
|
||
use Doctrine\Tests\OrmFunctionalTestCase; | ||
|
||
use function sprintf; | ||
use function str_replace; | ||
|
||
final class ChangeFiltersTest extends OrmFunctionalTestCase | ||
{ | ||
private const COMPANY_A = 'A'; | ||
private const COMPANY_B = 'B'; | ||
|
||
public function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->setUpEntitySchema([ | ||
Order::class, | ||
User::class, | ||
]); | ||
} | ||
|
||
/** | ||
* @return non-empty-array<"companyA"|"companyB", array{orderId: int, userId: int}> | ||
*/ | ||
private function prepareData(): array | ||
{ | ||
$user1 = new User(self::COMPANY_A); | ||
$order1 = new Order($user1); | ||
$user2 = new User(self::COMPANY_B); | ||
$order2 = new Order($user2); | ||
|
||
$this->_em->persist($user1); | ||
$this->_em->persist($order1); | ||
$this->_em->persist($user2); | ||
$this->_em->persist($order2); | ||
$this->_em->flush(); | ||
$this->_em->clear(); | ||
|
||
return [ | ||
'companyA' => ['orderId' => $order1->id, 'userId' => $user1->id], | ||
'companyB' => ['orderId' => $order2->id, 'userId' => $user2->id], | ||
]; | ||
} | ||
|
||
public function testUseEnableDisableFilter(): void | ||
{ | ||
$this->_em->getConfiguration()->addFilter(CompanySQLFilter::class, CompanySQLFilter::class); | ||
$this->_em->getFilters()->enable(CompanySQLFilter::class)->setParameter('company', self::COMPANY_A); | ||
|
||
['companyA' => $companyA, 'companyB' => $companyB] = $this->prepareData(); | ||
|
||
$order1 = $this->_em->find(Order::class, $companyA['orderId']); | ||
|
||
self::assertNotNull($order1->user, $this->generateMessage('Order1->User1 not found')); | ||
self::assertEquals($companyA['userId'], $order1->user->id, $this->generateMessage('Order1->User1 != User1')); | ||
|
||
$this->_em->getFilters()->disable(CompanySQLFilter::class); | ||
$this->_em->getFilters()->enable(CompanySQLFilter::class)->setParameter('company', self::COMPANY_B); | ||
|
||
$order2 = $this->_em->find(Order::class, $companyB['orderId']); | ||
|
||
self::assertNotNull($order2->user, $this->generateMessage('Order2->User2 not found')); | ||
self::assertEquals($companyB['userId'], $order2->user->id, $this->generateMessage('Order2->User2 != User2')); | ||
} | ||
|
||
public function testUseChangeFilterParameters(): void | ||
{ | ||
$this->_em->getConfiguration()->addFilter(CompanySQLFilter::class, CompanySQLFilter::class); | ||
$filter = $this->_em->getFilters()->enable(CompanySQLFilter::class); | ||
|
||
['companyA' => $companyA, 'companyB' => $companyB] = $this->prepareData(); | ||
|
||
$filter->setParameter('company', self::COMPANY_A); | ||
|
||
$order1 = $this->_em->find(Order::class, $companyA['orderId']); | ||
|
||
self::assertNotNull($order1->user, $this->generateMessage('Order1->User1 not found')); | ||
self::assertEquals($companyA['userId'], $order1->user->id, $this->generateMessage('Order1->User1 != User1')); | ||
|
||
$filter->setParameter('company', self::COMPANY_B); | ||
|
||
$order2 = $this->_em->find(Order::class, $companyB['orderId']); | ||
|
||
self::assertNotNull($order2->user, $this->generateMessage('Order2->User2 not found')); | ||
self::assertEquals($companyB['userId'], $order2->user->id, $this->generateMessage('Order2->User2 != User2')); | ||
} | ||
|
||
private function generateMessage(string $message): string | ||
{ | ||
$log = $this->getLastLoggedQuery(); | ||
|
||
return sprintf("%s\nSQL: %s", $message, str_replace(['?'], (array) $log['params'], $log['sql'])); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
tests/Tests/ORM/Functional/Ticket/SwitchContextWithFilter/CompanySQLFilter.php
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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\Tests\ORM\Functional\Ticket\SwitchContextWithFilter; | ||
|
||
use Doctrine\ORM\Mapping\ClassMetadata; | ||
use Doctrine\ORM\Query\Filter\SQLFilter; | ||
|
||
use function sprintf; | ||
|
||
class CompanySQLFilter extends SQLFilter | ||
{ | ||
public function addFilterConstraint(ClassMetadata $targetEntity, $targetTableAlias): string | ||
{ | ||
if ($targetEntity->getName() === User::class) { | ||
return sprintf('%s.%s = %s', $targetTableAlias, $targetEntity->fieldMappings['company']['fieldName'], $this->getParameter('company')); | ||
} | ||
|
||
if ($targetEntity->getName() === Order::class) { | ||
return sprintf('%s.%s = %s', $targetTableAlias, $targetEntity->fieldMappings['company']['fieldName'], $this->getParameter('company')); | ||
} | ||
|
||
return ''; | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
tests/Tests/ORM/Functional/Ticket/SwitchContextWithFilter/Order.php
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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\Tests\ORM\Functional\Ticket\SwitchContextWithFilter; | ||
|
||
use Doctrine\ORM\Mapping as ORM; | ||
|
||
/** | ||
* @ORM\Entity | ||
* @ORM\Table(name="Order_Master") | ||
*/ | ||
class Order | ||
{ | ||
/** | ||
* @ORM\Id | ||
* @ORM\Column(type="integer") | ||
* @ORM\GeneratedValue(strategy="AUTO") | ||
* | ||
* @var int | ||
*/ | ||
public $id; | ||
|
||
/** | ||
* @ORM\Column(type="string") | ||
* | ||
* @var string | ||
*/ | ||
public $company; | ||
|
||
/** | ||
* @ORM\ManyToOne(targetEntity="User", fetch="EAGER") | ||
* | ||
* @var User | ||
*/ | ||
public $user; | ||
|
||
public function __construct(User $user) | ||
{ | ||
$this->user = $user; | ||
$this->company = $user->company; | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
tests/Tests/ORM/Functional/Ticket/SwitchContextWithFilter/User.php
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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\Tests\ORM\Functional\Ticket\SwitchContextWithFilter; | ||
|
||
use Doctrine\ORM\Mapping as ORM; | ||
|
||
/** | ||
* @ORM\Entity | ||
*/ | ||
class User | ||
{ | ||
/** | ||
* @ORM\Id | ||
* @ORM\Column(type="integer") | ||
* @ORM\GeneratedValue(strategy="AUTO") | ||
* | ||
* @var int | ||
*/ | ||
public $id; | ||
|
||
/** | ||
* @ORM\Column(type="string") | ||
* | ||
* @var string | ||
*/ | ||
public $company; | ||
|
||
public function __construct(string $company) | ||
{ | ||
$this->company = $company; | ||
} | ||
} |