Skip to content

Commit

Permalink
Avoid adminBundle deprecations (#2289)
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet authored Feb 28, 2022
1 parent 0957e32 commit d5747fa
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 74 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
"phpunit/phpunit": "^9.5",
"psalm/plugin-phpunit": "^0.16",
"psalm/plugin-symfony": "^3.0",
"sonata-project/admin-bundle": "^4.0",
"sonata-project/admin-bundle": "^4.9",
"sonata-project/block-bundle": "^4.0",
"sonata-project/classification-bundle": "^4.0",
"sonata-project/doctrine-orm-admin-bundle": "^4.0",
Expand All @@ -98,7 +98,7 @@
"doctrine/orm": "<2.9",
"knplabs/knp-menu-bundle": "<3.0",
"liip/imagine-bundle": "<2.0",
"sonata-project/admin-bundle": "<4.0",
"sonata-project/admin-bundle": "<4.9",
"sonata-project/block-bundle": "<4.0",
"sonata-project/classification-bundle": "<4.0",
"sonata-project/doctrine-orm-admin-bundle": "<4.0"
Expand Down
44 changes: 33 additions & 11 deletions src/Admin/BaseMediaAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,43 @@ abstract class BaseMediaAdmin extends AbstractAdmin
protected $classnameLabel = 'Media';

/**
* @phpstan-param class-string<\Sonata\MediaBundle\Model\MediaInterface> $class
* NEXT_MAJOR: Change to (Pool, ?CategoryManagerInterface, ?ContextManagerInterface).
*
* @param Pool|string $pool
* @param CategoryManagerInterface|string|null $categoryManager
* @param ContextManagerInterface|string|null $contextManager
*
* @phpstan-param CategoryManagerInterface|class-string<\Sonata\MediaBundle\Model\MediaInterface>|null $categoryManager
*/
public function __construct(
string $code,
string $class,
string $baseControllerName,
Pool $pool,
?CategoryManagerInterface $categoryManager = null,
?ContextManagerInterface $contextManager = null
$pool,
$categoryManager = null,
$contextManager = null,
?Pool $deprecatedPool = null,
?CategoryManagerInterface $deprecatedCategoryManager = null,
?ContextManagerInterface $deprecatedContextManager = null
) {
parent::__construct($code, $class, $baseControllerName);
// NEXT_MAJOR: Keep the if part.
if ($pool instanceof Pool) {
\assert(!\is_string($categoryManager));
\assert(!\is_string($contextManager));

$this->pool = $pool;
$this->categoryManager = $categoryManager;
$this->contextManager = $contextManager;
parent::__construct();

$this->pool = $pool;
$this->categoryManager = $categoryManager;
$this->contextManager = $contextManager;
} else {
\assert(\is_string($categoryManager));
\assert(\is_string($contextManager));
\assert(null !== $deprecatedPool);

parent::__construct($pool, $categoryManager, $contextManager);

$this->pool = $deprecatedPool;
$this->categoryManager = $deprecatedCategoryManager;
$this->contextManager = $deprecatedContextManager;
}
}

final public function getObjectMetadata(object $object): MetadataInterface
Expand Down
11 changes: 4 additions & 7 deletions src/Admin/GalleryAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,9 @@ final class GalleryAdmin extends AbstractAdmin

private Pool $pool;

/**
* @phpstan-param class-string<\Sonata\MediaBundle\Model\GalleryInterface<\Sonata\MediaBundle\Model\GalleryItemInterface>> $class
*/
public function __construct(string $code, string $class, string $baseControllerName, Pool $pool)
public function __construct(Pool $pool)
{
parent::__construct($code, $class, $baseControllerName);
parent::__construct();

$this->pool = $pool;
}
Expand Down Expand Up @@ -118,8 +115,8 @@ protected function configureListFields(ListMapper $list): void
$list
->addIdentifier('name')
->add('enabled', 'boolean', ['editable' => true])
->add('context', 'trans', ['catalogue' => 'SonataMediaBundle'])
->add('defaultFormat', 'trans', ['catalogue' => 'SonataMediaBundle']);
->add('context', 'trans', ['value_translation_domain' => 'SonataMediaBundle'])
->add('defaultFormat', 'trans', ['value_translation_domain' => 'SonataMediaBundle']);
}

protected function configureDatagridFilters(DatagridMapper $filter): void
Expand Down
28 changes: 10 additions & 18 deletions src/Resources/config/doctrine_mongodb_admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,21 @@

->set('sonata.media.admin.media', MediaAdmin::class)
->tag('sonata.admin', [
'model_class' => '%sonata.media.media.class%',
'controller' => 'sonata.media.controller.media.admin',
'manager_type' => 'doctrine_mongodb',
'group' => 'sonata_media',
'label_catalogue' => 'SonataMediaBundle',
'translation_domain' => 'SonataMediaBundle',
'label' => 'media',
'label_translator_strategy' => 'sonata.admin.label.strategy.underscore',
'icon' => '<i class=\'fa fa-image\'></i>',
])
->args([
'',
'%sonata.media.media.class%',
'sonata.media.controller.media.admin',
new ReferenceConfigurator('sonata.media.pool'),
(new ReferenceConfigurator('sonata.media.manager.category'))->nullOnInvalid(),
(new ReferenceConfigurator('sonata.media.manager.context'))->nullOnInvalid(),
])
->call('setModelManager', [new ReferenceConfigurator('sonata.media.admin.media.manager')])
->call('setTranslationDomain', ['SonataMediaBundle'])
->call('setTemplates', [[
'inner_list_row' => '@SonataMedia/MediaAdmin/inner_row_media.html.twig',
'base_list_field' => '@SonataAdmin/CRUD/base_list_flat_field.html.twig',
Expand All @@ -52,38 +50,32 @@

->set('sonata.media.admin.gallery', GalleryAdmin::class)
->tag('sonata.admin', [
'model_class' => '%sonata.media.gallery.class%',
'controller' => 'sonata.media.controller.gallery.admin',
'manager_type' => 'doctrine_mongodb',
'group' => 'sonata_media',
'label_catalogue' => 'SonataMediaBundle',
'translation_domain' => 'SonataMediaBundle',
'label' => 'gallery',
'label_translator_strategy' => 'sonata.admin.label.strategy.underscore',
'icon' => '<i class=\'fa fa-image\'></i>',
])
->args([
'',
'%sonata.media.gallery.class%',
'sonata.media.controller.gallery.admin',
new ReferenceConfigurator('sonata.media.pool'),
])
->call('setTranslationDomain', ['SonataMediaBundle'])
->call('setTemplates', [[
'list' => '@SonataMedia/GalleryAdmin/list.html.twig',
]])

->set('sonata.media.admin.gallery_item', GalleryItemAdmin::class)
->tag('sonata.admin', [
'model_class' => '%sonata.media.gallery_item.class%',
'controller' => '%sonata.admin.configuration.default_controller%',
'manager_type' => 'doctrine_mongodb',
'group' => 'sonata_media',
'label_catalogue' => 'SonataMediaBundle',
'translation_domain' => 'SonataMediaBundle',
'label' => 'gallery_item',
'show_in_dashboard' => false,
'label_translator_strategy' => 'sonata.admin.label.strategy.underscore',
'icon' => '<i class=\'fa fa-image\'></i>',
])
->args([
'',
'%sonata.media.gallery_item.class%',
'%sonata.admin.configuration.default_controller%',
])
->call('setTranslationDomain', ['SonataMediaBundle']);
]);
};
28 changes: 10 additions & 18 deletions src/Resources/config/doctrine_orm_admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,21 @@

->set('sonata.media.admin.media', MediaAdmin::class)
->tag('sonata.admin', [
'model_class' => '%sonata.media.media.class%',
'controller' => 'sonata.media.controller.media.admin',
'manager_type' => 'orm',
'group' => 'sonata_media',
'label_catalogue' => 'SonataMediaBundle',
'translation_domain' => 'SonataMediaBundle',
'label' => 'media',
'label_translator_strategy' => 'sonata.admin.label.strategy.underscore',
'icon' => '<i class=\'fa fa-image\'></i>',
])
->args([
'',
'%sonata.media.media.class%',
'sonata.media.controller.media.admin',
new ReferenceConfigurator('sonata.media.pool'),
(new ReferenceConfigurator('sonata.media.manager.category'))->nullOnInvalid(),
(new ReferenceConfigurator('sonata.media.manager.context'))->nullOnInvalid(),
])
->call('setModelManager', [new ReferenceConfigurator('sonata.media.admin.media.manager')])
->call('setTranslationDomain', ['SonataMediaBundle'])
->call('setTemplates', [[
'inner_list_row' => '@SonataMedia/MediaAdmin/inner_row_media.html.twig',
'outer_list_rows_mosaic' => '@SonataMedia/MediaAdmin/list_outer_rows_mosaic.html.twig',
Expand All @@ -53,38 +51,32 @@

->set('sonata.media.admin.gallery', GalleryAdmin::class)
->tag('sonata.admin', [
'model_class' => '%sonata.media.gallery.class%',
'controller' => 'sonata.media.controller.gallery.admin',
'manager_type' => 'orm',
'group' => 'sonata_media',
'label' => 'gallery',
'label_catalogue' => 'SonataMediaBundle',
'translation_domain' => 'SonataMediaBundle',
'label_translator_strategy' => 'sonata.admin.label.strategy.underscore',
'icon' => '<i class=\'fa fa-image\'></i>',
])
->args([
'',
'%sonata.media.gallery.class%',
'sonata.media.controller.gallery.admin',
new ReferenceConfigurator('sonata.media.pool'),
])
->call('setTranslationDomain', ['SonataMediaBundle'])
->call('setTemplates', [[
'list' => '@SonataMedia/GalleryAdmin/list.html.twig',
]])

->set('sonata.media.admin.gallery_item', GalleryItemAdmin::class)
->tag('sonata.admin', [
'model_class' => '%sonata.media.gallery_item.class%',
'controller' => '%sonata.admin.configuration.default_controller%',
'manager_type' => 'orm',
'show_in_dashboard' => false,
'group' => 'sonata_media',
'label_catalogue' => 'SonataMediaBundle',
'translation_domain' => 'SonataMediaBundle',
'label' => 'gallery_item',
'label_translator_strategy' => 'sonata.admin.label.strategy.underscore',
'icon' => '<i class=\'fa fa-image\'></i>',
])
->args([
'',
'%sonata.media.gallery_item.class%',
'%sonata.admin.configuration.default_controller%',
])
->call('setTranslationDomain', ['SonataMediaBundle']);
]);
};
4 changes: 1 addition & 3 deletions tests/Admin/BaseMediaAdminTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,11 @@ protected function setUp(): void
$this->modelManager = $this->createStub(ModelManagerInterface::class);

$this->mediaAdmin = new TestMediaAdmin(
'media',
Media::class,
'SonataMediaBundle:MediaAdmin',
$this->pool,
$this->categoryManager,
$this->contextManager
);
$this->mediaAdmin->setModelClass(Media::class);
$this->mediaAdmin->setRequest($this->request);
$this->mediaAdmin->setModelManager($this->modelManager);
$this->mediaAdmin->setUniqId('uniqId');
Expand Down
4 changes: 0 additions & 4 deletions tests/Admin/GalleryAdminTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

use PHPUnit\Framework\TestCase;
use Sonata\MediaBundle\Admin\GalleryAdmin;
use Sonata\MediaBundle\Entity\BaseGallery;
use Sonata\MediaBundle\Provider\Pool;

class GalleryAdminTest extends TestCase
Expand All @@ -25,9 +24,6 @@ class GalleryAdminTest extends TestCase
protected function setUp(): void
{
$this->galleryAdmin = new GalleryAdmin(
'media',
BaseGallery::class,
'SonataMediaBundle:GalleryAdmin',
new Pool('default')
);
}
Expand Down
7 changes: 1 addition & 6 deletions tests/Admin/GalleryItemAdminTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,14 @@

use PHPUnit\Framework\TestCase;
use Sonata\MediaBundle\Admin\GalleryItemAdmin;
use Sonata\MediaBundle\Entity\BaseGalleryItem;

class GalleryItemAdminTest extends TestCase
{
private GalleryItemAdmin $galleryItemAdmin;

protected function setUp(): void
{
$this->galleryItemAdmin = new GalleryItemAdmin(
'gallery',
BaseGalleryItem::class,
'SonataMediaBundle:GalleryAdmin'
);
$this->galleryItemAdmin = new GalleryItemAdmin();
}

public function testItDoesNotHaveSubject(): void
Expand Down
4 changes: 0 additions & 4 deletions tests/Admin/ORM/MediaAdminTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use PHPUnit\Framework\TestCase;
use Sonata\ClassificationBundle\Model\CategoryManagerInterface;
use Sonata\MediaBundle\Admin\ORM\MediaAdmin;
use Sonata\MediaBundle\Entity\BaseMedia;
use Sonata\MediaBundle\Provider\Pool;

class MediaAdminTest extends TestCase
Expand All @@ -26,9 +25,6 @@ class MediaAdminTest extends TestCase
protected function setUp(): void
{
$this->mediaAdmin = new MediaAdmin(
'media',
BaseMedia::class,
'SonataMediaBundle:MediaAdmin',
new Pool('default'),
$this->createStub(CategoryManagerInterface::class)
);
Expand Down
3 changes: 2 additions & 1 deletion tests/Provider/AbstractProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ protected function setUp(): void
$this->formBuilder = $this->createMock(FormBuilderInterface::class);
$this->formBuilder->method('getOption')->willReturn('api');

$admin = new MediaAdmin('media', Media::class, '', new Pool('default'));
$admin = new MediaAdmin(new Pool('default'));
$admin->setModelClass(Media::class);
$admin->setLabelTranslatorStrategy($this->createStub(LabelTranslatorStrategyInterface::class));
$admin->setFieldDescriptionFactory($this->createStub(FieldDescriptionFactoryInterface::class));

Expand Down

0 comments on commit d5747fa

Please sign in to comment.