Skip to content

Commit

Permalink
Prepare support for block 4 (#6220)
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet authored Jul 28, 2020
1 parent 2118ef7 commit 4e250f9
Show file tree
Hide file tree
Showing 10 changed files with 425 additions and 31 deletions.
66 changes: 59 additions & 7 deletions src/Block/AdminListBlockService.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,72 @@ class AdminListBlockService extends AbstractBlockService
private $templateRegistry;

/**
* NEXT_MAJOR: Remove `$templating` argument.
* NEXT_MAJOR: Change signature for (Environment $twig, Pool $pool, ?TemplateRegistryInterface $templateRegistry = null).
*
* @param Environment|string $twigOrName
* @param Environment|string $twigOrName
* @param EngineInterface|Pool|null $poolOrTemplating
* @param Pool|TemplateRegistryInterface|null $templateRegistryOrPool
*/
public function __construct(
$twigOrName,
?EngineInterface $templating,
Pool $pool,
?object $poolOrTemplating,
?object $templateRegistryOrPool,
?TemplateRegistryInterface $templateRegistry = null
) {
parent::__construct($twigOrName, $templating);
if ($poolOrTemplating instanceof Pool) {
if (!$twigOrName instanceof Environment) {
throw new \TypeError(sprintf(
'Argument 1 passed to %s() must be an instance of %s, %s given.',
__METHOD__,
Environment::class,
\is_object($twigOrName) ? 'instance of '.\get_class($twigOrName) : \gettype($twigOrName)
));
}

if (null !== $templateRegistryOrPool && !$templateRegistryOrPool instanceof TemplateRegistryInterface) {
throw new \TypeError(sprintf(
'Argument 3 passed to %s() must be either null or an instance of %s, %s given.',
__METHOD__,
TemplateRegistryInterface::class,
\is_object($twigOrName) ? 'instance of '.\get_class($twigOrName) : \gettype($twigOrName)
));
}

parent::__construct($twigOrName);

$this->pool = $poolOrTemplating;
$this->templateRegistry = $templateRegistryOrPool ?: new TemplateRegistry();
} elseif (null === $poolOrTemplating || $poolOrTemplating instanceof EngineInterface) {
@trigger_error(sprintf(
'Passing %s as argument 2 to %s() is deprecated since sonata-project/admin-bundle 3.x'
.' and will throw a \TypeError in version 4.0. You must pass an instance of %s instead.',
null === $poolOrTemplating ? 'null' : EngineInterface::class,
__METHOD__,
Pool::class
), E_USER_DEPRECATED);

$this->pool = $pool;
$this->templateRegistry = $templateRegistry ?: new TemplateRegistry();
if (!$templateRegistryOrPool instanceof Pool) {
throw new \TypeError(sprintf(
'Argument 2 passed to %s() must be an instance of %s, %s given.',
__METHOD__,
Pool::class,
null === $templateRegistryOrPool ? 'null' : 'instance of '.\get_class($templateRegistryOrPool)
));
}

parent::__construct($twigOrName, $poolOrTemplating);

$this->pool = $templateRegistryOrPool;
$this->templateRegistry = $templateRegistry ?: new TemplateRegistry();
} else {
throw new \TypeError(sprintf(
'Argument 2 passed to %s() must be either null or an instance of %s or preferably %s, instance of %s given.',
__METHOD__,
EngineInterface::class,
Pool::class,
\get_class($poolOrTemplating)
));
}
}

public function execute(BlockContextInterface $blockContext, ?Response $response = null)
Expand Down
76 changes: 69 additions & 7 deletions src/Block/AdminSearchBlockService.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,78 @@ class AdminSearchBlockService extends AbstractBlockService
protected $searchHandler;

/**
* NEXT_MAJOR: Remove `$templating` argument.
* NEXT_MAJOR: Change signature for (Environment $twig, Pool $pool, SearchHandler $searchHandler).
*
* @param Environment|string $twigOrName
* @param Environment|string $twigOrName
* @param Pool|EngineInterface|null $poolOrTemplating
* @param SearchHandler|Pool $searchHandlerOrPool
*/
public function __construct($twigOrName, ?EngineInterface $templating, Pool $pool, SearchHandler $searchHandler)
public function __construct($twigOrName, ?object $poolOrTemplating, object $searchHandlerOrPool, ?SearchHandler $searchHandler = null)
{
parent::__construct($twigOrName, $templating);

$this->pool = $pool;
$this->searchHandler = $searchHandler;
if ($poolOrTemplating instanceof Pool) {
if (!$twigOrName instanceof Environment) {
throw new \TypeError(sprintf(
'Argument 1 passed to %s() must be an instance of %s, %s given.',
__METHOD__,
Environment::class,
\is_object($twigOrName) ? 'instance of '.\get_class($twigOrName) : \gettype($twigOrName)
));
}

if (!$searchHandlerOrPool instanceof SearchHandler) {
throw new \TypeError(sprintf(
'Argument 3 passed to %s() must be an instance of %s, instance of %s given.',
__METHOD__,
SearchHandler::class,
\get_class($twigOrName)
));
}

parent::__construct($twigOrName);

$this->pool = $poolOrTemplating;
$this->searchHandler = $searchHandlerOrPool;
} elseif (null === $poolOrTemplating || $poolOrTemplating instanceof EngineInterface) {
@trigger_error(sprintf(
'Passing %s as argument 2 to %s() is deprecated since sonata-project/admin-bundle 3.x'
.' and will throw a \TypeError in version 4.0. You must pass an instance of %s instead.',
null === $poolOrTemplating ? 'null' : EngineInterface::class,
__METHOD__,
Pool::class
), E_USER_DEPRECATED);

if (!$searchHandlerOrPool instanceof Pool) {
throw new \TypeError(sprintf(
'Argument 2 passed to %s() must be an instance of %s, instance of %s given.',
__METHOD__,
Pool::class,
\get_class($twigOrName)
));
}

if (null === $searchHandler) {
throw new \TypeError(sprintf(
'Passing null as argument 3 to %s() is not allowed when %s is passed as argument 2.'
.' You must pass an instance of %s instead.',
__METHOD__,
EngineInterface::class,
SearchHandler::class
));
}

parent::__construct($twigOrName, $poolOrTemplating);

$this->pool = $searchHandlerOrPool;
$this->searchHandler = $searchHandler;
} else {
throw new \TypeError(sprintf(
'Argument 2 passed to %s() must be either null or an instance of %s or preferably %s, instance of %s given.',
__METHOD__,
EngineInterface::class,
Pool::class,
\get_class($poolOrTemplating)
));
}
}

public function execute(BlockContextInterface $blockContext, ?Response $response = null)
Expand Down
54 changes: 48 additions & 6 deletions src/Block/AdminStatsBlockService.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,57 @@ class AdminStatsBlockService extends AbstractBlockService
protected $pool;

/**
* NEXT_MAJOR: Remove `$templating` argument.
* NEXT_MAJOR: Change signature for (Environment $twig, Pool $pool).
*
* @param Environment|string $twigOrName
* @param Environment|string $twigOrName
* @param Pool|EngineInterface|null $poolOrTemplating
*/
public function __construct($twigOrName, ?EngineInterface $templating, Pool $pool)
public function __construct($twigOrName, ?object $poolOrTemplating, ?Pool $pool = null)
{
parent::__construct($twigOrName, $templating);

$this->pool = $pool;
if ($poolOrTemplating instanceof Pool) {
if (!$twigOrName instanceof Environment) {
throw new \TypeError(sprintf(
'Argument 1 passed to %s() must be an instance of %s, %s given.',
__METHOD__,
Environment::class,
\is_object($twigOrName) ? 'instance of '.\get_class($twigOrName) : \gettype($twigOrName)
));
}

parent::__construct($twigOrName);

$this->pool = $poolOrTemplating;
} elseif (null === $poolOrTemplating || $poolOrTemplating instanceof EngineInterface) {
@trigger_error(sprintf(
'Passing %s as argument 2 to %s() is deprecated since sonata-project/admin-bundle 3.x'
.' and will throw a \TypeError in version 4.0. You must pass an instance of %s instead.',
null === $poolOrTemplating ? 'null' : EngineInterface::class,
__METHOD__,
Pool::class
), E_USER_DEPRECATED);

if (null === $pool) {
throw new \TypeError(sprintf(
'Passing null as argument 3 to %s() is not allowed when %s is passed as argument 2.'
.' You must pass an instance of %s instead.',
__METHOD__,
EngineInterface::class,
Pool::class
));
}

parent::__construct($twigOrName, $poolOrTemplating);

$this->pool = $pool;
} else {
throw new \TypeError(sprintf(
'Argument 2 passed to %s() must be either null or an instance of %s or preferably %s, instance of %s given.',
__METHOD__,
EngineInterface::class,
Pool::class,
\get_class($poolOrTemplating)
));
}
}

public function execute(BlockContextInterface $blockContext, ?Response $response = null)
Expand Down
6 changes: 0 additions & 6 deletions src/Resources/config/block.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,18 @@
<service id="sonata.admin.block.admin_list" class="Sonata\AdminBundle\Block\AdminListBlockService" public="true">
<tag name="sonata.block"/>
<argument type="service" id="twig"/>
<!-- NEXT_MAJOR: Remove "null" argument -->
<argument>null</argument>
<argument type="service" id="sonata.admin.pool"/>
<argument type="service" id="sonata.admin.global_template_registry"/>
</service>
<service id="sonata.admin.block.search_result" class="Sonata\AdminBundle\Block\AdminSearchBlockService" public="true">
<tag name="sonata.block"/>
<argument type="service" id="twig"/>
<!-- NEXT_MAJOR: Remove "null" argument -->
<argument>null</argument>
<argument type="service" id="sonata.admin.pool"/>
<argument type="service" id="sonata.admin.search.handler"/>
</service>
<service id="sonata.admin.block.stats" class="Sonata\AdminBundle\Block\AdminStatsBlockService" public="true">
<tag name="sonata.block"/>
<argument type="service" id="twig"/>
<!-- NEXT_MAJOR: Remove "null" argument -->
<argument>null</argument>
<argument type="service" id="sonata.admin.pool"/>
</service>
</services>
Expand Down
2 changes: 0 additions & 2 deletions tests/Block/AdminListBlockServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ public function testDefaultSettings(): void
{
$blockService = new AdminListBlockService(
$this->createMock(Environment::class),
null,
$this->pool,
$this->templateRegistry->reveal()
);
Expand All @@ -66,7 +65,6 @@ public function testOverriddenDefaultSettings(): void
{
$blockService = new FakeBlockService(
$this->createMock(Environment::class),
null,
$this->pool,
$this->templateRegistry->reveal()
);
Expand Down
2 changes: 0 additions & 2 deletions tests/Block/AdminSearchBlockServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ public function testDefaultSettings(): void
{
$blockService = new AdminSearchBlockService(
$this->createMock(Environment::class),
null,
$this->pool,
$this->searchHandler
);
Expand All @@ -69,7 +68,6 @@ public function testGlobalSearchReturnsEmptyWhenFiltersAreDisabled(): void

$blockService = new AdminSearchBlockService(
$this->createMock(Environment::class),
null,
$this->pool,
$this->searchHandler
);
Expand Down
1 change: 0 additions & 1 deletion tests/Block/AdminStatsBlockServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ public function testDefaultSettings(): void
{
$blockService = new AdminStatsBlockService(
$this->createMock(Environment::class),
null,
$this->pool
);
$blockContext = $this->getBlockContext($blockService);
Expand Down
87 changes: 87 additions & 0 deletions tests/Block/DeprecatedAdminListBlockServiceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?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\Tests\Block;

use Sonata\AdminBundle\Admin\Pool;
use Sonata\AdminBundle\Block\AdminListBlockService;
use Sonata\AdminBundle\Templating\TemplateRegistryInterface;
use Sonata\AdminBundle\Tests\Fixtures\Block\FakeBlockService;
use Sonata\BlockBundle\Test\BlockServiceTestCase;
use Twig\Environment;

/**
* NEXT_MAJOR: Remove this class.
*
* @group legacy
*
* @author Sullivan Senechal <[email protected]>
*/
class DeprecatedAdminListBlockServiceTest extends BlockServiceTestCase
{
/**
* @var Pool
*/
private $pool;

/**
* @var TemplateRegistryInterface
*/
private $templateRegistry;

protected function setUp(): void
{
parent::setUp();

$this->pool = $this->createMock(Pool::class);

$this->templateRegistry = $this->prophesize(TemplateRegistryInterface::class);
}

/**
* @expectedDeprecation Passing null as argument 2 to Sonata\AdminBundle\Block\AdminListBlockService::__construct() is deprecated since sonata-project/admin-bundle 3.x and will throw a \TypeError in version 4.0. You must pass an instance of Sonata\AdminBundle\Admin\Pool instead.
*/
public function testDefaultSettings(): void
{
$blockService = new AdminListBlockService(
$this->createMock(Environment::class),
null,
$this->pool,
$this->templateRegistry->reveal()
);
$blockContext = $this->getBlockContext($blockService);

$this->assertSettings([
'groups' => false,
], $blockContext);
}

/**
* @expectedDeprecation Passing null as argument 2 to Sonata\AdminBundle\Block\AdminListBlockService::__construct() is deprecated since sonata-project/admin-bundle 3.x and will throw a \TypeError in version 4.0. You must pass an instance of Sonata\AdminBundle\Admin\Pool instead.
*/
public function testOverriddenDefaultSettings(): void
{
$blockService = new FakeBlockService(
$this->createMock(Environment::class),
null,
$this->pool,
$this->templateRegistry->reveal()
);
$blockContext = $this->getBlockContext($blockService);

$this->assertSettings([
'foo' => 'bar',
'groups' => true,
], $blockContext);
}
}
Loading

0 comments on commit 4e250f9

Please sign in to comment.