Skip to content

Commit

Permalink
Add deprecation for not passing empty boxes option
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet authored and greg0ire committed Nov 15, 2020
1 parent f87ea9f commit d181089
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 7 deletions.
7 changes: 7 additions & 0 deletions UPGRADE-3.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ UPGRADE 3.x
UPGRADE FROM 3.x to 3.x
=======================

### Sonata\AdminBundle\Block\AdminSearchBlockService

Not passing the `empty_boxes` option as argument 4 to `Sonata\AdminBundle\Block\AdminSearchBlockService()` is deprecated.

UPGRADE FROM 3.79 to 3.80
=========================

### Sonata\AdminBundle\Form\Type\Operator\StringOperatorType

Added "Not equal" in the default list for "choices" option in order to allow filtering by strings that are not equal to the model data.
Expand Down
32 changes: 28 additions & 4 deletions src/Block/AdminSearchBlockService.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,26 @@ class AdminSearchBlockService extends AbstractBlockService
protected $searchHandler;

/**
* NEXT_MAJOR: Change signature for (Environment $twig, Pool $pool, SearchHandler $searchHandler).
* NEXT_MAJOR: Change var to string and phpstan-var to 'show'|'hide'|'fade'.
*
* @var string|null
*
* @phpstan-var 'show'|'hide'|'fade'|null
*/
private $emptyBoxesOption;

/**
* NEXT_MAJOR: Change signature for (Environment $twig, Pool $pool, SearchHandler $searchHandler, string $emptyBoxesOption).
*
* @param Environment|string $twigOrName
* @param Pool|EngineInterface|null $poolOrTemplating
* @param SearchHandler|Pool $searchHandlerOrPool
* @param string|SearchHandler|null $emptyBoxesOptionOrSearchHandler
*
* @phpstan-param 'show'|'hide'|'fade'|SearchHandler|null $emptyBoxesOptionOrSearchHandler
* @phpstan-param 'show'|'hide'|'fade'|null $emptyBoxesOption
*/
public function __construct($twigOrName, ?object $poolOrTemplating, object $searchHandlerOrPool, ?SearchHandler $searchHandler = null)
public function __construct($twigOrName, ?object $poolOrTemplating, object $searchHandlerOrPool, $emptyBoxesOptionOrSearchHandler = null, ?string $emptyBoxesOption = null)
{
if ($poolOrTemplating instanceof Pool) {
if (!$twigOrName instanceof Environment) {
Expand All @@ -71,8 +84,17 @@ public function __construct($twigOrName, ?object $poolOrTemplating, object $sear

parent::__construct($twigOrName);

if (!\is_string($emptyBoxesOptionOrSearchHandler)) {
@trigger_error(sprintf(
'Not passing a string as argument 4 to %s() is deprecated since sonata-project/admin-bundle 3.x'
.' and will throw a \TypeError in version 4.0.',
__METHOD__
), E_USER_DEPRECATED);
}

$this->pool = $poolOrTemplating;
$this->searchHandler = $searchHandlerOrPool;
$this->emptyBoxesOption = $emptyBoxesOptionOrSearchHandler;
} elseif (null === $poolOrTemplating || $poolOrTemplating instanceof EngineInterface) {
@trigger_error(sprintf(
'Passing %s as argument 2 to %s() is deprecated since sonata-project/admin-bundle 3.76'
Expand All @@ -91,7 +113,7 @@ public function __construct($twigOrName, ?object $poolOrTemplating, object $sear
));
}

if (null === $searchHandler) {
if (null === $emptyBoxesOptionOrSearchHandler) {
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.',
Expand All @@ -104,7 +126,8 @@ public function __construct($twigOrName, ?object $poolOrTemplating, object $sear
parent::__construct($twigOrName, $poolOrTemplating);

$this->pool = $searchHandlerOrPool;
$this->searchHandler = $searchHandler;
$this->searchHandler = $emptyBoxesOptionOrSearchHandler;
$this->emptyBoxesOption = $emptyBoxesOption;
} 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.',
Expand Down Expand Up @@ -149,6 +172,7 @@ public function execute(BlockContextInterface $blockContext, ?Response $response
'admin_pool' => $this->pool,
'pager' => $pager,
'admin' => $admin,
'show_empty_boxes' => $this->emptyBoxesOption,
], $response);
}

Expand Down
1 change: 1 addition & 0 deletions src/Resources/config/block.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
new ReferenceConfigurator('twig'),
new ReferenceConfigurator('sonata.admin.pool'),
new ReferenceConfigurator('sonata.admin.search.handler'),
'%sonata.admin.configuration.global_search.empty_boxes%',
])

->set('sonata.admin.block.stats', AdminStatsBlockService::class)
Expand Down
3 changes: 2 additions & 1 deletion src/Resources/views/Block/block_search_result.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ file that was distributed with this source code.
{% extends sonata_block.templates.block_base %}

{% block block %}
{% set show_empty_boxes = sonata_admin.adminPool.container.getParameter('sonata.admin.configuration.global_search.empty_boxes') %}
{# NEXT_MAJOR: Remove the following line #}
{% set show_empty_boxes = show_empty_boxes|default(sonata_admin.adminPool.container.getParameter('sonata.admin.configuration.global_search.empty_boxes')) %}
{% set visibility_class = 'sonata-search-result-' ~ show_empty_boxes %}
{% if pager and pager.getResults()|length %}
{% set visibility_class = 'sonata-search-result-show' %}
Expand Down
34 changes: 32 additions & 2 deletions tests/Block/AdminSearchBlockServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Sonata\AdminBundle\Block\AdminSearchBlockService;
use Sonata\AdminBundle\Search\SearchHandler;
use Sonata\BlockBundle\Test\BlockServiceTestCase;
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
use Symfony\Component\HttpFoundation\Response;
use Twig\Environment;

Expand All @@ -26,6 +27,8 @@
*/
class AdminSearchBlockServiceTest extends BlockServiceTestCase
{
use ExpectDeprecationTrait;

/**
* @var Pool
*/
Expand All @@ -49,7 +52,33 @@ public function testDefaultSettings(): void
$blockService = new AdminSearchBlockService(
$this->createMock(Environment::class),
$this->pool,
$this->searchHandler
$this->searchHandler,
'show'
);
$blockContext = $this->getBlockContext($blockService);

$this->assertSettings([
'admin_code' => '',
'query' => '',
'page' => 0,
'per_page' => 10,
'icon' => '<i class="fa fa-list"></i>',
], $blockContext);
}

/**
* NEXT_MAJOR: Remove this test.
*
* @group legacy
*/
public function testDefaultSettingsWithoutEmptyBoxOption(): void
{
$this->expectDeprecation('Not passing a string as argument 4 to %s() is deprecated since sonata-project/admin-bundle 3.x and will throw a \TypeError in version 4.0.');

$blockService = new AdminSearchBlockService(
$this->createMock(Environment::class),
$this->pool,
$this->searchHandler,
);
$blockContext = $this->getBlockContext($blockService);

Expand All @@ -69,7 +98,8 @@ public function testGlobalSearchReturnsEmptyWhenFiltersAreDisabled(): void
$blockService = new AdminSearchBlockService(
$this->createMock(Environment::class),
$this->pool,
$this->searchHandler
$this->searchHandler,
'show'
);
$blockContext = $this->getBlockContext($blockService);

Expand Down

0 comments on commit d181089

Please sign in to comment.