Skip to content

Commit

Permalink
Deprecate EntryChoice and reduce dependency on utils bundle (#25)
Browse files Browse the repository at this point in the history
* started removing utils bundle

* updated tests
  • Loading branch information
koertho committed Jan 24, 2023
1 parent ebbd55e commit 7f22ee4
Show file tree
Hide file tree
Showing 8 changed files with 189 additions and 59 deletions.
4 changes: 4 additions & 0 deletions src/Choice/EntryChoice.php
Expand Up @@ -12,6 +12,10 @@
use HeimrichHannot\EncoreBundle\Collection\EntryCollection;
use HeimrichHannot\UtilsBundle\Choice\AbstractChoice;

/**
* @deprecated use EncoreEntryOptionListener instead
* @codeCoverageIgnore
*/
class EntryChoice extends AbstractChoice
{
private EntryCollection $entryCollection;
Expand Down
23 changes: 13 additions & 10 deletions src/DataContainer/LayoutContainer.php
Expand Up @@ -9,34 +9,37 @@
namespace HeimrichHannot\EncoreBundle\DataContainer;

use Contao\CoreBundle\Framework\ContaoFramework;
use Contao\CoreBundle\Routing\ScopeMatcher;
use Contao\DataContainer;
use Contao\LayoutModel;
use Contao\Message;
use HeimrichHannot\UtilsBundle\Util\Utils;
use Symfony\Component\HttpFoundation\RequestStack;

class LayoutContainer
{
protected array $bundleConfig;
protected ContaoFramework $contaoFramework;
private Utils $utils;
private RequestStack $requestStack;
private ScopeMatcher $scopeMatcher;

/**
* LayoutContainer constructor.
*/
public function __construct(array $bundleConfig, Utils $utils, ContaoFramework $contaoFramework)
public function __construct(array $bundleConfig, ContaoFramework $contaoFramework, RequestStack $requestStack, ScopeMatcher $scopeMatcher)
{
$this->bundleConfig = $bundleConfig;
$this->contaoFramework = $contaoFramework;
$this->utils = $utils;
$this->requestStack = $requestStack;
$this->scopeMatcher = $scopeMatcher;
}

/**
* @param DataContainer|null $dc
*/
public function onLoadCallback($dc): void
public function onLoadCallback(DataContainer $dc = null): void
{
if (!$dc
|| !$this->utils->container()->isBackend()
$request = $this->requestStack->getCurrentRequest();

if (!$request
|| !$dc
|| !$this->scopeMatcher->isBackendRequest($request)
|| !isset($this->bundleConfig['use_contao_template_variables'])
|| true !== $this->bundleConfig['use_contao_template_variables']
|| !($layout = $this->contaoFramework->getAdapter(LayoutModel::class)->findByPk($dc->id))) {
Expand Down
3 changes: 2 additions & 1 deletion src/Dca/DcaGenerator.php
Expand Up @@ -8,6 +8,7 @@

namespace HeimrichHannot\EncoreBundle\Dca;

use HeimrichHannot\EncoreBundle\EventListener\Callback\EncoreEntryOptionListener;
use Symfony\Contracts\Translation\TranslatorInterface;

class DcaGenerator
Expand Down Expand Up @@ -42,7 +43,7 @@ public function getEncoreEntriesSelect(bool $includeActiveCheckbox = false): arr
'exclude' => true,
'filter' => true,
'inputType' => 'select',
'options_callback' => ['huh.encore.choice.entry', 'getCachedChoices'],
'options_callback' => [EncoreEntryOptionListener::class, 'getEntriesAsOptions'],
'eval' => ['tl_class' => 'w50', 'mandatory' => true, 'includeBlankOption' => true, 'groupStyle' => 'width: 710px', 'chosen' => true],
],
],
Expand Down
40 changes: 40 additions & 0 deletions src/EventListener/Callback/EncoreEntryOptionListener.php
@@ -0,0 +1,40 @@
<?php

/*
* Copyright (c) 2022 Heimrich & Hannot GmbH
*
* @license LGPL-3.0-or-later
*/

namespace HeimrichHannot\EncoreBundle\EventListener\Callback;

use HeimrichHannot\EncoreBundle\Collection\EntryCollection;

class EncoreEntryOptionListener
{
private EntryCollection $entryCollection;

public function __construct(EntryCollection $entryCollection)
{
$this->entryCollection = $entryCollection;
}

public function getEntriesAsOptions(): array
{
$choices = [];

$projectEntries = $this->entryCollection->getEntries();

if (empty($projectEntries)) {
return $choices;
}

foreach ($projectEntries as $entry) {
$choices[$entry['name']] = $entry['name'].(isset($entry['file']) ? ' ['.$entry['file'].']' : '');
}

asort($choices);

return $choices;
}
}
6 changes: 4 additions & 2 deletions src/Resources/contao/dca/tl_layout.php
@@ -1,11 +1,13 @@
<?php

/*
* Copyright (c) 2021 Heimrich & Hannot GmbH
* Copyright (c) 2022 Heimrich & Hannot GmbH
*
* @license LGPL-3.0-or-later
*/

use HeimrichHannot\EncoreBundle\EventListener\Callback\EncoreEntryOptionListener;

$dca = &$GLOBALS['TL_DCA']['tl_layout'];

/*
Expand Down Expand Up @@ -69,7 +71,7 @@
'exclude' => true,
'filter' => true,
'inputType' => 'select',
'options_callback' => ['huh.encore.choice.entry', 'getCachedChoices'],
'options_callback' => [EncoreEntryOptionListener::class, 'getEntriesAsOptions'],
'eval' => ['tl_class' => 'w50', 'mandatory' => true, 'includeBlankOption' => true, 'groupStyle' => 'width: 710px', 'chosen' => true],
],
],
Expand Down
56 changes: 31 additions & 25 deletions src/Resources/contao/dca/tl_page.php
@@ -1,8 +1,16 @@
<?php

/*
* Copyright (c) 2022 Heimrich & Hannot GmbH
*
* @license LGPL-3.0-or-later
*/

use HeimrichHannot\EncoreBundle\EventListener\Callback\EncoreEntryOptionListener;

$dca = &$GLOBALS['TL_DCA']['tl_page'];

/**
/*
* Palettes
*/
$dca['palettes']['__selector__'][] = 'addEncore';
Expand All @@ -12,42 +20,40 @@
$dca['palettes'][$palette] = str_replace(';{layout_legend', ';{encore_legend},encoreEntries;{layout_legend', $dca['palettes'][$palette]);
}



/**
* Fields
* Fields.
*/
$fields = [
'encoreEntries' => [
'label' => &$GLOBALS['TL_LANG']['tl_page']['encoreEntries'],
'exclude' => true,
'encoreEntries' => [
'label' => &$GLOBALS['TL_LANG']['tl_page']['encoreEntries'],
'exclude' => true,
'inputType' => 'multiColumnEditor',
'eval' => [
'tl_class' => 'long clr',
'eval' => [
'tl_class' => 'long clr',
'multiColumnEditor' => [
'minRowCount' => 0,
'sortable' => true,
'fields' => [
'sortable' => true,
'fields' => [
'active' => [
'label' => &$GLOBALS['TL_LANG']['tl_page']['encoreEntries_active'],
'exclude' => true,
'default' => true,
'label' => &$GLOBALS['TL_LANG']['tl_page']['encoreEntries_active'],
'exclude' => true,
'default' => true,
'inputType' => 'checkbox',
'eval' => ['tl_class' => 'w50', 'groupStyle' => 'width: 65px']
'eval' => ['tl_class' => 'w50', 'groupStyle' => 'width: 65px'],
],
'entry' => [
'label' => &$GLOBALS['TL_LANG']['tl_page']['encoreEntries_entry'],
'exclude' => true,
'filter' => true,
'inputType' => 'select',
'options_callback' => [EncoreEntryOptionListener::class, 'getEntriesAsOptions'],
'eval' => ['tl_class' => 'w50', 'mandatory' => true, 'includeBlankOption' => true, 'groupStyle' => 'width: 710px', 'chosen' => true],
],
'entry' => [
'label' => &$GLOBALS['TL_LANG']['tl_page']['encoreEntries_entry'],
'exclude' => true,
'filter' => true,
'inputType' => 'select',
'options_callback' => ['huh.encore.choice.entry', 'getCachedChoices'],
'eval' => ['tl_class' => 'w50', 'mandatory' => true, 'includeBlankOption' => true, 'groupStyle' => 'width: 710px', 'chosen' => true]
]
],
],
],
'sql' => "blob NULL",
'sql' => 'blob NULL',
],
];

$dca['fields'] = array_merge(is_array($dca['fields']) ? $dca['fields'] : [], $fields);
$dca['fields'] = array_merge(is_array($dca['fields']) ? $dca['fields'] : [], $fields);
75 changes: 54 additions & 21 deletions tests/DataContainer/LayoutContainerTest.php
Expand Up @@ -9,20 +9,36 @@
namespace HeimrichHannot\EncoreBundle\Test\DataContainer;

use Contao\CoreBundle\Framework\ContaoFramework;
use Contao\CoreBundle\Routing\ScopeMatcher;
use Contao\DataContainer;
use Contao\LayoutModel;
use Contao\Message;
use Contao\TestCase\ContaoTestCase;
use HeimrichHannot\EncoreBundle\DataContainer\LayoutContainer;
use HeimrichHannot\UtilsBundle\Util\Utils;
use PHPUnit\Framework\MockObject\MockObject;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;

class LayoutContainerTest extends ContaoTestCase
{
public function createTestInstance(array $parameters = []): LayoutContainer
{
$bundleConfig = $parameters['bundleConfig'] ?? [];
$contaoFramework = $parameters['contaoFramework'] ?? $this->mockContaoFramework();
$requestStack = $parameters['requestStack'] ?? $this->createMock(RequestStack::class);
$scopeMatcher = $parameters['scopeMatcher'] ?? $this->createMock(ScopeMatcher::class);

return new LayoutContainer(
$bundleConfig,
$contaoFramework,
$requestStack,
$scopeMatcher
);
}

public function testOnLoadCallback()
{
$GLOBALS['TL_LANG']['tl_layout']['INFO']['jquery_order_conflict'] = 'Info';
$bundleConfig = [];

$messageAdapter = $this->mockAdapter(['addInfo']);
$messageAdapter->expects($this->never())->method('addInfo')->willReturn(null);
Expand All @@ -32,35 +48,42 @@ public function testOnLoadCallback()
Message::class => $messageAdapter,
]);

$utils = $this->createMock(Utils::class);
$instance = $this->createTestInstance([
'contaoFramework' => $contaoFramework,
]);
$instance->onLoadCallback(null);

$instance = new LayoutContainer($bundleConfig, $utils, $contaoFramework);
$requestStack = $this->createMock(RequestStack::class);
$requestStack->method('getCurrentRequest')->willReturn(new Request());

$instance = $this->createTestInstance([
'contaoFramework' => $contaoFramework,
'requestStack' => $requestStack,
]);
$instance->onLoadCallback(null);

$dc = $this->mockClassWithProperties(DataContainer::class, ['id' => 1]);
$instance->onLoadCallback($dc);

$instance = new LayoutContainer($bundleConfig, $utils, $contaoFramework);
$instance->onLoadCallback($dc);

$bundleConfig['use_contao_template_variables'] = false;
$instance = new LayoutContainer($bundleConfig, $utils, $contaoFramework);
$instance->onLoadCallback($dc);

$bundleConfig['use_contao_template_variables'] = true;
$instance = new LayoutContainer($bundleConfig, $utils, $contaoFramework);
$instance = $this->createTestInstance([
'contaoFramework' => $contaoFramework,
'bundleConfig' => $bundleConfig,
'requestStack' => $requestStack,
]);
$instance->onLoadCallback($dc);

$instance = new LayoutContainer($bundleConfig, $utils, $contaoFramework);
$bundleConfig['use_contao_template_variables'] = true;
$instance = $this->createTestInstance([
'contaoFramework' => $contaoFramework,
'bundleConfig' => $bundleConfig,
'requestStack' => $requestStack,
]);
$instance->onLoadCallback($dc);

$utils = $this->createMock(Utils::class);
$utils->method('container')->willReturnCallback(function () {
$container = $this->createMock(\HeimrichHannot\UtilsBundle\Util\Container\ContainerUtil::class);
$container->method('isBackend')->willReturn(true);

return $container;
});
$scopeMatcher = $this->createMock(ScopeMatcher::class);
$scopeMatcher->method('isBackendRequest')->willReturn(true);

$layoutModel = $this->mockAdapter(['findByPk']);
$layoutModel->method('findByPk')->willReturn($this->mockClassWithProperties(LayoutModel::class, [
Expand All @@ -75,7 +98,12 @@ public function testOnLoadCallback()
Message::class => $messageAdapter,
LayoutModel::class => $layoutModel,
]);
$instance = new LayoutContainer($bundleConfig, $utils, $contaoFramework);
$instance = $this->createTestInstance([
'contaoFramework' => $contaoFramework,
'bundleConfig' => $bundleConfig,
'scopeMatcher' => $scopeMatcher,
'requestStack' => $requestStack,
]);
$instance->onLoadCallback($dc);

$bundleConfig['unset_jquery'] = true;
Expand All @@ -86,7 +114,12 @@ public function testOnLoadCallback()
Message::class => $messageAdapter,
LayoutModel::class => $layoutModel,
]);
$instance = new LayoutContainer($bundleConfig, $utils, $contaoFramework);
$instance = $this->createTestInstance([
'contaoFramework' => $contaoFramework,
'bundleConfig' => $bundleConfig,
'scopeMatcher' => $scopeMatcher,
'requestStack' => $requestStack,
]);
$instance->onLoadCallback($dc);
}
}
41 changes: 41 additions & 0 deletions tests/EventListener/Callback/EncoreEntryOptionListenerTest.php
@@ -0,0 +1,41 @@
<?php

/*
* Copyright (c) 2022 Heimrich & Hannot GmbH
*
* @license LGPL-3.0-or-later
*/

namespace HeimrichHannot\EncoreBundle\Test\EventListener\Callback;

use Contao\TestCase\ContaoTestCase;
use HeimrichHannot\EncoreBundle\Collection\EntryCollection;
use HeimrichHannot\EncoreBundle\EventListener\Callback\EncoreEntryOptionListener;

class EncoreEntryOptionListenerTest extends ContaoTestCase
{
public function createTestInstance(array $parameters = []): EncoreEntryOptionListener
{
$entryCollection = $parameters['entryCollection'] ?? $this->createMock(EntryCollection::class);

return new EncoreEntryOptionListener($entryCollection);
}

public function testGetEntriesAsOptions()
{
$instance = $this->createTestInstance();
$this->assertEmpty($instance->getEntriesAsOptions());

$entryCollection = $this->createMock(EntryCollection::class);
$entryCollection->method('getEntries')->willReturn([
['name' => 'hello'],
['name' => 'abcd', 'file' => 'abcd.js'],
]);

$instance = $this->createTestInstance(['entryCollection' => $entryCollection]);
$this->assertSame([
'abcd' => 'abcd [abcd.js]',
'hello' => 'hello',
], $instance->getEntriesAsOptions());
}
}

0 comments on commit 7f22ee4

Please sign in to comment.