Skip to content

Commit

Permalink
Add ModelManagerInterface::supportQuery
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet committed Sep 13, 2020
1 parent c2e970b commit 6857494
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
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

0 comments on commit 6857494

Please sign in to comment.