Skip to content

Commit

Permalink
Resolve some "NEXT_MAJOR" comments from 3.x (#5602)
Browse files Browse the repository at this point in the history
  • Loading branch information
phansys authored and kunicmarko20 committed Jun 24, 2019
1 parent 1c08282 commit c11e720
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 45 deletions.
12 changes: 6 additions & 6 deletions src/Admin/Pool.php
Expand Up @@ -250,12 +250,12 @@ public function getAdminByAdminCode($adminCode)

foreach ($codes as $code) {
if (!\in_array($code, $this->adminServiceIds, true)) {
@trigger_error(sprintf(
'Passing an invalid admin code as argument 1 for %s() is deprecated since 3.50 and will throw an exception in 4.0.',
__METHOD__
), E_USER_DEPRECATED);

// NEXT_MAJOR : throw `\InvalidArgumentException` instead
throw new \InvalidArgumentException(sprintf(
'Argument 1 passed to %s() must contain a valid admin reference, "%s" found at "%s".',
__METHOD__,
$code,
$adminCode
));
}

if (!$admin->hasChild($code)) {
Expand Down
9 changes: 1 addition & 8 deletions src/Datagrid/ListMapper.php
Expand Up @@ -78,14 +78,7 @@ public function add($name, $type = null, array $fieldDescriptionOptions = [])
}

if (\array_key_exists('identifier', $fieldDescriptionOptions) && !\is_bool($fieldDescriptionOptions['identifier'])) {
@trigger_error(
'Passing a non boolean value for the "identifier" option is deprecated since sonata-project/admin-bundle 3.x and will throw an exception in 4.0.',
E_USER_DEPRECATED
);

$fieldDescriptionOptions['identifier'] = (bool) $fieldDescriptionOptions['identifier'];
// NEXT_MAJOR: Remove the previous 6 lines and use commented line below it instead
// throw new \InvalidArgumentException(sprintf('Value for "identifier" option must be boolean, %s given.', gettype($fieldDescriptionOptions['identifier'])));
throw new \InvalidArgumentException(sprintf('Value for "identifier" option must be boolean, %s given.', \gettype($fieldDescriptionOptions['identifier'])));
}

if ($name instanceof FieldDescriptionInterface) {
Expand Down
43 changes: 31 additions & 12 deletions tests/Admin/PoolTest.php
Expand Up @@ -225,12 +225,7 @@ public function testGetAdminByAdminCodeForChildClass(): void
$this->assertSame('commentAdminClass', $this->pool->getAdminByAdminCode('sonata.news.admin.post|sonata.news.admin.comment'));
}

/**
* @group legacy
*
* @expectedDeprecation Passing an invalid admin code as argument 1 for Sonata\AdminBundle\Admin\Pool::getAdminByAdminCode() is deprecated since 3.50 and will throw an exception in 4.0.
*/
public function testGetAdminByAdminCodeForChildInvalidClass(): void
public function testGetAdminByAdminCodeWithInvalidCode(): void
{
$adminMock = $this->getMockBuilder(AdminInterface::class)
->disableOriginalConstructor()
Expand All @@ -247,7 +242,30 @@ public function testGetAdminByAdminCodeForChildInvalidClass(): void
$this->pool = new Pool($containerMock, 'Sonata', '/path/to/logo.png');
$this->pool->setAdminServiceIds(['sonata.news.admin.post']);

$this->assertFalse($this->pool->getAdminByAdminCode('sonata.news.admin.post|sonata.news.admin.invalid'));
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Argument 1 passed to Sonata\AdminBundle\Admin\Pool::getAdminByAdminCode() must contain a valid admin reference, "sonata.news.admin.valid" found at "sonata.news.admin.post|sonata.news.admin.valid".');

$this->pool->getAdminByAdminCode('sonata.news.admin.post|sonata.news.admin.valid');
}

public function testGetAdminByAdminCodeWithCodeNotChild(): void
{
$adminMock = $this->getMockBuilder(AdminInterface::class)
->disableOriginalConstructor()
->getMock();
$adminMock->expects($this->any())
->method('hasChild')
->willReturn(false);

$containerMock = $this->createMock(ContainerInterface::class);
$containerMock->expects($this->any())
->method('get')
->willReturn($adminMock);

$this->pool = new Pool($containerMock, 'Sonata', '/path/to/logo.png');
$this->pool->setAdminServiceIds(['sonata.news.admin.post', 'sonata.news.admin.valid']);

$this->assertFalse($this->pool->getAdminByAdminCode('sonata.news.admin.post|sonata.news.admin.valid'));
}

/**
Expand Down Expand Up @@ -287,10 +305,6 @@ public function getEmptyRootAdminServiceNames()

/**
* @dataProvider getInvalidChildAdminServiceNames
*
* @group legacy
*
* @expectedDeprecation Passing an invalid admin code as argument 1 for Sonata\AdminBundle\Admin\Pool::getAdminByAdminCode() is deprecated since 3.50 and will throw an exception in 4.0.
*/
public function testGetAdminByAdminCodeWithInvalidChildCode(string $adminId): void
{
Expand All @@ -314,7 +328,12 @@ public function testGetAdminByAdminCodeWithInvalidChildCode(string $adminId): vo
->method('getInstance')
->willReturn($adminMock);

$this->assertFalse($poolMock->getAdminByAdminCode($adminId));
$this->expectExceptionMessageRegExp(sprintf(
'{^Argument 1 passed to Sonata\\\AdminBundle\\\Admin\\\Pool::getAdminByAdminCode\(\) must contain a valid admin reference, "[^"]+" found at "%s"\.$}',
$adminId
));

$poolMock->getAdminByAdminCode($adminId);
}

public function getInvalidChildAdminServiceNames()
Expand Down
20 changes: 1 addition & 19 deletions tests/Datagrid/ListMapperTest.php
Expand Up @@ -137,33 +137,15 @@ public function testAddOptionIdentifier(): void
$this->assertFalse($this->listMapper->get('bazName')->getOption('identifier'));
}

/**
* @group legacy
*
* @expectedDeprecation Passing a non boolean value for the "identifier" option is deprecated since sonata-project/admin-bundle 3.x and will throw an exception in 4.0.
*
* @dataProvider getWrongIdentifierOptions
*/
public function testAddOptionIdentifierWithDeprecatedValue(bool $expected, $value): void
{
$this->assertFalse($this->listMapper->has('fooName'));
$this->listMapper->add('fooName', null, ['identifier' => $value]);
$this->assertTrue($this->listMapper->has('fooName'));
$this->assertSame($expected, $this->listMapper->get('fooName')->getOption('identifier'));
}

/**
* @dataProvider getWrongIdentifierOptions
*/
public function testAddOptionIdentifierWithWrongValue(bool $expected, $value): void
{
// NEXT_MAJOR: Remove the following `markTestSkipped()` call and the `testAddOptionIdentifierWithDeprecatedValue()` test
$this->markTestSkipped('This test must be run in 4.0');

$this->assertFalse($this->listMapper->has('fooName'));

$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessageRegExp('{^Value for "identifier" option must be boolean, [^]+ given.$}');
$this->expectExceptionMessageRegExp('{^Value for "identifier" option must be boolean, [^ ]+ given\.$}');

$this->listMapper->add('fooName', null, ['identifier' => $value]);
}
Expand Down

0 comments on commit c11e720

Please sign in to comment.