Skip to content

Commit

Permalink
Merge branch '3.2.x' into 4.0.x
Browse files Browse the repository at this point in the history
* 3.2.x:
  Fix failed merge (#11464)
  Test with actual lock modes (#11465)
  Backport test for Query::setLockMode() (#11463)
  Fix return type of Query::getLockMode() (#11462)
  • Loading branch information
derrabus committed May 21, 2024
2 parents 3695d57 + 22b1f52 commit b2a09b9
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 11 deletions.
5 changes: 3 additions & 2 deletions src/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -635,9 +635,10 @@ public function setLockMode(LockMode|int $lockMode): self
/**
* Get the current lock mode for this query.
*
* @return int|null The current lock mode of this query or NULL if no specific lock mode is set.
* @return LockMode|int|null The current lock mode of this query or NULL if no specific lock mode is set.
* @psalm-return LockMode::*|null
*/
public function getLockMode(): int|null
public function getLockMode(): LockMode|int|null
{
$lockMode = $this->getHint(self::HINT_LOCK_MODE);

Expand Down
2 changes: 1 addition & 1 deletion src/UnitOfWork.php
Original file line number Diff line number Diff line change
Expand Up @@ -2653,7 +2653,7 @@ private function eagerLoadCollections(array $collections, ToManyInverseSideMappi
$entities[] = $collection->getOwner();
}

$found = $this->getEntityPersister($targetEntity)->loadAll([$mappedBy => $entities], $mapping['orderBy'] ?? null);
$found = $this->getEntityPersister($targetEntity)->loadAll([$mappedBy => $entities], $mapping->orderBy);

$targetClass = $this->em->getClassMetadata($targetEntity);
$targetProperty = $targetClass->getReflectionProperty($mappedBy);
Expand Down
21 changes: 15 additions & 6 deletions tests/Tests/ORM/Cache/Persister/Entity/EntityPersisterTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Criteria;
use Doctrine\DBAL\LockMode;
use Doctrine\ORM\Cache\Persister\CachedPersister;
use Doctrine\ORM\Cache\Persister\Entity\AbstractEntityPersister;
use Doctrine\ORM\Cache\Persister\Entity\CachedEntityPersister;
Expand Down Expand Up @@ -97,7 +98,7 @@ public function testInvokeGetSelectSQL(): void
->with(
self::identicalTo(['name' => 'Foo']),
self::identicalTo($associationMapping),
self::identicalTo(1),
self::identicalTo(LockMode::OPTIMISTIC),
self::identicalTo(2),
self::identicalTo(3),
self::identicalTo([4]),
Expand All @@ -107,7 +108,7 @@ public function testInvokeGetSelectSQL(): void
self::assertSame('SELECT * FROM foo WERE name = ?', $persister->getSelectSQL(
['name' => 'Foo'],
$associationMapping,
1,
LockMode::OPTIMISTIC,
2,
3,
[4],
Expand Down Expand Up @@ -233,13 +234,21 @@ public function testInvokeLoad(): void
self::identicalTo($entity),
self::identicalTo($associationMapping),
self::identicalTo([1]),
self::identicalTo(2),
self::identicalTo(LockMode::PESSIMISTIC_READ),
self::identicalTo(3),
self::identicalTo([4]),
)
->willReturn($entity);

self::assertSame($entity, $persister->load(['id' => 1], $entity, $associationMapping, [1], 2, 3, [4]));
self::assertSame($entity, $persister->load(
['id' => 1],
$entity,
$associationMapping,
[1],
LockMode::PESSIMISTIC_READ,
3,
[4],
));
}

public function testInvokeLoadAll(): void
Expand Down Expand Up @@ -402,9 +411,9 @@ public function testInvokeLock(): void

$this->entityPersister->expects(self::once())
->method('lock')
->with(self::identicalTo($identifier), self::identicalTo(1));
->with(self::identicalTo($identifier), self::identicalTo(LockMode::OPTIMISTIC));

$persister->lock($identifier, 1);
$persister->lock($identifier, LockMode::OPTIMISTIC);
}

public function testInvokeExists(): void
Expand Down
30 changes: 28 additions & 2 deletions tests/Tests/ORM/Query/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver;
use Doctrine\DBAL\Driver\Result;
use Doctrine\DBAL\LockMode;
use Doctrine\DBAL\ParameterType;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Query;
use Doctrine\ORM\Query\Parameter;
use Doctrine\ORM\Query\QueryException;
use Doctrine\ORM\UnitOfWork;
Expand All @@ -39,8 +42,7 @@

class QueryTest extends OrmTestCase
{
/** @var EntityManagerMock */
protected $entityManager;
private EntityManagerMock $entityManager;

protected function setUp(): void
{
Expand Down Expand Up @@ -80,6 +82,30 @@ public function testSetParameters(): void
self::assertEquals($parameters, $query->getParameters());
}

/** @psalm-param LockMode::* $lockMode */
#[DataProvider('provideLockModes')]
public function testSetLockMode(LockMode|int $lockMode): void
{
$query = $this->entityManager->wrapInTransaction(static function (EntityManagerInterface $em) use ($lockMode): Query {
$query = $em->createQuery('select u from Doctrine\Tests\Models\CMS\CmsUser u where u.username = ?1');
$query->setLockMode($lockMode);

return $query;
});

self::assertSame($lockMode, $query->getLockMode());
self::assertSame($lockMode, $query->getHint(Query::HINT_LOCK_MODE));
}

/** @psalm-return list<array{LockMode::*}> */
public static function provideLockModes(): array
{
return [
[LockMode::PESSIMISTIC_READ],
[LockMode::PESSIMISTIC_WRITE],
];
}

public function testFree(): void
{
$query = $this->entityManager->createQuery('select u from Doctrine\Tests\Models\CMS\CmsUser u where u.username = ?1');
Expand Down

0 comments on commit b2a09b9

Please sign in to comment.