Skip to content
Open
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
7 changes: 7 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ parameters:
- tests/*
-
message: "#^Cannot call method warning\\(\\) on Psr\\\\Log\\\\LoggerInterface\\|null\\.$#"
-
# ObjectManager::find() declares 2 parameters, but the runtime EntityManager
# supports $lockMode and $lockVersion, forwarded like Doctrine\ORM\Decorator\EntityManagerDecorator does
message: '#^Method Doctrine\\Persistence\\ObjectManager\:\:find\(\) invoked with 4 parameters, 2 required\.$#'
identifier: arguments.count
count: 1
path: src/lib/Persistence/Doctrine/SiteAccessAwareEntityManager.php
paths:
- src
- tests
Expand Down
17 changes: 15 additions & 2 deletions src/lib/Persistence/Doctrine/SiteAccessAwareEntityManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,9 +226,22 @@ public function getCache(): ?Cache
return $this->getWrapped()->getCache();
}

public function find($className, $id): ?object
/**
* @template T of object
*
* @phpstan-param class-string<T> $className
* @phpstan-param \Doctrine\DBAL\LockMode::*|null $lockMode
*
* @param string $className
* @param mixed $id
* @param int|null $lockMode
* @param int|null $lockVersion
*
* @return T|null
*/
public function find($className, $id, ?int $lockMode = null, ?int $lockVersion = null): ?object
{
return $this->getWrapped()->find($className, $id);
return $this->getWrapped()->find($className, $id, $lockMode, $lockVersion);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Tests\Core\Persistence\Doctrine;

use Doctrine\DBAL\LockMode;
use Doctrine\ORM\EntityManagerInterface;
use Ibexa\Bundle\Core\Entity\EntityManagerFactory;
use Ibexa\Core\Persistence\Doctrine\SiteAccessAwareEntityManager;
use PHPUnit\Framework\TestCase;
use stdClass;

final class SiteAccessAwareEntityManagerTest extends TestCase
{
/** @var \Doctrine\ORM\EntityManagerInterface&\PHPUnit\Framework\MockObject\MockObject */
private EntityManagerInterface $wrappedEntityManager;

private SiteAccessAwareEntityManager $entityManager;

protected function setUp(): void
{
$this->wrappedEntityManager = $this->createMock(EntityManagerInterface::class);

$entityManagerFactory = $this->createMock(EntityManagerFactory::class);
$entityManagerFactory
->method('getEntityManager')
->willReturn($this->wrappedEntityManager);

$this->entityManager = new SiteAccessAwareEntityManager($entityManagerFactory);
}

public function testFindForwardsLockModeAndLockVersion(): void
{
$entity = new stdClass();

$this->wrappedEntityManager
->expects(self::once())
->method('find')
->with(stdClass::class, 1, LockMode::PESSIMISTIC_WRITE, 2)
->willReturn($entity);

self::assertSame(
$entity,
$this->entityManager->find(stdClass::class, 1, LockMode::PESSIMISTIC_WRITE, 2)
);
}

public function testFindWithoutLockArgumentsForwardsNullDefaults(): void
{
$entity = new stdClass();

$this->wrappedEntityManager
->expects(self::once())
->method('find')
->with(stdClass::class, 1, null, null)
->willReturn($entity);

self::assertSame(
$entity,
$this->entityManager->find(stdClass::class, 1)
);
}
}