Skip to content

Commit

Permalink
Address block bundle deprecations
Browse files Browse the repository at this point in the history
  • Loading branch information
franmomu authored and core23 committed Feb 14, 2020
1 parent 8ba398a commit 9533247
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 53 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
"friendsofsymfony/rest-bundle": "<2.1 || >=3.0",
"jms/serializer": "<0.13",
"liip/imagine-bundle": "<1.9",
"sonata-project/block-bundle": "<3.16.1 || >=4.0",
"sonata-project/block-bundle": "<3.17 || >=4.0",
"sonata-project/classification-bundle": "<3.0 || >= 5.0",
"sonata-project/formatter-bundle": "<3.4",
"sonata-project/notification-bundle": "<3.3",
Expand All @@ -79,7 +79,7 @@
"nelmio/api-doc-bundle": "^2.13.4",
"sensio/framework-extra-bundle": "^5.1",
"sonata-project/admin-bundle": "^3.18.3",
"sonata-project/block-bundle": "^3.16.1",
"sonata-project/block-bundle": "^3.17",
"sonata-project/datagrid-bundle": "^2.5",
"sonata-project/doctrine-orm-admin-bundle": "^3.14",
"sonata-project/formatter-bundle": "^3.4 || ^4.0",
Expand Down
18 changes: 13 additions & 5 deletions src/Block/GalleryBlockService.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Form\Type\ModelListType;
use Sonata\BlockBundle\Block\BlockContextInterface;
use Sonata\BlockBundle\Block\Service\AbstractAdminBlockService;
use Sonata\BlockBundle\Block\Service\AbstractBlockService;
use Sonata\BlockBundle\Meta\Metadata;
use Sonata\BlockBundle\Model\BlockInterface;
use Sonata\Doctrine\Model\ManagerInterface;
Expand All @@ -34,13 +34,14 @@
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Templating\EngineInterface;
use Twig\Environment;

/**
* @final since sonata-project/media-bundle 3.21.0
*
* @author Thomas Rabaix <[email protected]>
*/
class GalleryBlockService extends AbstractAdminBlockService
class GalleryBlockService extends AbstractBlockService
{
/**
* @var ManagerInterface
Expand All @@ -53,11 +54,18 @@ class GalleryBlockService extends AbstractAdminBlockService
protected $galleryManager;

/**
* @param string $name
* @var ContainerInterface
*/
public function __construct($name, EngineInterface $templating, ContainerInterface $container, ManagerInterface $galleryManager)
private $container;

/**
* NEXT_MAJOR: Remove `$templating` argument.
*
* @param Environment|string $twigOrName
*/
public function __construct($twigOrName, ?EngineInterface $templating, ContainerInterface $container, ManagerInterface $galleryManager)
{
parent::__construct($name, $templating);
parent::__construct($twigOrName, $templating);

$this->galleryManager = $galleryManager;
$this->container = $container;
Expand Down
13 changes: 8 additions & 5 deletions src/Block/GalleryListBlockService.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

use Sonata\AdminBundle\Form\FormMapper;
use Sonata\BlockBundle\Block\BlockContextInterface;
use Sonata\BlockBundle\Block\Service\AbstractAdminBlockService;
use Sonata\BlockBundle\Block\Service\AbstractBlockService;
use Sonata\BlockBundle\Meta\Metadata;
use Sonata\BlockBundle\Model\BlockInterface;
use Sonata\Form\Type\ImmutableArrayType;
Expand All @@ -27,11 +27,12 @@
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Twig\Environment;

/**
* @final since sonata-project/media-bundle 3.21.0
*/
class GalleryListBlockService extends AbstractAdminBlockService
class GalleryListBlockService extends AbstractBlockService
{
/**
* @var GalleryManagerInterface
Expand All @@ -44,11 +45,13 @@ class GalleryListBlockService extends AbstractAdminBlockService
protected $pool;

/**
* @param string $name
* NEXT_MAJOR: Remove `$templating` argument.
*
* @param Environment|string $twigOrName
*/
public function __construct($name, EngineInterface $templating, GalleryManagerInterface $galleryManager, Pool $pool)
public function __construct($twigOrName, ?EngineInterface $templating, GalleryManagerInterface $galleryManager, Pool $pool)
{
parent::__construct($name, $templating);
parent::__construct($twigOrName, $templating);

$this->galleryManager = $galleryManager;
$this->pool = $pool;
Expand Down
26 changes: 19 additions & 7 deletions src/Block/MediaBlockService.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,29 @@
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Form\Type\ModelListType;
use Sonata\BlockBundle\Block\BlockContextInterface;
use Sonata\BlockBundle\Block\Service\AbstractAdminBlockService;
use Sonata\BlockBundle\Block\Service\AbstractBlockService;
use Sonata\BlockBundle\Meta\Metadata;
use Sonata\BlockBundle\Model\BlockInterface;
use Sonata\Doctrine\Model\ManagerInterface;
use Sonata\Form\Type\ImmutableArrayType;
use Sonata\MediaBundle\Admin\BaseMediaAdmin;
use Sonata\MediaBundle\Model\MediaInterface;
use Sonata\MediaBundle\Provider\Pool;
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilder;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Templating\EngineInterface;
use Twig\Environment;

/**
* @final since sonata-project/media-bundle 3.21.0
*
* @author Thomas Rabaix <[email protected]>
*/
class MediaBlockService extends AbstractAdminBlockService
class MediaBlockService extends AbstractBlockService
{
/**
* @var BaseMediaAdmin
Expand All @@ -51,11 +52,22 @@ class MediaBlockService extends AbstractAdminBlockService
protected $mediaManager;

/**
* @param string $name
* @var ContainerInterface
*/
public function __construct($name, EngineInterface $templating, ContainerInterface $container, ManagerInterface $mediaManager)
{
parent::__construct($name, $templating);
private $container;

/**
* NEXT_MAJOR: Remove `$templating` argument.
*
* @param Environment|string $twigOrName
*/
public function __construct(
$twigOrName,
?EngineInterface $templating,
ContainerInterface $container,
ManagerInterface $mediaManager
) {
parent::__construct($twigOrName, $templating);

$this->mediaManager = $mediaManager;
$this->container = $container;
Expand Down
15 changes: 9 additions & 6 deletions src/Resources/config/block.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,25 @@
</service>
<service id="sonata.media.block.feature_media" class="%sonata.media.block.feature_media.class%" public="true">
<tag name="sonata.block"/>
<argument>sonata.media.block.feature_media</argument>
<argument type="service" id="sonata.templating"/>
<argument type="service" id="twig"/>
<!-- NEXT_MAJOR: Remove "null" argument -->
<argument>null</argument>
<argument type="service" id="service_container"/>
<argument type="service" id="sonata.media.manager.media"/>
</service>
<service id="sonata.media.block.gallery" class="%sonata.media.block.gallery.class%" public="true">
<tag name="sonata.block"/>
<argument>sonata.media.block.gallery</argument>
<argument type="service" id="sonata.templating"/>
<argument type="service" id="twig"/>
<!-- NEXT_MAJOR: Remove "null" argument -->
<argument>null</argument>
<argument type="service" id="service_container"/>
<argument type="service" id="sonata.media.manager.gallery"/>
</service>
<service id="sonata.media.block.gallery_list" class="%sonata.media.block.gallery_list.class%" public="true">
<tag name="sonata.block"/>
<argument>sonata.media.block.gallery_list</argument>
<argument type="service" id="sonata.templating"/>
<argument type="service" id="twig"/>
<!-- NEXT_MAJOR: Remove "null" argument -->
<argument>null</argument>
<argument type="service" id="sonata.media.manager.gallery"/>
<argument type="service" id="sonata.media.pool"/>
</service>
Expand Down
4 changes: 2 additions & 2 deletions tests/Block/FeatureMediaBlockServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ protected function setUp(): void
$this->galleryManager = $this->prophesize(GalleryManagerInterface::class);

$this->blockService = new FeatureMediaBlockService(
$this->templating,
$this->templating,
$this->twig,
null,
$this->container->reveal(),
$this->galleryManager->reveal()
);
Expand Down
20 changes: 12 additions & 8 deletions tests/Block/GalleryBlockServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ protected function setUp(): void
$this->galleryManager = $this->prophesize(GalleryManagerInterface::class);

$this->blockService = new GalleryBlockService(
$this->templating,
$this->templating,
$this->twig,
null,
$this->container->reveal(),
$this->galleryManager->reveal()
);
Expand All @@ -66,13 +66,17 @@ public function testExecute(): void
$block->getSetting('galleryId')->willReturn($gallery->reveal());
$gallery->getGalleryHasMedias()->willReturn([]);

$this->blockService->execute($blockContext->reveal());
$this->twig
->expects($this->once())
->method('render')
->with('template', [
'gallery' => $gallery->reveal(),
'block' => $block->reveal(),
'elements' => [],
'settings' => ['settings'],
]);

$this->assertSame('template', $this->templating->view);
$this->assertIsArray($this->templating->parameters['settings']);
$this->assertIsArray($this->templating->parameters['elements']);
$this->assertSame($gallery->reveal(), $this->templating->parameters['gallery']);
$this->assertSame($block->reveal(), $this->templating->parameters['block']);
$this->blockService->execute($blockContext->reveal());
}

public function testDefaultSettings(): void
Expand Down
27 changes: 16 additions & 11 deletions tests/Block/GalleryListBlockServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use PHPUnit\Framework\MockObject\MockObject;
use Sonata\BlockBundle\Block\BlockContext;
use Sonata\BlockBundle\Model\Block;
use Sonata\BlockBundle\Model\BlockInterface;
use Sonata\BlockBundle\Test\BlockServiceTestCase;
use Sonata\DatagridBundle\Pager\PagerInterface;
use Sonata\MediaBundle\Block\GalleryListBlockService;
Expand Down Expand Up @@ -50,29 +49,35 @@ public function testExecute(): void

$block = new Block();

$blockContext = new BlockContext($block, [
$settings = [
'number' => 15,
'mode' => 'public',
'order' => 'createdAt',
'sort' => 'desc',
'context' => false,
'template' => '@SonataMedia/Block/block_gallery_list.html.twig',
]);
];

$blockService = new GalleryListBlockService($this->templating, $this->templating, $this->galleryManager, $this->pool);
$blockService->execute($blockContext);
$blockContext = new BlockContext($block, $settings);

$blockService = new GalleryListBlockService($this->twig, null, $this->galleryManager, $this->pool);

$this->assertSame('@SonataMedia/Block/block_gallery_list.html.twig', $this->templating->view);
$this->twig
->expects($this->once())
->method('render')
->with('@SonataMedia/Block/block_gallery_list.html.twig', [
'context' => $blockContext,
'pager' => $pager,
'block' => $block,
'settings' => $settings,
]);

$this->assertSame($blockContext, $this->templating->parameters['context']);
$this->assertIsArray($this->templating->parameters['settings']);
$this->assertInstanceOf(BlockInterface::class, $this->templating->parameters['block']);
$this->assertSame($pager, $this->templating->parameters['pager']);
$blockService->execute($blockContext);
}

public function testDefaultSettings(): void
{
$blockService = new GalleryListBlockService($this->templating, $this->templating, $this->galleryManager, $this->pool);
$blockService = new GalleryListBlockService($this->twig, null, $this->galleryManager, $this->pool);
$blockContext = $this->getBlockContext($blockService);

$this->assertSettings([
Expand Down
18 changes: 11 additions & 7 deletions tests/Block/MediaBlockServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ protected function setUp(): void
$this->galleryManager = $this->prophesize(GalleryManagerInterface::class);

$this->blockService = new MediaBlockService(
$this->templating,
$this->templating,
$this->twig,
null,
$this->container->reveal(),
$this->galleryManager->reveal()
);
Expand All @@ -71,12 +71,16 @@ public function testExecute(): void
$blockContext->getSetting('mediaId')->willReturn($media->reveal());
$block->getSetting('mediaId')->willReturn($media->reveal());

$this->blockService->execute($blockContext->reveal());
$this->twig
->expects($this->once())
->method('render')
->with('template', [
'media' => $media->reveal(),
'block' => $block->reveal(),
'settings' => [],
]);

$this->assertSame('template', $this->templating->view);
$this->assertIsArray($this->templating->parameters['settings']);
$this->assertSame($media->reveal(), $this->templating->parameters['media']);
$this->assertSame($block->reveal(), $this->templating->parameters['block']);
$this->blockService->execute($blockContext->reveal());
}

public function testDefaultSettings(): void
Expand Down

0 comments on commit 9533247

Please sign in to comment.