Skip to content

Commit

Permalink
Deprecate some invalid arguments at Pool::getAdminByAdminCode()
Browse files Browse the repository at this point in the history
  • Loading branch information
phansys authored and greg0ire committed Jun 24, 2019
1 parent bf184d3 commit e95e95c
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 12 deletions.
25 changes: 24 additions & 1 deletion src/Admin/Pool.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,16 @@ public function hasAdminByClass($class)
*/
public function getAdminByAdminCode($adminCode)
{
if (!\is_string($adminCode)) {
@trigger_error(sprintf(
'Passing a non string value as argument 1 for %s() is deprecated since sonata-project/admin-bundle 3.x and will throw an exception in 4.0.',
__METHOD__
), E_USER_DEPRECATED);

return false;

// NEXT_MAJOR : remove this condition check and declare "string" as type without default value for argument 1
}
$codes = explode('|', $adminCode);
$code = trim(array_shift($codes));

Expand All @@ -251,14 +261,27 @@ 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.',
'Passing an invalid admin code as argument 1 for %s() is deprecated since sonata-project/admin-bundle 3.50 and will throw an exception in 4.0.',
__METHOD__
), E_USER_DEPRECATED);

// NEXT_MAJOR : throw `\InvalidArgumentException` instead
}

if (!$admin->hasChild($code)) {
@trigger_error(sprintf(
'Passing an invalid admin hierarchy inside argument 1 for %s() is deprecated since sonata-project/admin-bundle 3.x and will throw an exception in 4.0.',
__METHOD__
), E_USER_DEPRECATED);

// NEXT_MAJOR : remove the previous `trigger_error()` call, uncomment the following excception and declare AdminInterface as return type
// throw new InvalidArgumentException(sprintf(
// 'Argument 1 passed to %s() must contain a valid admin hierarchy, "%s" is not a valid child for "%s"',
// __METHOD__,
// $code,
// $admin->getCode()
// ));

return false;
}

Expand Down
17 changes: 10 additions & 7 deletions src/Block/AdminSearchBlockService.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,15 @@ public function getName()

public function configureSettings(OptionsResolver $resolver)
{
$resolver->setDefaults([
'admin_code' => false,
'query' => '',
'page' => 0,
'per_page' => 10,
'icon' => '<i class="fa fa-list"></i>',
]);
$resolver
->setDefaults([
'admin_code' => '',
'query' => '',
'page' => 0,
'per_page' => 10,
'icon' => '<i class="fa fa-list"></i>',
])
->setRequired('admin_code')
->setAllowedTypes('admin_code', ['string']);
}
}
56 changes: 53 additions & 3 deletions tests/Admin/PoolTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,9 @@ public function testGetAdminByAdminCodeForChildClass(): void
/**
* @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.
* @expectedDeprecation Passing an invalid admin code as argument 1 for Sonata\AdminBundle\Admin\Pool::getAdminByAdminCode() is deprecated since sonata-project/admin-bundle 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 @@ -250,6 +250,56 @@ public function testGetAdminByAdminCodeForChildInvalidClass(): void
$this->assertFalse($this->pool->getAdminByAdminCode('sonata.news.admin.post|sonata.news.admin.invalid'));
}

/**
* @group legacy
*
* @expectedDeprecation Passing a non string value as argument 1 for Sonata\AdminBundle\Admin\Pool::getAdminByAdminCode() is deprecated since sonata-project/admin-bundle 3.x and will throw an exception in 4.0.
*/
public function testGetAdminByAdminCodeWithNonStringCode(): void
{
$this->pool = new Pool($this->createMock(ContainerInterface::class), 'Sonata', '/path/to/logo.png');

$this->assertFalse($this->pool->getAdminByAdminCode(false));
$this->assertFalse($this->pool->getAdminByAdminCode(true));
$this->assertFalse($this->pool->getAdminByAdminCode(null));
$this->assertFalse($this->pool->getAdminByAdminCode(['']));
$this->assertFalse($this->pool->getAdminByAdminCode(['some_value']));
$this->assertFalse($this->pool->getAdminByAdminCode(1));
$this->assertFalse($this->pool->getAdminByAdminCode(new \stdClass()));
// NEXT_MAJOR: remove the previous assertions, the "@group" and "@expectedDeprecation" annotations, and uncomment the following line
// $this->expectException(\TypeError::class);
}

/**
* @group legacy
*
* @expectedDeprecation Passing an invalid admin hierarchy inside argument 1 for Sonata\AdminBundle\Admin\Pool::getAdminByAdminCode() is deprecated since sonata-project/admin-bundle 3.x and will throw an exception in 4.0.
*/
public function testGetAdminByAdminCodeWithCodeNotChild(): void
{
$adminMock = $this->getMockBuilder(AdminInterface::class)
->disableOriginalConstructor()
->getMock();
$adminMock->expects($this->any())
->method('hasChild')
->willReturn(false);
$adminMock->expects($this->any())
->method('getCode')
->willReturn('sonata.news.admin.post');
$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'));
// NEXT_MAJOR: remove the "@group" and "@expectedDeprecation" annotations, the previous assertion and uncomment the following lines
// $this->expectException(\InvalidArgumentException::class);
// $this->expectExceptionMessage('Argument 1 passed to Sonata\AdminBundle\Admin\Pool::getAdminByAdminCode() must contain a valid admin hierarchy, "sonata.news.admin.valid" is not a valid child for "sonata.news.admin.post"');
//
// $this->pool->getAdminByAdminCode('sonata.news.admin.post|sonata.news.admin.valid');
}

/**
* @dataProvider getEmptyRootAdminServiceNames
*/
Expand Down Expand Up @@ -290,7 +340,7 @@ public function getEmptyRootAdminServiceNames()
*
* @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.
* @expectedDeprecation Passing an invalid admin code as argument 1 for Sonata\AdminBundle\Admin\Pool::getAdminByAdminCode() is deprecated since sonata-project/admin-bundle 3.50 and will throw an exception in 4.0.
*/
public function testGetAdminByAdminCodeWithInvalidChildCode(string $adminId): void
{
Expand Down
2 changes: 1 addition & 1 deletion tests/Block/AdminSearchBlockServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function testDefaultSettings(): void
$blockContext = $this->getBlockContext($blockService);

$this->assertSettings([
'admin_code' => false,
'admin_code' => '',
'query' => '',
'page' => 0,
'per_page' => 10,
Expand Down

0 comments on commit e95e95c

Please sign in to comment.