Skip to content

Commit

Permalink
Deprecate collection-related methods on ModelManager
Browse files Browse the repository at this point in the history
These methods were deprecated in the interface in sonata-project/SonataAdminBundle#6312 and released in 3.75
  • Loading branch information
franmomu committed Sep 12, 2020
1 parent 3a3779d commit 350fd59
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 1 deletion.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"doctrine/mongodb-odm": "^1.3 || ^2.0",
"doctrine/mongodb-odm-bundle": "^3.0 || ^4.0",
"doctrine/persistence": "^1.3.4 || ^2.0",
"sonata-project/admin-bundle": "^3.69",
"sonata-project/admin-bundle": "^3.75",
"sonata-project/form-extensions": "^0.1 || ^1.4",
"symfony/config": "^4.4",
"symfony/dependency-injection": "^4.4",
Expand Down
70 changes: 70 additions & 0 deletions src/Model/ModelManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -356,8 +356,18 @@ public function getModelInstance($class)
return new $class();
}

/**
* NEXT_MAJOR: Remove this method.
*
* @deprecated since sonata-project/doctrine-mongodb-admin-bundle 3.x and will be removed in version 4.0.
*/
public function getSortParameters(FieldDescriptionInterface $fieldDescription, DatagridInterface $datagrid)
{
@trigger_error(sprintf(
'Method %s() is deprecated since sonata-project/doctrine-mongodb-admin-bundle 3.x and will be removed in version 4.0.',
__METHOD__
), E_USER_DEPRECATED);

$values = $datagrid->getValues();

if ($this->isFieldAlreadySorted($fieldDescription, $datagrid)) {
Expand All @@ -375,8 +385,18 @@ public function getSortParameters(FieldDescriptionInterface $fieldDescription, D
return ['filter' => $values];
}

/**
* NEXT_MAJOR: Remove this method.
*
* @deprecated since sonata-project/doctrine-mongodb-admin-bundle 3.x and will be removed in version 4.0.
*/
public function getPaginationParameters(DatagridInterface $datagrid, $page)
{
@trigger_error(sprintf(
'Method %s() is deprecated since sonata-project/doctrine-mongodb-admin-bundle 3.x and will be removed in version 4.0.',
__METHOD__
), E_USER_DEPRECATED);

$values = $datagrid->getValues();

if (isset($values['_sort_by']) && $values['_sort_by'] instanceof FieldDescriptionInterface) {
Expand Down Expand Up @@ -421,28 +441,78 @@ public function modelReverseTransform($class, array $array = [])
return $instance;
}

/**
* NEXT_MAJOR: Remove this method.
*
* @deprecated since sonata-project/doctrine-mongodb-admin-bundle 3.x and will be removed in version 4.0.
*/
public function getModelCollectionInstance($class)
{
@trigger_error(sprintf(
'Method %s() is deprecated since sonata-project/doctrine-mongodb-admin-bundle 3.x and will be removed in version 4.0.',
__METHOD__
), E_USER_DEPRECATED);

return new ArrayCollection();
}

/**
* NEXT_MAJOR: Remove this method.
*
* @deprecated since sonata-project/doctrine-mongodb-admin-bundle 3.x and will be removed in version 4.0.
*/
public function collectionClear(&$collection)
{
@trigger_error(sprintf(
'Method %s() is deprecated since sonata-project/doctrine-mongodb-admin-bundle 3.x and will be removed in version 4.0.',
__METHOD__
), E_USER_DEPRECATED);

return $collection->clear();
}

/**
* NEXT_MAJOR: Remove this method.
*
* @deprecated since sonata-project/doctrine-mongodb-admin-bundle 3.x and will be removed in version 4.0.
*/
public function collectionHasElement(&$collection, &$element)
{
@trigger_error(sprintf(
'Method %s() is deprecated since sonata-project/doctrine-mongodb-admin-bundle 3.x and will be removed in version 4.0.',
__METHOD__
), E_USER_DEPRECATED);

return $collection->contains($element);
}

/**
* NEXT_MAJOR: Remove this method.
*
* @deprecated since sonata-project/doctrine-mongodb-admin-bundle 3.x and will be removed in version 4.0.
*/
public function collectionAddElement(&$collection, &$element)
{
@trigger_error(sprintf(
'Method %s() is deprecated since sonata-project/doctrine-mongodb-admin-bundle 3.x and will be removed in version 4.0.',
__METHOD__
), E_USER_DEPRECATED);

return $collection->add($element);
}

/**
* NEXT_MAJOR: Remove this method.
*
* @deprecated since sonata-project/doctrine-mongodb-admin-bundle 3.x and will be removed in version 4.0.
*/
public function collectionRemoveElement(&$collection, &$element)
{
@trigger_error(sprintf(
'Method %s() is deprecated since sonata-project/doctrine-mongodb-admin-bundle 3.x and will be removed in version 4.0.',
__METHOD__
), E_USER_DEPRECATED);

return $collection->removeElement($element);
}

Expand Down
1 change: 1 addition & 0 deletions tests/App/public/bundles/sonataadmin
1 change: 1 addition & 0 deletions tests/App/public/bundles/sonatacore
1 change: 1 addition & 0 deletions tests/App/public/bundles/sonatatwig
21 changes: 21 additions & 0 deletions tests/Model/ModelManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,15 @@
use Sonata\DoctrineMongoDBAdminBundle\Tests\Fixtures\Document\SimpleDocument;
use Sonata\DoctrineMongoDBAdminBundle\Tests\Fixtures\Document\SimpleDocumentWithPrivateSetter;
use Symfony\Bridge\Doctrine\ManagerRegistry;
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
use Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException;
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\PropertyAccess\PropertyAccessor;

final class ModelManagerTest extends TestCase
{
use ExpectDeprecationTrait;

/**
* @var PropertyAccessor
*/
Expand Down Expand Up @@ -89,6 +92,11 @@ public function testGetNormalizedIdentifierNull(): void
$this->assertNull($manager->getNormalizedIdentifier(null));
}

/**
* NEXT_MAJOR: Remove this method.
*
* @group legacy
*/
public function testSortParameters(): void
{
$manager = new ModelManager($this->registry, $this->propertyAccessor);
Expand Down Expand Up @@ -120,6 +128,7 @@ public function testSortParameters(): void
'_sort_order' => 'ASC',
]);

$this->expectDeprecation('Method Sonata\DoctrineMongoDBAdminBundle\Model\ModelManager::getSortParameters() is deprecated since sonata-project/doctrine-mongodb-admin-bundle 3.x and will be removed in version 4.0.');
$parameters = $manager->getSortParameters($field1, $datagrid1);

$this->assertSame('DESC', $parameters['filter']['_sort_order']);
Expand Down Expand Up @@ -239,10 +248,16 @@ public function testModelReverseTransformFailsWithPrivateProperties2(): void
$manager->modelReverseTransform($class, ['plumbus' => 42]);
}

/**
* NEXT_MAJOR: Remove this test.
*
* @group legacy
*/
public function testCollections(): void
{
$model = new ModelManager($this->registry, $this->propertyAccessor);

$this->expectDeprecation('Method Sonata\DoctrineMongoDBAdminBundle\Model\ModelManager::getModelCollectionInstance() is deprecated since sonata-project/doctrine-mongodb-admin-bundle 3.x and will be removed in version 4.0.');
$collection = $model->getModelCollectionInstance('whyDoWeEvenHaveThisParameter');
$this->assertInstanceOf(ArrayCollection::class, $collection);

Expand Down Expand Up @@ -272,6 +287,11 @@ public function testModelTransform(): void
$this->assertSame($instance, $result);
}

/**
* NEXT_MAJOR: Remove this test.
*
* @group legacy
*/
public function testGetPaginationParameters(): void
{
$datagrid = $this->createMock(DatagridInterface::class);
Expand All @@ -287,6 +307,7 @@ public function testGetPaginationParameters(): void

$model = new ModelManager($this->registry, $this->propertyAccessor);

$this->expectDeprecation('Method Sonata\DoctrineMongoDBAdminBundle\Model\ModelManager::getPaginationParameters() is deprecated since sonata-project/doctrine-mongodb-admin-bundle 3.x and will be removed in version 4.0.');
$result = $model->getPaginationParameters($datagrid, $page = 5);

$this->assertSame($page, $result['filter']['_page']);
Expand Down

0 comments on commit 350fd59

Please sign in to comment.