Skip to content

Commit

Permalink
Add some deprecations about invalid values
Browse files Browse the repository at this point in the history
  • Loading branch information
franmomu committed Oct 29, 2020
1 parent dee39a5 commit 5ad29fd
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
6 changes: 6 additions & 0 deletions UPGRADE-3.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ UPGRADE 3.x
UPGRADE FROM 3.x to 3.x
=======================

### Sonata\DoctrineMongoDBAdminBundle\Model\ModelManager

Deprecated passing `null` as argument 2 for `find()`.
Deprecated passing `null` or an object which is in state new or removed as argument 1 for `getNormalizedIdentifier()`.
Deprecated passing `null` as argument 1 for `getUrlSafeIdentifier()`.

UPGRADE FROM 3.3 to 3.4
=======================

Expand Down
26 changes: 25 additions & 1 deletion src/Model/ModelManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,13 @@ public function delete($object)

public function find($class, $id)
{
if (!isset($id)) {
if (null === $id) {
@trigger_error(sprintf(
'Passing null as argument 1 for %s() is deprecated since'
.' sonata-project/doctrine-mongodb-admin-bundle 3.x and will be not allowed in version 4.0.',
__METHOD__
), E_USER_DEPRECATED);

return null;
}

Expand Down Expand Up @@ -293,7 +299,14 @@ public function getIdentifierFieldNames($class)

public function getNormalizedIdentifier($document)
{
// NEXT_MAJOR: Remove the following 2 checks and declare "object" as type for argument 1.
if (null === $document) {
@trigger_error(sprintf(
'Passing null as argument 1 for %s() is deprecated since'
.' sonata-project/doctrine-mongodb-admin-bundle 3.x and will be not allowed in version 4.0.',
__METHOD__
), E_USER_DEPRECATED);

return null;
}

Expand All @@ -313,6 +326,17 @@ public function getNormalizedIdentifier($document)

public function getUrlSafeIdentifier($document)
{
// NEXT_MAJOR: Remove the following check and declare "object" as type for argument 1.
if (!\is_object($document)) {
@trigger_error(sprintf(
'Passing other type than object for argument 1 for %s() is deprecated since'
.' sonata-project/doctrine-mongodb-admin-bundle 3.x and will be not allowed in version 4.0.',
__METHOD__
), E_USER_DEPRECATED);

return null;
}

return $this->getNormalizedIdentifier($document);
}

Expand Down
27 changes: 27 additions & 0 deletions tests/Model/ModelManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,19 @@ public function getWrongDocuments(): iterable
yield ['sonata-project'];
}

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

$this->expectDeprecation(
'Passing null as argument 1 for Sonata\DoctrineMongoDBAdminBundle\Model\ModelManager::getNormalizedIdentifier() is deprecated since sonata-project/doctrine-mongodb-admin-bundle 3.x and will be not allowed in version 4.0.'
);

$this->assertNull($manager->getNormalizedIdentifier(null));
}

Expand Down Expand Up @@ -335,10 +344,19 @@ public function testGetModelInstanceForProtectedDocument(): void
$this->assertInstanceOf(ProtectedDocument::class, $model->getModelInstance(ProtectedDocument::class));
}

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

$this->expectDeprecation(
'Passing null as argument 1 for Sonata\DoctrineMongoDBAdminBundle\Model\ModelManager::find() is deprecated since sonata-project/doctrine-mongodb-admin-bundle 3.x and will be not allowed in version 4.0.'
);

$this->assertNull($model->find('notImportant', null));
}

Expand All @@ -351,10 +369,19 @@ public function testGetUrlSafeIdentifierException(): void
$model->getNormalizedIdentifier(new \stdClass());
}

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

$this->expectDeprecation(
'Passing null as argument 1 for Sonata\DoctrineMongoDBAdminBundle\Model\ModelManager::getNormalizedIdentifier() is deprecated since sonata-project/doctrine-mongodb-admin-bundle 3.x and will be not allowed in version 4.0.'
);

$this->assertNull($model->getNormalizedIdentifier(null));
}

Expand Down

0 comments on commit 5ad29fd

Please sign in to comment.