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

Conversation

djpretzel
Copy link
Contributor

@djpretzel djpretzel commented Aug 27, 2022

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

### Added
 - ModelManager::supportsQuery now supports AbstractQuery
 - ModelManager::execute now supports AbstractQuery

Makes life easier when you want to set sorting, caching, etc. directly from an admin, e.g. in configureFormFields
src/Model/ModelManager.php Outdated Show resolved Hide resolved
Copy link
Member

@VincentLanglet VincentLanglet left a comment

Choose a reason for hiding this comment

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

djpretzel and others added 2 commits August 27, 2022 15:07
Co-authored-by: Vincent Langlet <VincentLanglet@users.noreply.github.com>
Added test for Query class
@djpretzel
Copy link
Contributor Author

Committed change & added test - I think it should work?

@@ -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)];
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

Forgot use statement for Query
@@ -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 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.
@djpretzel
Copy link
Contributor Author

Switched to AbstractQuery - supports NativeQuery + easier to mock

@VincentLanglet VincentLanglet requested review from jordisala1991 and a team August 28, 2022 16:08
@VincentLanglet VincentLanglet merged commit 26ec748 into sonata-project:4.x Aug 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants