-
-
Notifications
You must be signed in to change notification settings - Fork 344
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
Conversation
Makes life easier when you want to set sorting, caching, etc. directly from an admin, e.g. in configureFormFields
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A case could be added for supportsQueryDataProvider
https://github.com/sonata-project/SonataDoctrineORMAdminBundle/blob/4.x/tests/Model/ModelManagerTest.php#L141-L154
Co-authored-by: Vincent Langlet <VincentLanglet@users.noreply.github.com>
Added test for Query class
Committed change & added test - I think it should work? |
tests/Model/ModelManagerTest.php
Outdated
@@ -149,6 +149,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)]; |
There was a problem hiding this comment.
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;
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
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
Forgot use statement for Query
tests/Model/ModelManagerTest.php
Outdated
@@ -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)]; |
There was a problem hiding this comment.
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
Switch to AbstractQuery since that's where execute() lives, this way we can support NativeQuery, AND AbstractQuery can be mocked for tests.
Switched to AbstractQuery - supports NativeQuery + easier to mock |
Makes life easier when you want to set sorting, caching, etc. directly from an admin, e.g. in configureFormFields
Subject
I am hitting use cases in admins where I want to change sort order, add caching, etc. which requires modifying the actual Doctrine query object, not the QueryBuilder, and I need to then be able to pass it in. This worked previously in 3.X because there was a default call to execute() which just happened to work on Doctrine's Query class; I think it should be explicitly supported in ModelManager, to make things easier.
I am targeting this branch, because it does not break BC.
Changelog