Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update ModelManager to support Doctrine\ORM\Query directly #1691

Merged
merged 6 commits into from
Aug 28, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
12 changes: 9 additions & 3 deletions src/Model/ModelManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\OptimisticLockException;
use Doctrine\ORM\Query;
use Doctrine\ORM\QueryBuilder;
use Doctrine\ORM\Tools\Pagination\Paginator;
use Doctrine\ORM\UnitOfWork;
Expand Down Expand Up @@ -208,7 +209,7 @@ public function createQuery(string $class, string $alias = 'o'): BaseProxyQueryI

public function supportsQuery(object $query): bool
{
return $query instanceof ProxyQuery || $query instanceof QueryBuilder;
return $query instanceof ProxyQuery || $query instanceof Query || $query instanceof QueryBuilder;
}

public function executeQuery(object $query)
Expand All @@ -217,6 +218,10 @@ public function executeQuery(object $query)
return $query->getQuery()->execute();
}

if ($query instanceof Query) {
return $query->execute();
}

if ($query instanceof ProxyQuery) {
/** @phpstan-var Paginator<T> $results */
$results = $query->execute();
Expand All @@ -225,10 +230,11 @@ public function executeQuery(object $query)
}

throw new \InvalidArgumentException(sprintf(
'Argument 1 passed to %s() must be an instance of %s or %s',
'Argument 1 passed to %s() must be an instance of %s, %s, or %s',
__METHOD__,
QueryBuilder::class,
ProxyQuery::class,
Query::class,
ProxyQuery::class
));
}

Expand Down
2 changes: 2 additions & 0 deletions tests/Model/ModelManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\OptimisticLockException;
use Doctrine\ORM\Query;
use Doctrine\ORM\QueryBuilder;
use Doctrine\Persistence\ManagerRegistry;
use PHPUnit\Framework\MockObject\MockObject;
Expand Down Expand Up @@ -149,6 +150,7 @@ public function testSupportsQuery(bool $expected, object $object): void
public function supportsQueryDataProvider(): iterable
{
yield [true, new ProxyQuery($this->createMock(QueryBuilder::class))];
yield [true, $this->createMock(Query::class)];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You forgot use Doctrine\ORM\Query;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I did! I need to get comfortable doing this via IDE instead of the github website, so I get warnings - added!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also with an IDE you can install the vendor and run the tests, seems like it's failing

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have to use

        yield [true, new Query()];

to avoid the error

1) Error
The data provider specified for Sonata\DoctrineORMAdminBundle\Tests\Model\ModelManagerTest::testSupportsQuery is invalid.
PHPUnit\Framework\MockObject\ClassIsFinalException: Class "Doctrine\ORM\Query" is declared "final" and cannot be doubled
/home/runner/work/SonataDoctrineORMAdminBundle/SonataDoctrineORMAdminBundle/tests/Model/ModelManagerTest.php:153

yield [true, $this->createMock(QueryBuilder::class)];
yield [false, new \stdClass()];
}
Expand Down