Skip to content

Commit

Permalink
Remove deprecation using Symfony 6.2 (#8003)
Browse files Browse the repository at this point in the history
  • Loading branch information
franmomu authored Jan 11, 2023
1 parent 7cd96b2 commit 53c44ee
Show file tree
Hide file tree
Showing 6 changed files with 173 additions and 118 deletions.
2 changes: 2 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
<directory name="src"/>
<directory name="tests"/>
<ignoreFiles>
<!-- see https://github.com/vimeo/psalm/issues/5338 -->
<file name="src/ArgumentResolver/CompatibleValueResolverInterface.php"/>
<directory name="src/Resources/skeleton"/>
<directory name="vendor"/>
</ignoreFiles>
Expand Down
22 changes: 19 additions & 3 deletions src/ArgumentResolver/AdminValueResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@
use Sonata\AdminBundle\Admin\AdminInterface;
use Sonata\AdminBundle\Request\AdminFetcherInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Controller\ArgumentValueResolverInterface;
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata;

final class AdminValueResolver implements ArgumentValueResolverInterface
final class AdminValueResolver implements CompatibleValueResolverInterface
{
private AdminFetcherInterface $adminFetcher;

Expand All @@ -28,6 +27,7 @@ public function __construct(AdminFetcherInterface $adminFetcher)
$this->adminFetcher = $adminFetcher;
}

// TODO: Deprecate this method when dropping support of Symfony < 6.2
public function supports(Request $request, ArgumentMetadata $argument): bool
{
$type = $argument->getType();
Expand All @@ -54,6 +54,22 @@ public function supports(Request $request, ArgumentMetadata $argument): bool
*/
public function resolve(Request $request, ArgumentMetadata $argument): iterable
{
yield $this->adminFetcher->get($request);
$type = $argument->getType();

if (null === $type) {
return [];
}

if (AdminInterface::class !== $type && !is_subclass_of($type, AdminInterface::class)) {
return [];
}

try {
$admin = $this->adminFetcher->get($request);
} catch (\InvalidArgumentException $exception) {
return [];
}

return is_a($admin, $type) ? [$admin] : [];
}
}
30 changes: 30 additions & 0 deletions src/ArgumentResolver/CompatibleValueResolverInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sonata\AdminBundle\ArgumentResolver;

use Symfony\Component\HttpKernel\Controller\ArgumentValueResolverInterface;
use Symfony\Component\HttpKernel\Controller\ValueResolverInterface;

// TODO: Remove this interface when dropping support of Symfony < 6.2 and replace its usage with ValueResolverInterface
if (interface_exists(ValueResolverInterface::class)) {
/** @internal */
interface CompatibleValueResolverInterface extends ValueResolverInterface
{
}
} else {
/** @internal */
interface CompatibleValueResolverInterface extends ArgumentValueResolverInterface
{
}
}
20 changes: 15 additions & 5 deletions src/ArgumentResolver/ProxyQueryResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@

use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Controller\ArgumentValueResolverInterface;
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata;

final class ProxyQueryResolver implements ArgumentValueResolverInterface
final class ProxyQueryResolver implements CompatibleValueResolverInterface
{
// TODO: Deprecate this method when dropping support of Symfony < 6.2
public function supports(Request $request, ArgumentMetadata $argument): bool
{
$type = $argument->getType();
Expand All @@ -46,12 +46,22 @@ public function supports(Request $request, ArgumentMetadata $argument): bool
*/
public function resolve(Request $request, ArgumentMetadata $argument): iterable
{
$type = $argument->getType();

if (null === $type) {
return [];
}

if (ProxyQueryInterface::class !== $type && !is_subclass_of($type, ProxyQueryInterface::class)) {
return [];
}

foreach ($request->attributes as $attribute) {
if ($attribute instanceof ProxyQueryInterface) {
yield $attribute;

break;
return [$attribute];
}
}

return [];
}
}
130 changes: 66 additions & 64 deletions tests/ArgumentResolver/AdminValueResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,108 +26,110 @@

final class AdminValueResolverTest extends TestCase
{
private PostAdmin $admin;

private AdminValueResolver $adminValueResolver;

protected function setUp(): void
/**
* @dataProvider provideInvalidData
*/
public function testWithInvalidData(Request $request, ArgumentMetadata $argumentMetadata): void
{
$this->admin = new PostAdmin();
$this->admin->setCode('sonata.admin.post');
$admin = new PostAdmin();
$admin->setCode('sonata.admin.post');

$container = new Container();
$container->set('sonata.admin.post', $this->admin);
$container->set('sonata.admin.post', $admin);

$adminFetcher = new AdminFetcher(new Pool($container, ['sonata.admin.post']));
$adminValueResolver = new AdminValueResolver($adminFetcher);

$this->adminValueResolver = new AdminValueResolver($adminFetcher);
static::assertFalse($adminValueResolver->supports($request, $argumentMetadata));
static::assertSame(
[],
$adminValueResolver->resolve($request, $argumentMetadata)
);
}

/**
* @dataProvider supportDataProvider
* @phpstan-return iterable<array-key, array{Request, ArgumentMetadata}>
*/
public function testSupports(bool $expectedSupport, Request $request, ArgumentMetadata $argumentMetadata): void
public function provideInvalidData(): iterable
{
static::assertSame($expectedSupport, $this->adminValueResolver->supports($request, $argumentMetadata));
}
yield 'Object with no type' => [
static::createRequest(),
static::createArgumentMetadata('_sonata_admin'),
];

/**
* @phpstan-return iterable<array-key, array{bool, Request, ArgumentMetadata}>
*/
public function supportDataProvider(): iterable
{
yield 'Object must implement AdminInterface' => [
false,
new Request(),
new ArgumentMetadata('_sonata_admin', self::class, false, false, null),
static::createRequest(),
static::createArgumentMetadata('_sonata_admin', self::class),
];

yield 'Admin code must be passed' => [
false,
Request::create('/'),
new ArgumentMetadata('_sonata_admin', PostAdmin::class, false, false, null),
static::createRequest(),
static::createArgumentMetadata('_sonata_admin', PostAdmin::class),
];

$request = Request::create('/');
$request->attributes->set('_sonata_admin', 'non_existing');

yield 'Admin code must exist' => [
false,
$request,
new ArgumentMetadata('_sonata_admin', PostAdmin::class, false, false, null),
static::createRequest(['_sonata_admin' => 'non_existing']),
static::createArgumentMetadata('_sonata_admin', PostAdmin::class),
];

$request = new Request();
$request->attributes->set('_sonata_admin', 'non_existing');

yield 'Admin fetched must be of the type specified in the action' => [
false,
$request,
new ArgumentMetadata('_sonata_admin', CommentAdmin::class, false, false, null),
static::createRequest(['_sonata_admin' => 'sonata.admin.post']),
static::createArgumentMetadata('_sonata_admin', CommentAdmin::class),
];
}

$request = new Request();
$request->attributes->set('_sonata_admin', 'sonata.admin.post');
public function testResolvesAdminClass(): void
{
$admin = new PostAdmin();
$admin->setCode('sonata.admin.post');

yield 'Admin class is valid' => [
true,
$request,
new ArgumentMetadata('_sonata_admin', PostAdmin::class, false, false, null),
];
$container = new Container();
$container->set('sonata.admin.post', $admin);

yield 'Admin can fetch by interface' => [
true,
$request,
new ArgumentMetadata('_sonata_admin', AdminInterface::class, false, false, null),
];
$adminFetcher = new AdminFetcher(new Pool($container, ['sonata.admin.post']));
$adminValueResolver = new AdminValueResolver($adminFetcher);

$request = static::createRequest(['_sonata_admin' => 'sonata.admin.post']);
$argumentMetadata = static::createArgumentMetadata('_sonata_admin', PostAdmin::class);

static::assertTrue($adminValueResolver->supports($request, $argumentMetadata));
static::assertSame(
[$admin],
$adminValueResolver->resolve($request, $argumentMetadata)
);
}

public function testResolveAdmin(): void
public function testResolvesAdminInterface(): void
{
$request = new Request();
$request->attributes->set('_sonata_admin', 'sonata.admin.post');
$admin = new PostAdmin();
$admin->setCode('sonata.admin.post');

$container = new Container();
$container->set('sonata.admin.post', $admin);

$argument = new ArgumentMetadata('_sonata_admin', PostAdmin::class, false, false, null);
$adminFetcher = new AdminFetcher(new Pool($container, ['sonata.admin.post']));
$adminValueResolver = new AdminValueResolver($adminFetcher);

static::assertTrue($this->adminValueResolver->supports($request, $argument));
$request = static::createRequest(['_sonata_admin' => 'sonata.admin.post']);
$argumentMetadata = static::createArgumentMetadata('_sonata_admin', AdminInterface::class);

static::assertTrue($adminValueResolver->supports($request, $argumentMetadata));
static::assertSame(
[$this->admin],
$this->iterableToArray($this->adminValueResolver->resolve($request, $argument))
[$admin],
$adminValueResolver->resolve($request, $argumentMetadata)
);
}

/**
* @phpstan-template T
* @phpstan-param iterable<T> $iterable
* @phpstan-return array<T>
* @param array<string, mixed> $attributes
*/
private function iterableToArray(iterable $iterable): array
private static function createRequest(array $attributes = []): Request
{
$array = [];
foreach ($iterable as $admin) {
$array[] = $admin;
}
return new Request([], [], $attributes);
}

return $array;
private static function createArgumentMetadata(string $name, ?string $type = null): ArgumentMetadata
{
return new ArgumentMetadata($name, $type, false, false, null);
}
}
Loading

0 comments on commit 53c44ee

Please sign in to comment.