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

Add ModelManagerInterface::supportsQuery #6381

Merged
merged 1 commit into from
Sep 16, 2020
Merged
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
18 changes: 17 additions & 1 deletion src/Form/ChoiceList/ModelChoiceLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,25 @@ public function __construct(
$this->modelManager = $modelManager;
$this->class = $class;
$this->property = $property;
$this->query = $query;
$this->choices = $choices;

if ($query) {
// NEXT_MAJOR: Remove the method_exists check.
if (method_exists($this->modelManager, 'supportsQuery')) {
if (!$this->modelManager->supportsQuery($query)) {
// NEXT_MAJOR: Remove the deprecation and uncomment the exception.
@trigger_error(
'Passing a query which is not supported by the model manager is deprecated since'
.' sonata-project/admin-bundle 3.x and will throw an exception in version 4.0.',
E_USER_DEPRECATED
);
// throw new \InvalidArgumentException('The model manager does not support the query.');
}
}

$this->query = $query;
}

$this->identifier = $this->modelManager->getIdentifierFieldNames($this->class);

// The property option defines, which property (path) is used for
Expand Down
5 changes: 5 additions & 0 deletions src/Model/ModelManagerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

/**
* A model manager is a bridge between the model classes and the admin functionality.
*
* @method bool supportsQuery(object $query)
*/
interface ModelManagerInterface extends DatagridManagerInterface
{
Expand Down Expand Up @@ -293,6 +295,9 @@ public function modelReverseTransform($class, array $array = []);
*/
public function modelTransform($class, $instance);

// NEXT_MAJOR: Uncomment this.
// public function supportsQuery(object $query): bool;

/**
* @param object $query
*/
Expand Down
5 changes: 5 additions & 0 deletions tests/App/Model/ModelManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,11 @@ public function modelTransform($class, $instance): object
throw new \BadMethodCallException('Not implemented.');
}

public function supportsQuery(object $query): bool
{
return true;
}

public function executeQuery($query): void
{
}
Expand Down
24 changes: 23 additions & 1 deletion tests/Form/ChoiceList/ModelChoiceLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,36 @@
use Sonata\AdminBundle\Form\ChoiceList\ModelChoiceLoader;
use Sonata\AdminBundle\Model\ModelManagerInterface;
use Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\Foo;
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;

class ModelChoiceLoaderTest extends TestCase
{
use ExpectDeprecationTrait;

private $modelManager;

protected function setUp(): void
{
$this->modelManager = $this->getMockForAbstractClass(ModelManagerInterface::class);
$this->modelManager = $this->createMock(ModelManagerInterface::class);
}

/**
* NEXT_MAJOR: Expect exception instead.
*
* @group legacy
*/
public function testConstructWithUnsupportedQuery(): void
{
// NEXT_MAJOR: Use `$this->modelManager` instead
$modelManager = $this
->getMockBuilder(ModelManagerInterface::class)
->addMethods(['supportsQuery'])
->getMockForAbstractClass();

$modelManager->method('supportsQuery')->willReturn(false);

$this->expectDeprecation('Passing a query which is not supported by the model manager is deprecated since sonata-project/admin-bundle 3.x and will throw an exception in version 4.0.');
new ModelChoiceLoader($modelManager, \stdClass::class, null, new \stdClass());
}

public function testLoadFromEntityWithSamePropertyValues(): void
Expand Down