Skip to content

Commit

Permalink
Merge pull request #44 from kbond/orm3
Browse files Browse the repository at this point in the history
Support ORM 3+, DBAL 4+, Pagerfanta 4+
  • Loading branch information
kbond authored Mar 27, 2024
2 parents c2f1b95 + 1521cf1 commit c3f6bea
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 25 deletions.
12 changes: 6 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
"require-dev": {
"composer-runtime-api": "^2.0",
"doctrine/collections": "^2.1",
"doctrine/dbal": "^2.12|^3.0",
"doctrine/doctrine-bundle": "^2.4",
"doctrine/orm": "^2.15",
"pagerfanta/pagerfanta": "^1.0|^2.0|^3.0",
"doctrine/dbal": "^3.0|^4.0",
"doctrine/doctrine-bundle": "^2.7",
"doctrine/orm": "^2.15|^3.0",
"pagerfanta/pagerfanta": "^1.0|^2.0|^3.0|^4.0",
"phpstan/phpstan": "^1.4",
"phpunit/phpunit": "^9.5.0",
"symfony/expression-language": "^6.4|^7.0",
Expand All @@ -31,8 +31,8 @@
"zenstruck/uri": "^2.3"
},
"suggest": {
"doctrine/orm": "To use ORM implementation and batch utilities (>=2.10).",
"doctrine/dbal": "To use DBAL implementation.",
"doctrine/orm": "To use ORM implementation and batch utilities (>=2.15).",
"doctrine/dbal": "To use DBAL implementation (>=3.0).",
"doctrine/collections": "To use CollectionDecorator.",
"pagerfanta/pagerfanta": "To use CollectionAdapter."
},
Expand Down
13 changes: 12 additions & 1 deletion src/Collection/Doctrine/ORM/EntityRepositoryBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Zenstruck\Collection\Doctrine\ORM;

use Doctrine\Common\Collections\Criteria;
use Doctrine\DBAL\LockMode;
use Doctrine\ORM\QueryBuilder;

/**
Expand All @@ -26,6 +27,8 @@ trait EntityRepositoryBridge

/**
* @param mixed|Criteria|array<string,mixed>|(object&callable(QueryBuilder,string):void)|object $specification
* @param LockMode|int|null $lockMode
* @param int|null $lockVersion
*/
public function find($specification, $lockMode = null, $lockVersion = null): ?object
{
Expand Down Expand Up @@ -68,11 +71,19 @@ public function count(array $criteria = []): int
}

/**
* @param string $alias
* @param string|null $indexBy
*
* @return EntityResultQueryBuilder<V>
*/
public function createQueryBuilder($alias, $indexBy = null): EntityResultQueryBuilder
{
return EntityResultQueryBuilder::forEntity($this->_em, $this->getClassName(), $alias, $indexBy);
return EntityResultQueryBuilder::forEntity(
parent::createQueryBuilder($alias, $indexBy)->getEntityManager(),
$this->getClassName(),
$alias,
$indexBy
);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Collection/LazyCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function __construct(iterable|callable $source = [])
$source = $source(...); // @phpstan-ignore-line
}

$this->source = \is_array($source) ? new \ArrayIterator($source) : $source;
$this->source = \is_array($source) ? new \ArrayIterator($source) : $source; // @phpstan-ignore-line
}

/**
Expand Down
32 changes: 17 additions & 15 deletions tests/Doctrine/HasDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@

namespace Zenstruck\Collection\Tests\Doctrine;

use Doctrine\DBAL\Logging\DebugStack;
use Doctrine\Bundle\DoctrineBundle\Middleware\DebugMiddleware;
use Doctrine\DBAL\Configuration;
use Doctrine\DBAL\DriverManager;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\ORMSetup;
use Doctrine\ORM\Tools\SchemaTool;
use Doctrine\ORM\Tools\Setup;
use Symfony\Bridge\Doctrine\Middleware\Debug\DebugDataHolder;
use Zenstruck\Collection\Tests\Doctrine\Fixture\Entity;
use Zenstruck\Collection\Tests\Doctrine\Fixture\Relation;

Expand All @@ -23,16 +26,20 @@
*/
trait HasDatabase
{
protected ?EntityManager $em = null;
protected EntityManager $em;
private DebugDataHolder $debugDataHolder;

/**
* @before
*/
protected function setupEntityManager(): void
{
$this->em = EntityManager::create(
['driver' => 'pdo_sqlite', 'memory' => true],
Setup::createAttributeMetadataConfiguration([], true),
$configuration = new Configuration();
$configuration->setMiddlewares([new DebugMiddleware($this->debugDataHolder = new DebugDataHolder(), null)]);

$this->em = new EntityManager(
DriverManager::getConnection(['driver' => 'pdo_sqlite', 'memory' => true], $configuration),
ORMSetup::createAttributeMetadataConfiguration([], true),
);

$schemaTool = new SchemaTool($this->em);
Expand All @@ -47,23 +54,18 @@ protected function setupEntityManager(): void
*/
protected function teardownEntityManager(): void
{
$this->em = null;
unset($this->em, $this->debugDataHolder);
}

protected function assertQueryCount(int $expected, callable $callback): void
{
$logger = new DebugStack();
$this->em->getConnection()->getConfiguration()->setSQLLogger($logger);
$this->debugDataHolder->reset();

$callback();

if ($expected === \count($logger->queries)) {
$this->assertTrue(true);

return;
}
$queries = $this->debugDataHolder->getData()['default'] ?? [];

$this->fail(\sprintf('Expected %d queries but got %d.', $expected, \count($logger->queries)));
$this->assertCount($expected, $queries, \sprintf('Expected %d queries but got %d.', $expected, $queries));
}

protected function persistEntities(int $count): void
Expand Down
13 changes: 11 additions & 2 deletions tests/Doctrine/ORM/Bridge/ORMEntityRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@

namespace Zenstruck\Collection\Tests\Doctrine\ORM\Bridge;

use Zenstruck\Collection\Doctrine\ObjectRepository;
use Zenstruck\Collection\Doctrine\ORM\Bridge\ORMEntityRepository;
use Zenstruck\Collection\Tests\Doctrine\Fixture\Entity;
use Zenstruck\Collection\Tests\Doctrine\ORM\EntityRepositoryTest;

/**
* @author Kevin Bond <[email protected]>
*/
final class ORMEntityRepositoryTest extends EntityRepositoryTest
class ORMEntityRepositoryTest extends EntityRepositoryTest
{
/**
* @test
Expand All @@ -38,7 +39,15 @@ public function find_all_is_empty_if_repository_is_empty(): void
$this->assertSame([], $this->createWithItems(0)->findAll());
}

protected function repo(): ORMEntityRepository
/**
* @test
*/
public function can_create_query_builder(): void
{
$this->assertEmpty($this->repo()->createQueryBuilder('e')->getQuery()->execute());
}

protected function repo(): ObjectRepository
{
return new ORMEntityRepository($this->em, $this->em->getClassMetadata(Entity::class));
}
Expand Down
30 changes: 30 additions & 0 deletions tests/Doctrine/ORM/Bridge/ORMServiceEntityRepositoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

/*
* This file is part of the zenstruck/collection package.
*
* (c) Kevin Bond <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Zenstruck\Collection\Tests\Doctrine\ORM\Bridge;

use Doctrine\Persistence\ManagerRegistry;
use Zenstruck\Collection\Doctrine\ORM\Bridge\ORMServiceEntityRepository;
use Zenstruck\Collection\Tests\Doctrine\Fixture\Entity;

/**
* @author Kevin Bond <[email protected]>
*/
final class ORMServiceEntityRepositoryTest extends ORMEntityRepositoryTest
{
protected function repo(): ORMServiceEntityRepository
{
$manager = $this->createMock(ManagerRegistry::class);
$manager->method('getManagerForClass')->willReturn($this->em);

return new ORMServiceEntityRepository($manager, Entity::class);
}
}

0 comments on commit c3f6bea

Please sign in to comment.