Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate some Pool methods related to groups and fix array shapes #6659

Merged
merged 4 commits into from
Dec 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions UPGRADE-3.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ Argument 2 of `Sonata\AdminBundle\Model\ModelManagerInterface::createQuery()` me
Use `Sonata\AdminBundle\SonataConfiguration::getLogo()` instead.
- `Sonata\AdminBundle\Admin\Pool::getOption()` method has been deprecated.
Use `Sonata\AdminBundle\SonataConfiguration::getOption()` instead.
- `Sonata\AdminBundle\Admin\Pool::getGroups()` method has been deprecated.
- `Sonata\AdminBundle\Admin\Pool::hasGroup()` method has been deprecated.
- `Sonata\AdminBundle\Admin\Pool::getAdminsByGroup()` method has been deprecated.

### Sonata\AdminBundle\Admin\FieldDescriptionInterface

Expand Down
62 changes: 61 additions & 1 deletion src/Admin/Pool.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,24 @@
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;

/**
* @psalm-type Group = array{
* label: string,
* label_catalogue: string,
* icon: string,
* item_adds: array,
* items: array<array-key, array{
* admin?: string,
* label?: string,
* roles: list<string>,
* route?: string,
* router_absolute: bool,
* route_params: array<string, string>
* }>,
* keep_open: bool,
* on_top: bool,
* roles: list<string>
* }
*
* @final since sonata-project/admin-bundle 3.52
*
* @author Thomas Rabaix <[email protected]>
Expand All @@ -38,6 +56,8 @@ class Pool

/**
* @var array
* @phpstan-var array<string, array<string, mixed>>
* @psalm-var array<string, Group>
*/
protected $adminGroups = [];

Expand Down Expand Up @@ -137,10 +157,19 @@ public function __construct(
}

/**
* @return array<string, array<string, AdminInterface>>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you remove the docs here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Psalm was failing and I'll check later today, but looks like this method does not work, even throws an error, I was going to check to deprecate it.

Based on this structure:

https://github.com/sonata-project/SonataAdminBundle/pull/6659/files#diff-96aac3a8c78cc9632a36cbb7489484b6c87ae336c1ceeaa028c4b265cafc6f38R41-R57

this method would try to to get and admin with id label, label_catalogue, icon, etc, that's why I guess it is not being used, but I haven't checked yet.

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

$groups = $this->adminGroups;

foreach ($this->adminGroups as $name => $adminGroup) {
Expand All @@ -153,6 +182,10 @@ public function getGroups()
}

/**
* NEXT_MAJOR: Remove this method.
*
* @deprecated since sonata-project/admin-bundle 3.x and will be removed in 4.0.
*
* Returns whether an admin group exists or not.
*
* @param string $group
Expand All @@ -161,11 +194,26 @@ public function getGroups()
*/
public function hasGroup($group)
{
@trigger_error(sprintf(
'Method "%s()" is deprecated since sonata-project/admin-bundle 3.x and will be removed in version 4.0.',
__METHOD__
), E_USER_DEPRECATED);

return isset($this->adminGroups[$group]);
}

/**
* @return array
* @phpstan-return array<string, array{
* label: string,
* label_catalogue: string,
* icon: string,
* item_adds: array,
* items: array<array-key, AdminInterface>,
* keep_open: bool,
* on_top: bool,
* roles: list<string>
* }>
*/
public function getDashboardGroups()
{
Expand Down Expand Up @@ -198,6 +246,10 @@ public function getDashboardGroups()
}

/**
* NEXT_MAJOR: Remove this method.
*
* @deprecated since sonata-project/admin-bundle 3.x and will be removed in 4.0.
*
* Returns all admins related to the given $group.
*
* @param string $group
Expand All @@ -208,6 +260,11 @@ public function getDashboardGroups()
*/
public function getAdminsByGroup($group)
{
@trigger_error(sprintf(
'Method "%s()" is deprecated since sonata-project/admin-bundle 3.x and will be removed in version 4.0.',
__METHOD__
), E_USER_DEPRECATED);

if (!isset($this->adminGroups[$group])) {
throw new \InvalidArgumentException(sprintf('Group "%s" not found in admin pool.', $group));
}
Expand Down Expand Up @@ -449,6 +506,9 @@ public function getContainer()
}

/**
* @phpstan-param array<string, array<string, mixed>> $adminGroups
* @psalm-param array<string, Group> $adminGroups
*
* @return void
*/
public function setAdminGroups(array $adminGroups)
Expand Down
13 changes: 8 additions & 5 deletions src/Twig/Extension/GroupExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,16 @@ public function getFunctions(): array
}

/**
* @phpstan-return array{array{
* roles: list<string>,
* icon: string,
* @phpstan-return array<array{
* label: string,
* label_catalogue: string,
* items: AdminInterface[]
* }}
* icon: string,
* item_adds: array,
* items: AdminInterface[],
* keep_open: bool,
* on_top: bool,
* roles: list<string>
* }>
*/
public function getDashboardGroupsWithCreatableAdmins(): array
{
Expand Down
40 changes: 38 additions & 2 deletions tests/Admin/PoolTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@
use Sonata\AdminBundle\Admin\AdminInterface;
use Sonata\AdminBundle\Admin\Pool;
use Sonata\AdminBundle\Templating\MutableTemplateRegistryInterface;
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\ContainerInterface;

class PoolTest extends TestCase
{
use ExpectDeprecationTrait;

/**
* @var Container
*/
Expand All @@ -40,6 +43,11 @@ protected function setUp(): void
$this->pool = new Pool($this->container, 'Sonata Admin', '/path/to/pic.png', ['foo' => 'bar']);
}

/**
* NEXT_MAJOR: Remove this method.
*
* @group legacy
*/
public function testGetGroups(): void
{
$this->container->set('sonata.user.admin.group1', $this->createMock(AdminInterface::class));
Expand All @@ -50,17 +58,26 @@ public function testGetGroups(): void
'adminGroup1' => ['sonata.user.admin.group1' => []],
]);

$this->expectDeprecation('Method "Sonata\AdminBundle\Admin\Pool::getGroups()" is deprecated since sonata-project/admin-bundle 3.x and will be removed in version 4.0.');

$result = $this->pool->getGroups();
$this->assertArrayHasKey('adminGroup1', $result);
$this->assertArrayHasKey('sonata.user.admin.group1', $result['adminGroup1']);
}

/**
* NEXT_MAJOR: Remove this test.
*
* @group legacy
*/
public function testHasGroup(): void
{
$this->pool->setAdminGroups([
'adminGroup1' => [],
]);

$this->expectDeprecation('Method "Sonata\AdminBundle\Admin\Pool::hasGroup()" is deprecated since sonata-project/admin-bundle 3.x and will be removed in version 4.0.');

$this->assertTrue($this->pool->hasGroup('adminGroup1'));
$this->assertFalse($this->pool->hasGroup('adminGroup2'));
}
Expand Down Expand Up @@ -103,26 +120,43 @@ public function testGetDashboardGroups(): void
$this->assertSame($adminGroup1, $groups['adminGroup1']['items']['itemKey']);
}

/**
* NEXT_MAJOR: Remove this test.
*
* @group legacy
*/
public function testGetAdminsByGroupWhenGroupNotSet(): void
{
$this->expectException(\InvalidArgumentException::class);

$this->pool->setAdminGroups([
'adminGroup1' => [],
]);

$this->expectException(\InvalidArgumentException::class);

$this->pool->getAdminsByGroup('adminGroup2');
}

/**
* NEXT_MAJOR: Remove this test.
*
* @group legacy
*/
public function testGetAdminsByGroupWhenGroupIsEmpty(): void
{
$this->pool->setAdminGroups([
'adminGroup1' => [],
]);

$this->expectDeprecation('Method "Sonata\AdminBundle\Admin\Pool::getAdminsByGroup()" is deprecated since sonata-project/admin-bundle 3.x and will be removed in version 4.0.');

$this->assertSame([], $this->pool->getAdminsByGroup('adminGroup1'));
}

/**
* NEXT_MAJOR: Remove this test.
*
* @group legacy
*/
public function testGetAdminsByGroup(): void
{
$this->container->set('sonata.admin1', $this->createMock(AdminInterface::class));
Expand All @@ -143,6 +177,8 @@ public function testGetAdminsByGroup(): void
],
]);

$this->expectDeprecation('Method "Sonata\AdminBundle\Admin\Pool::getAdminsByGroup()" is deprecated since sonata-project/admin-bundle 3.x and will be removed in version 4.0.');

$this->assertCount(2, $this->pool->getAdminsByGroup('adminGroup1'));
$this->assertCount(1, $this->pool->getAdminsByGroup('adminGroup2'));
}
Expand Down