forked from doctrine/orm
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/2.20.x' into 3.3.x
- Loading branch information
Showing
12 changed files
with
361 additions
and
10 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
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
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
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
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
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
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
128 changes: 128 additions & 0 deletions
128
tests/Doctrine/Tests/ORM/Functional/Ticket/LazyEagerCollectionTest.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,128 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\Tests\ORM\Functional\Ticket; | ||
|
||
use Doctrine\Common\Collections\ArrayCollection; | ||
use Doctrine\Common\Collections\Collection; | ||
use Doctrine\ORM\Mapping as ORM; | ||
use Doctrine\ORM\Mapping\Column; | ||
use Doctrine\ORM\Mapping\Entity; | ||
use Doctrine\ORM\Mapping\GeneratedValue; | ||
use Doctrine\ORM\Mapping\Id; | ||
use Doctrine\Tests\OrmFunctionalTestCase; | ||
|
||
class LazyEagerCollectionTest extends OrmFunctionalTestCase | ||
{ | ||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->createSchemaForModels( | ||
LazyEagerCollectionUser::class, | ||
LazyEagerCollectionAddress::class, | ||
LazyEagerCollectionPhone::class, | ||
); | ||
} | ||
|
||
public function testRefreshRefreshesBothLazyAndEagerCollections(): void | ||
{ | ||
$user = new LazyEagerCollectionUser(); | ||
$user->data = 'Guilherme'; | ||
|
||
$ph = new LazyEagerCollectionPhone(); | ||
$ph->data = '12345'; | ||
$user->addPhone($ph); | ||
|
||
$ad = new LazyEagerCollectionAddress(); | ||
$ad->data = '6789'; | ||
$user->addAddress($ad); | ||
|
||
$this->_em->persist($user); | ||
$this->_em->persist($ad); | ||
$this->_em->persist($ph); | ||
$this->_em->flush(); | ||
$this->_em->clear(); | ||
|
||
$user = $this->_em->find(LazyEagerCollectionUser::class, $user->id); | ||
$ph = $user->phones[0]; | ||
$ad = $user->addresses[0]; | ||
|
||
$ph->data = 'abc'; | ||
$ad->data = 'def'; | ||
|
||
$this->_em->refresh($user); | ||
|
||
self::assertSame('12345', $ph->data); | ||
self::assertSame('6789', $ad->data); | ||
} | ||
} | ||
|
||
#[Entity] | ||
class LazyEagerCollectionUser | ||
{ | ||
#[Id] | ||
#[Column(type: 'integer')] | ||
#[GeneratedValue(strategy: 'AUTO')] | ||
public int $id; | ||
|
||
#[Column(type: 'string', length: 255)] | ||
public string $data; | ||
|
||
/** @var Collection<LazyEagerCollectionPhone> */ | ||
#[ORM\OneToMany(targetEntity: 'LazyEagerCollectionPhone', cascade: ['refresh'], fetch: 'EAGER', mappedBy: 'user')] | ||
public Collection $phones; | ||
|
||
/** @var Collection<LazyEagerCollectionAddress> */ | ||
#[ORM\OneToMany(targetEntity: 'LazyEagerCollectionAddress', cascade: ['refresh'], mappedBy: 'user')] | ||
public Collection $addresses; | ||
|
||
public function __construct() | ||
{ | ||
$this->addresses = new ArrayCollection(); | ||
$this->phones = new ArrayCollection(); | ||
} | ||
|
||
public function addPhone(LazyEagerCollectionPhone $phone): void | ||
{ | ||
$phone->user = $this; | ||
$this->phones[] = $phone; | ||
} | ||
|
||
public function addAddress(LazyEagerCollectionAddress $address): void | ||
{ | ||
$address->user = $this; | ||
$this->addresses[] = $address; | ||
} | ||
} | ||
|
||
#[Entity] | ||
class LazyEagerCollectionPhone | ||
{ | ||
#[Id] | ||
#[Column(type: 'integer')] | ||
#[GeneratedValue(strategy: 'AUTO')] | ||
public int $id; | ||
|
||
#[Column(type: 'string', length: 255)] | ||
public string $data; | ||
|
||
#[ORM\ManyToOne(targetEntity: 'LazyEagerCollectionUser', inversedBy: 'phones')] | ||
public LazyEagerCollectionUser $user; | ||
} | ||
|
||
#[Entity] | ||
class LazyEagerCollectionAddress | ||
{ | ||
#[Id] | ||
#[Column(type: 'integer')] | ||
#[GeneratedValue(strategy: 'AUTO')] | ||
public int $id; | ||
|
||
#[Column(type: 'string', length: 255)] | ||
public string $data; | ||
|
||
#[ORM\ManyToOne(targetEntity: 'LazyEagerCollectionUser', inversedBy: 'addresses')] | ||
public LazyEagerCollectionUser $user; | ||
} |
140 changes: 140 additions & 0 deletions
140
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,140 @@ | ||
<?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')); | ||
} | ||
|
||
public function testUseQueryBuilder(): void | ||
{ | ||
$this->_em->getConfiguration()->addFilter(CompanySQLFilter::class, CompanySQLFilter::class); | ||
$filter = $this->_em->getFilters()->enable(CompanySQLFilter::class); | ||
|
||
['companyA' => $companyA, 'companyB' => $companyB] = $this->prepareData(); | ||
|
||
$getOrderByIdCache = function (int $orderId): Order|null { | ||
return $this->_em->createQueryBuilder() | ||
->select('orderMaster, user') | ||
->from(Order::class, 'orderMaster') | ||
->innerJoin('orderMaster.user', 'user') | ||
->where('orderMaster.id = :orderId') | ||
->setParameter('orderId', $orderId) | ||
->setCacheable(true) | ||
->getQuery() | ||
->setQueryCacheLifetime(10) | ||
->getOneOrNullResult(); | ||
}; | ||
|
||
$filter->setParameter('company', self::COMPANY_A); | ||
|
||
$order = $getOrderByIdCache($companyB['orderId']); | ||
self::assertNull($order); | ||
|
||
$order = $getOrderByIdCache($companyA['orderId']); | ||
|
||
self::assertInstanceOf(Order::class, $order); | ||
self::assertInstanceOf(User::class, $order->user); | ||
self::assertEquals($companyA['userId'], $order->user->id); | ||
|
||
$filter->setParameter('company', self::COMPANY_B); | ||
|
||
$order = $getOrderByIdCache($companyA['orderId']); | ||
self::assertNull($order); | ||
|
||
$order = $getOrderByIdCache($companyB['orderId']); | ||
|
||
self::assertInstanceOf(Order::class, $order); | ||
self::assertInstanceOf(User::class, $order->user); | ||
self::assertEquals($companyB['userId'], $order->user->id); | ||
} | ||
|
||
private function generateMessage(string $message): string | ||
{ | ||
$log = $this->getLastLoggedQuery(); | ||
|
||
return sprintf("%s\nSQL: %s", $message, str_replace(['?'], (array) $log['params'], $log['sql'])); | ||
} | ||
} |
Oops, something went wrong.