From 06a7eb535269bc2954ecfa1ca778dc10767182de Mon Sep 17 00:00:00 2001 From: Ryan Weaver Date: Thu, 25 May 2023 20:19:06 -0400 Subject: [PATCH] Updates for WebpackEncoreBundle 2.0 --- .github/workflows/ci.yml | 19 +- .github/workflows/static.yml | 10 +- CHANGELOG.md | 5 + composer.json | 22 +- doc/index.rst | 6 +- phpstan-baseline.neon | 10 - psalm.baseline.xml | 45 -- psalm.xml | 1 - src/Asset/EntrypointLookup.php | 4 +- src/Asset/TagRenderer.php | 21 +- .../Compiler/RemoveStimulusServicesPass.php | 24 - src/DependencyInjection/Configuration.php | 2 +- src/Dto/AbstractStimulusDto.php | 67 --- src/Dto/StimulusActionsDto.php | 56 --- src/Dto/StimulusControllersDto.php | 88 ---- src/Dto/StimulusTargetsDto.php | 47 -- .../PreLoadAssetsEventListener.php | 5 +- .../ResetAssetsEventListener.php | 8 +- src/Resources/config/services.xml | 4 - src/Twig/StimulusTwigExtension.php | 169 ------- src/WebpackEncoreBundle.php | 6 - tests/Dto/StimulusActionsDtoTest.php | 42 -- tests/Dto/StimulusControllersDtoTest.php | 54 --- tests/Dto/StimulusTargetsDtoTest.php | 42 -- tests/IntegrationTest.php | 439 +----------------- 25 files changed, 48 insertions(+), 1148 deletions(-) delete mode 100644 psalm.baseline.xml delete mode 100644 src/DependencyInjection/Compiler/RemoveStimulusServicesPass.php delete mode 100644 src/Dto/AbstractStimulusDto.php delete mode 100644 src/Dto/StimulusActionsDto.php delete mode 100644 src/Dto/StimulusControllersDto.php delete mode 100644 src/Dto/StimulusTargetsDto.php delete mode 100644 src/Twig/StimulusTwigExtension.php delete mode 100644 tests/Dto/StimulusActionsDtoTest.php delete mode 100644 tests/Dto/StimulusControllersDtoTest.php delete mode 100644 tests/Dto/StimulusTargetsDtoTest.php diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 105cafc8..e4f27ccd 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -15,17 +15,13 @@ jobs: fail-fast: false matrix: # normal, highest, non-dev installs - php-version: ['7.1', '7.2', '7.3', '7.4', '8.0', '8.1'] + php-version: ['8.1', '8.2'] dependency-versions: ['highest'] include: # testing lowest PHP version with lowest dependencies - - php-version: '7.1' + - php-version: '8.1' dependency-versions: 'lowest' - # testing dev versions with highest PHP - - php-version: '8.0' - dependency-versions: 'highest' - steps: - name: "Checkout code" uses: "actions/checkout@v2" @@ -51,15 +47,4 @@ jobs: composer-options: "--prefer-dist --no-progress" - name: Run tests - env: - SYMFONY_DEPRECATIONS_HELPER: "max[self]=11" - SYMFONY_PHPUNIT_VERSION: "8.5" - run: ./vendor/bin/simple-phpunit - if: matrix.php-version != '7.1' - - - name: Run tests - env: - SYMFONY_DEPRECATIONS_HELPER: "max[self]=0" - SYMFONY_PHPUNIT_VERSION: "7.5" run: ./vendor/bin/simple-phpunit - if: matrix.php-version == '7.1' diff --git a/.github/workflows/static.yml b/.github/workflows/static.yml index a681edd5..6f9b5a8c 100644 --- a/.github/workflows/static.yml +++ b/.github/workflows/static.yml @@ -13,7 +13,7 @@ jobs: - name: Setup PHP uses: shivammathur/setup-php@v2 with: - php-version: 8.0 + php-version: 8.1 coverage: none tools: cs2pr @@ -39,7 +39,7 @@ jobs: - name: Setup PHP uses: shivammathur/setup-php@v2 with: - php-version: 8.0 + php-version: 8.1 coverage: none tools: cs2pr @@ -49,7 +49,7 @@ jobs: composer-options: "--working-dir=tools/php-cs-fixer" - name: PHP-CS-Fixer - run: tools/php-cs-fixer/vendor/bin/php-cs-fixer fix --dry-run + run: tools/php-cs-fixer/vendor/bin/php-cs-fixer fix --dry-run --diff psalm: name: Psalm @@ -61,7 +61,7 @@ jobs: - name: Setup PHP uses: shivammathur/setup-php@v2 with: - php-version: 8.0 + php-version: 8.1 coverage: none tools: vimeo/psalm:4.11.2 @@ -84,7 +84,7 @@ jobs: - name: Setup PHP uses: shivammathur/setup-php@v2 with: - php-version: 8.0 + php-version: 8.1 coverage: none tools: composer-normalize diff --git a/CHANGELOG.md b/CHANGELOG.md index ae65c9b3..581ae335 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog +## v2.0.0 + +- Minimum PHP version is now 8.1 +- Minimum Symfony version is now 5.4 + ## v1.17.0 - Deprecated the `stimulus_controller()`, `stimulus_action()` and `stimulus_target` diff --git a/composer.json b/composer.json index 4f282158..92f812eb 100644 --- a/composer.json +++ b/composer.json @@ -10,20 +10,18 @@ } ], "require": { - "php": ">=7.1.3", - "symfony/asset": "^4.4 || ^5.0 || ^6.0", - "symfony/config": "^4.4 || ^5.0 || ^6.0", - "symfony/dependency-injection": "^4.4 || ^5.0 || ^6.0", - "symfony/deprecation-contracts": "^2.1 || ^3.0", - "symfony/http-kernel": "^4.4 || ^5.0 || ^6.0", - "symfony/polyfill-php80": "^1.25.0", - "symfony/service-contracts": "^1.0 || ^2.0 || ^3.0" + "php": ">=8.1.0", + "symfony/asset": "^5.4 || ^6.2", + "symfony/config": "^5.4 || ^6.2", + "symfony/dependency-injection": "^5.4 || ^6.2", + "symfony/http-kernel": "^5.4 || ^6.2", + "symfony/service-contracts": "^3.0" }, "require-dev": { - "symfony/framework-bundle": "^4.4 || ^5.0 || ^6.0", - "symfony/phpunit-bridge": "^5.3 || ^6.0", - "symfony/twig-bundle": "^4.4 || ^5.0 || ^6.0", - "symfony/web-link": "^4.4 || ^5.0 || ^6.0" + "symfony/framework-bundle": "^5.4 || ^6.2", + "symfony/phpunit-bridge": "^5.4 || ^6.2", + "symfony/twig-bundle": "^5.4 || ^6.2", + "symfony/web-link": "^5.4 || ^6.2" }, "minimum-stability": "dev", "autoload": { diff --git a/doc/index.rst b/doc/index.rst index 84879d2d..96ee8a32 100644 --- a/doc/index.rst +++ b/doc/index.rst @@ -204,9 +204,9 @@ different ways: Stimulus / Symfony UX Helper ---------------------------- -The ``stimulus_controller()``, ``stimulus_action()`` and ``stimulus_target()`` -Twig functions are deprecated in WebpackEncoreBundle 1.17.0. Install and -use `symfony/stimulus-bundle`_ for that functionality. +Version 1 of this bundle came with ``stimulus_controller()``, +``stimulus_action()`` and ``stimulus_target()`` Twig functions. These have been +removed: use `symfony/stimulus-bundle`_ instead. .. _`Webpack Encore`: https://symfony.com/doc/current/frontend.html .. _`enable the bundle manually`: https://symfony.com/doc/current/bundles.html diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index e92191c3..fbb37f59 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -9,13 +9,3 @@ parameters: message: "#^Call to an undefined method Symfony\\\\Component\\\\Config\\\\Definition\\\\Builder\\\\TreeBuilder\\:\\:root\\(\\)\\.$#" count: 1 path: src/DependencyInjection/Configuration.php - - - - message: "#^Function twig_escape_filter not found\\.$#" - count: 1 - path: src/Dto/AbstractStimulusDto.php - - - - message: "#^Call to function is_array\\(\\) with string will always evaluate to false\\.$#" - count: 2 - path: src/Twig/StimulusTwigExtension.php diff --git a/psalm.baseline.xml b/psalm.baseline.xml deleted file mode 100644 index a111f4d7..00000000 --- a/psalm.baseline.xml +++ /dev/null @@ -1,45 +0,0 @@ - - - - - method_exists($treeBuilder, 'getRootNode') - - - root - - - - - twig_escape_filter($this->env, $value, 'html_attr') - - - - - [$possibleEventName => $controllerAction] - - - \is_string($controllerAction) - \is_string($controllerAction) - \is_string($controllerActions) - \is_string($possibleEventName) - \is_string($possibleEventName) && \is_string($controllerAction) - - - - - method_exists($event, 'isMainRequest') - - - FigGenericLinkProvider - FigLink - - - $linkProvider - $linkProvider - $linkProvider - $this->createLink('preload', $href) - $this->createLink('preload', $href) - Link|FigLink - - - diff --git a/psalm.xml b/psalm.xml index f0acd22c..4e61c12e 100644 --- a/psalm.xml +++ b/psalm.xml @@ -5,7 +5,6 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="https://getpsalm.org/schema/config" xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd" - errorBaseline="psalm.baseline.xml" phpVersion="8.0" > diff --git a/src/Asset/EntrypointLookup.php b/src/Asset/EntrypointLookup.php index f5fdd38a..2a4e120d 100644 --- a/src/Asset/EntrypointLookup.php +++ b/src/Asset/EntrypointLookup.php @@ -64,6 +64,8 @@ public function getIntegrityData(): array /** * Resets the state of this service. + * + * @return void */ public function reset() { @@ -89,7 +91,7 @@ private function getEntryFiles(string $entryName, string $key): array return $newFiles; } - private function validateEntryName(string $entryName) + private function validateEntryName(string $entryName): void { $entriesData = $this->getEntriesData(); if (!isset($entriesData['entrypoints'][$entryName]) && $this->strictMode) { diff --git a/src/Asset/TagRenderer.php b/src/Asset/TagRenderer.php index d84617eb..6faaa417 100644 --- a/src/Asset/TagRenderer.php +++ b/src/Asset/TagRenderer.php @@ -10,7 +10,6 @@ namespace Symfony\WebpackEncoreBundle\Asset; use Symfony\Component\Asset\Packages; -use Symfony\Component\DependencyInjection\ServiceLocator; use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; use Symfony\Contracts\Service\ResetInterface; use Symfony\WebpackEncoreBundle\Event\RenderAssetTagEvent; @@ -30,27 +29,14 @@ class TagRenderer implements ResetInterface private $renderedFiles = []; public function __construct( - $entrypointLookupCollection, + EntrypointLookupCollectionInterface $entrypointLookupCollection, Packages $packages, array $defaultAttributes = [], array $defaultScriptAttributes = [], array $defaultLinkAttributes = [], EventDispatcherInterface $eventDispatcher = null ) { - if ($entrypointLookupCollection instanceof EntrypointLookupInterface) { - @trigger_error(sprintf('The "$entrypointLookupCollection" argument in method "%s()" must be an instance of EntrypointLookupCollection.', __METHOD__), \E_USER_DEPRECATED); - - $this->entrypointLookupCollection = new EntrypointLookupCollection( - new ServiceLocator(['_default' => function () use ($entrypointLookupCollection) { - return $entrypointLookupCollection; - }]) - ); - } elseif ($entrypointLookupCollection instanceof EntrypointLookupCollectionInterface) { - $this->entrypointLookupCollection = $entrypointLookupCollection; - } else { - throw new \TypeError('The "$entrypointLookupCollection" argument must be an instance of EntrypointLookupCollectionInterface.'); - } - + $this->entrypointLookupCollection = $entrypointLookupCollection; $this->packages = $packages; $this->defaultAttributes = $defaultAttributes; $this->defaultScriptAttributes = $defaultScriptAttributes; @@ -150,6 +136,9 @@ public function getDefaultAttributes(): array return $this->defaultAttributes; } + /** + * @return void + */ public function reset() { $this->renderedFiles = [ diff --git a/src/DependencyInjection/Compiler/RemoveStimulusServicesPass.php b/src/DependencyInjection/Compiler/RemoveStimulusServicesPass.php deleted file mode 100644 index 02db3c51..00000000 --- a/src/DependencyInjection/Compiler/RemoveStimulusServicesPass.php +++ /dev/null @@ -1,24 +0,0 @@ - - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\WebpackEncoreBundle\DependencyInjection\Compiler; - -use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; -use Symfony\Component\DependencyInjection\ContainerBuilder; - -class RemoveStimulusServicesPass implements CompilerPassInterface -{ - public function process(ContainerBuilder $container) - { - if ($container->hasDefinition('stimulus.helper')) { - // remove the Stimulus integration if StimulusBundle is installed - $container->removeDefinition('webpack_encore.twig_stimulus_extension'); - } - } -} diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index 2fd47d4d..1d9eecb1 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -20,7 +20,7 @@ public function getConfigTreeBuilder(): TreeBuilder { $treeBuilder = new TreeBuilder('webpack_encore'); /** @var ArrayNodeDefinition $rootNode */ - $rootNode = method_exists($treeBuilder, 'getRootNode') ? $treeBuilder->getRootNode() : $treeBuilder->root('webpack_encore'); + $rootNode = $treeBuilder->getRootNode(); $rootNode ->validate() diff --git a/src/Dto/AbstractStimulusDto.php b/src/Dto/AbstractStimulusDto.php deleted file mode 100644 index b968246b..00000000 --- a/src/Dto/AbstractStimulusDto.php +++ /dev/null @@ -1,67 +0,0 @@ - - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\WebpackEncoreBundle\Dto; - -use Twig\Environment; - -/** - * @internal - * - * @deprecated since 1.17.0 - install symfony/stimulus-bundle instead. - */ -abstract class AbstractStimulusDto implements \Stringable -{ - /** - * @var Environment - */ - private $env; - - public function __construct(Environment $env) - { - $this->env = $env; - } - - abstract public function toArray(): array; - - protected function getFormattedControllerName(string $controllerName): string - { - return $this->escapeAsHtmlAttr($this->normalizeControllerName($controllerName)); - } - - protected function getFormattedValue($value) - { - if ($value instanceof \Stringable || (\is_object($value) && \is_callable([$value, '__toString']))) { - $value = (string) $value; - } elseif (!\is_scalar($value)) { - $value = json_encode($value); - } elseif (\is_bool($value)) { - $value = $value ? 'true' : 'false'; - } - - return (string) $value; - } - - protected function escapeAsHtmlAttr($value): string - { - return (string) twig_escape_filter($this->env, $value, 'html_attr'); - } - - /** - * Normalize a Stimulus controller name into its HTML equivalent (no special character and / becomes --). - * - * @see https://stimulus.hotwired.dev/reference/controllers - */ - private function normalizeControllerName(string $controllerName): string - { - return preg_replace('/^@/', '', str_replace('_', '-', str_replace('/', '--', $controllerName))); - } -} diff --git a/src/Dto/StimulusActionsDto.php b/src/Dto/StimulusActionsDto.php deleted file mode 100644 index d2ad92fd..00000000 --- a/src/Dto/StimulusActionsDto.php +++ /dev/null @@ -1,56 +0,0 @@ - - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\WebpackEncoreBundle\Dto; - -/** - * @deprecated since 1.17.0 - install symfony/stimulus-bundle instead. - */ -final class StimulusActionsDto extends AbstractStimulusDto -{ - private $actions = []; - private $parameters = []; - - /** - * @param array $parameters Parameters to pass to the action. Optional. - */ - public function addAction(string $controllerName, string $actionName, string $eventName = null, array $parameters = []): void - { - $controllerName = $this->getFormattedControllerName($controllerName); - $action = $controllerName.'#'.$this->escapeAsHtmlAttr($actionName); - - if (null !== $eventName) { - $action = $eventName.'->'.$action; - } - - $this->actions[] = $action; - - foreach ($parameters as $name => $value) { - $this->parameters['data-'.$controllerName.'-'.$name.'-param'] = $this->getFormattedValue($value); - } - } - - public function __toString(): string - { - if (0 === \count($this->actions)) { - return ''; - } - - return rtrim('data-action="'.implode(' ', $this->actions).'" '.implode(' ', array_map(function (string $attribute, string $value): string { - return $attribute.'="'.$this->escapeAsHtmlAttr($value).'"'; - }, array_keys($this->parameters), $this->parameters))); - } - - public function toArray(): array - { - return ['data-action' => implode(' ', $this->actions)] + $this->parameters; - } -} diff --git a/src/Dto/StimulusControllersDto.php b/src/Dto/StimulusControllersDto.php deleted file mode 100644 index 483a6bc3..00000000 --- a/src/Dto/StimulusControllersDto.php +++ /dev/null @@ -1,88 +0,0 @@ - - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\WebpackEncoreBundle\Dto; - -/** - * @deprecated since 1.17.0 - install symfony/stimulus-bundle instead. - */ -final class StimulusControllersDto extends AbstractStimulusDto -{ - private $controllers = []; - private $values = []; - private $classes = []; - - public function addController(string $controllerName, array $controllerValues = [], array $controllerClasses = []): void - { - $controllerName = $this->getFormattedControllerName($controllerName); - $this->controllers[] = $controllerName; - - foreach ($controllerValues as $key => $value) { - if (null === $value) { - continue; - } - - $key = $this->escapeAsHtmlAttr($this->normalizeKeyName($key)); - $value = $this->getFormattedValue($value); - - $this->values['data-'.$controllerName.'-'.$key.'-value'] = $value; - } - - foreach ($controllerClasses as $key => $class) { - $key = $this->escapeAsHtmlAttr($this->normalizeKeyName($key)); - - $this->values['data-'.$controllerName.'-'.$key.'-class'] = $class; - } - } - - public function __toString(): string - { - if (0 === \count($this->controllers)) { - return ''; - } - - return rtrim( - 'data-controller="'.implode(' ', $this->controllers).'" '. - implode(' ', array_map(function (string $attribute, string $value): string { - return $attribute.'="'.$this->escapeAsHtmlAttr($value).'"'; - }, array_keys($this->values), $this->values)).' '. - implode(' ', array_map(function (string $attribute, string $value): string { - return $attribute.'="'.$this->escapeAsHtmlAttr($value).'"'; - }, array_keys($this->classes), $this->classes)) - ); - } - - public function toArray(): array - { - if (0 === \count($this->controllers)) { - return []; - } - - return [ - 'data-controller' => implode(' ', $this->controllers), - ] + $this->values + $this->classes; - } - - /** - * Normalize a Stimulus Value API key into its HTML equivalent ("kebab case"). - * Backport features from symfony/string. - * - * @see https://stimulus.hotwired.dev/reference/values - */ - private function normalizeKeyName(string $str): string - { - // Adapted from ByteString::camel - $str = ucfirst(str_replace(' ', '', ucwords(preg_replace('/[^a-zA-Z0-9\x7f-\xff]++/', ' ', $str)))); - - // Adapted from ByteString::snake - return strtolower(preg_replace(['/([A-Z]+)([A-Z][a-z])/', '/([a-z\d])([A-Z])/'], '\1-\2', $str)); - } -} diff --git a/src/Dto/StimulusTargetsDto.php b/src/Dto/StimulusTargetsDto.php deleted file mode 100644 index a2691127..00000000 --- a/src/Dto/StimulusTargetsDto.php +++ /dev/null @@ -1,47 +0,0 @@ - - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\WebpackEncoreBundle\Dto; - -/** - * @deprecated since 1.17.0 - install symfony/stimulus-bundle instead. - */ -final class StimulusTargetsDto extends AbstractStimulusDto -{ - private $targets = []; - - /** - * @param string $controllerName the Stimulus controller name - * @param string|null $targetNames The space-separated list of target names if a string is passed to the 1st argument. Optional. - */ - public function addTarget(string $controllerName, string $targetNames = null): void - { - $controllerName = $this->getFormattedControllerName($controllerName); - - $this->targets['data-'.$controllerName.'-target'] = $targetNames; - } - - public function __toString(): string - { - if (0 === \count($this->targets)) { - return ''; - } - - return implode(' ', array_map(function (string $attribute, string $value): string { - return $attribute.'="'.$this->escapeAsHtmlAttr($value).'"'; - }, array_keys($this->targets), $this->targets)); - } - - public function toArray(): array - { - return $this->targets; - } -} diff --git a/src/EventListener/PreLoadAssetsEventListener.php b/src/EventListener/PreLoadAssetsEventListener.php index da149207..c06b296e 100644 --- a/src/EventListener/PreLoadAssetsEventListener.php +++ b/src/EventListener/PreLoadAssetsEventListener.php @@ -29,10 +29,7 @@ public function __construct(TagRenderer $tagRenderer) public function onKernelResponse(ResponseEvent $event): void { - // Handle deprecated `KernelEvent::isMasterRequest() - Can be removed when Symfony < 5.3 support is dropped. - $mainRequestMethod = method_exists($event, 'isMainRequest') ? 'isMainRequest' : 'isMasterRequest'; - - if (!$event->$mainRequestMethod()) { + if (!$event->isMainRequest()) { return; } diff --git a/src/EventListener/ResetAssetsEventListener.php b/src/EventListener/ResetAssetsEventListener.php index 7d126289..a39482c6 100644 --- a/src/EventListener/ResetAssetsEventListener.php +++ b/src/EventListener/ResetAssetsEventListener.php @@ -34,12 +34,12 @@ public static function getSubscribedEvents() ]; } + /** + * @return void + */ public function resetAssets(FinishRequestEvent $event) { - // Handle deprecated `KernelEvent::isMasterRequest() - Can be removed when Symfony < 5.3 support is dropped. - $mainRequestMethod = method_exists($event, 'isMainRequest') ? 'isMainRequest' : 'isMasterRequest'; - - if (!$event->$mainRequestMethod()) { + if (!$event->isMainRequest()) { return; } foreach ($this->buildNames as $name) { diff --git a/src/Resources/config/services.xml b/src/Resources/config/services.xml index 1b89d720..874caf0b 100644 --- a/src/Resources/config/services.xml +++ b/src/Resources/config/services.xml @@ -36,10 +36,6 @@ - - - - diff --git a/src/Twig/StimulusTwigExtension.php b/src/Twig/StimulusTwigExtension.php deleted file mode 100644 index 41837173..00000000 --- a/src/Twig/StimulusTwigExtension.php +++ /dev/null @@ -1,169 +0,0 @@ - - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\WebpackEncoreBundle\Twig; - -use Symfony\WebpackEncoreBundle\Dto\StimulusActionsDto; -use Symfony\WebpackEncoreBundle\Dto\StimulusControllersDto; -use Symfony\WebpackEncoreBundle\Dto\StimulusTargetsDto; -use Twig\Environment; -use Twig\Extension\AbstractExtension; -use Twig\TwigFilter; -use Twig\TwigFunction; - -/** - * @deprecated since 1.17.0 - install symfony/stimulus-bundle instead. - */ -final class StimulusTwigExtension extends AbstractExtension -{ - public function getFunctions(): array - { - return [ - new TwigFunction('stimulus_controller', [$this, 'renderStimulusController'], ['needs_environment' => true, 'is_safe' => ['html_attr']]), - new TwigFunction('stimulus_action', [$this, 'renderStimulusAction'], ['needs_environment' => true, 'is_safe' => ['html_attr']]), - new TwigFunction('stimulus_target', [$this, 'renderStimulusTarget'], ['needs_environment' => true, 'is_safe' => ['html_attr']]), - ]; - } - - public function getFilters(): array - { - return [ - new TwigFilter('stimulus_controller', [$this, 'appendStimulusController'], ['is_safe' => ['html_attr']]), - new TwigFilter('stimulus_action', [$this, 'appendStimulusAction'], ['is_safe' => ['html_attr']]), - new TwigFilter('stimulus_target', [$this, 'appendStimulusTarget'], ['is_safe' => ['html_attr']]), - ]; - } - - /** - * @param string $controllerName the Stimulus controller name - * @param array $controllerValues array of controller values - * @param array $controllerClasses array of controller CSS classes - */ - public function renderStimulusController(Environment $env, $controllerName, array $controllerValues = [], array $controllerClasses = []): StimulusControllersDto - { - $dto = new StimulusControllersDto($env); - - if (\is_array($controllerName)) { - trigger_deprecation('symfony/webpack-encore-bundle', 'v1.15.0', 'Passing an array as first argument of stimulus_controller() is deprecated.'); - - if ($controllerValues || $controllerClasses) { - throw new \InvalidArgumentException('You cannot pass an array to the first and second/third argument of stimulus_controller(): check the documentation.'); - } - - $data = $controllerName; - - foreach ($data as $controllerName => $controllerValues) { - $dto->addController($controllerName, $controllerValues); - } - - return $dto; - } - - $dto->addController($controllerName, $controllerValues, $controllerClasses); - - return $dto; - } - - /** - * @param array $parameters Parameters to pass to the action. Optional. - */ - public function renderStimulusAction(Environment $env, $controllerName, string $actionName = null, string $eventName = null, array $parameters = []): StimulusActionsDto - { - $dto = new StimulusActionsDto($env); - if (\is_array($controllerName)) { - trigger_deprecation('symfony/webpack-encore-bundle', 'v1.15.0', 'Passing an array as first argument of stimulus_action() is deprecated.'); - - if ($actionName || $eventName || $parameters) { - throw new \InvalidArgumentException('You cannot pass a string to the second or third argument nor an array to the fourth argument while passing an array to the first argument of stimulus_action(): check the documentation.'); - } - - $data = $controllerName; - - foreach ($data as $controllerName => $controllerActions) { - if (\is_string($controllerActions)) { - $controllerActions = [[$controllerActions]]; - } - - foreach ($controllerActions as $possibleEventName => $controllerAction) { - if (\is_string($possibleEventName) && \is_string($controllerAction)) { - $controllerAction = [$possibleEventName => $controllerAction]; - } elseif (\is_string($controllerAction)) { - $controllerAction = [$controllerAction]; - } - - foreach ($controllerAction as $eventName => $actionName) { - $dto->addAction($controllerName, $actionName, \is_string($eventName) ? $eventName : null); - } - } - } - - return $dto; - } - - $dto->addAction($controllerName, $actionName, $eventName, $parameters); - - return $dto; - } - - public function appendStimulusController(StimulusControllersDto $dto, string $controllerName, array $controllerValues = [], array $controllerClasses = []): StimulusControllersDto - { - $dto->addController($controllerName, $controllerValues, $controllerClasses); - - return $dto; - } - - /** - * @param array $parameters Parameters to pass to the action. Optional. - */ - public function appendStimulusAction(StimulusActionsDto $dto, string $controllerName, string $actionName, string $eventName = null, array $parameters = []): StimulusActionsDto - { - $dto->addAction($controllerName, $actionName, $eventName, $parameters); - - return $dto; - } - - /** - * @param string $controllerName the Stimulus controller name - * @param string|null $targetNames The space-separated list of target names if a string is passed to the 1st argument. Optional. - */ - public function renderStimulusTarget(Environment $env, $controllerName, string $targetNames = null): StimulusTargetsDto - { - $dto = new StimulusTargetsDto($env); - if (\is_array($controllerName)) { - trigger_deprecation('symfony/webpack-encore-bundle', 'v1.15.0', 'Passing an array as first argument of stimulus_target() is deprecated.'); - - if ($targetNames) { - throw new \InvalidArgumentException('You cannot pass a string to the second argument while passing an array to the first argument of stimulus_target(): check the documentation.'); - } - - $data = $controllerName; - - foreach ($data as $controllerName => $targetNames) { - $dto->addTarget($controllerName, $targetNames); - } - - return $dto; - } - - $dto->addTarget($controllerName, $targetNames); - - return $dto; - } - - /** - * @param string $controllerName the Stimulus controller name - * @param string|null $targetNames The space-separated list of target names if a string is passed to the 1st argument. Optional. - */ - public function appendStimulusTarget(StimulusTargetsDto $dto, string $controllerName, string $targetNames = null): StimulusTargetsDto - { - $dto->addTarget($controllerName, $targetNames); - - return $dto; - } -} diff --git a/src/WebpackEncoreBundle.php b/src/WebpackEncoreBundle.php index 3a6be80a..b4fcfbae 100644 --- a/src/WebpackEncoreBundle.php +++ b/src/WebpackEncoreBundle.php @@ -9,14 +9,8 @@ namespace Symfony\WebpackEncoreBundle; -use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\HttpKernel\Bundle\Bundle; -use Symfony\WebpackEncoreBundle\DependencyInjection\Compiler\RemoveStimulusServicesPass; final class WebpackEncoreBundle extends Bundle { - public function build(ContainerBuilder $container) - { - $container->addCompilerPass(new RemoveStimulusServicesPass()); - } } diff --git a/tests/Dto/StimulusActionsDtoTest.php b/tests/Dto/StimulusActionsDtoTest.php deleted file mode 100644 index 9c86d80e..00000000 --- a/tests/Dto/StimulusActionsDtoTest.php +++ /dev/null @@ -1,42 +0,0 @@ - - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\WebpackEncoreBundle\Tests\Dto; - -use PHPUnit\Framework\TestCase; -use Symfony\WebpackEncoreBundle\Dto\StimulusActionsDto; -use Twig\Environment; -use Twig\Loader\ArrayLoader; - -class StimulusActionsDtoTest extends TestCase -{ - /** - * @var StimulusActionsDto - */ - private $stimulusActionsDto; - - protected function setUp(): void - { - $this->stimulusActionsDto = new StimulusActionsDto(new Environment(new ArrayLoader())); - } - - public function testToStringEscapingAttributeValues(): void - { - $this->stimulusActionsDto->addAction('foo', 'bar', 'baz', ['qux' => '"']); - $attributesHtml = (string) $this->stimulusActionsDto; - self::assertSame('data-action="baz->foo#bar" data-foo-qux-param="""', $attributesHtml); - } - - public function testToArrayNoEscapingAttributeValues(): void - { - $this->stimulusActionsDto->addAction('foo', 'bar', 'baz', ['qux' => '"']); - $attributesArray = $this->stimulusActionsDto->toArray(); - self::assertSame(['data-action' => 'baz->foo#bar', 'data-foo-qux-param' => '"'], $attributesArray); - } -} diff --git a/tests/Dto/StimulusControllersDtoTest.php b/tests/Dto/StimulusControllersDtoTest.php deleted file mode 100644 index 2ef6fa0d..00000000 --- a/tests/Dto/StimulusControllersDtoTest.php +++ /dev/null @@ -1,54 +0,0 @@ - - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\WebpackEncoreBundle\Tests\Dto; - -use PHPUnit\Framework\TestCase; -use Symfony\WebpackEncoreBundle\Dto\StimulusControllersDto; -use Twig\Environment; -use Twig\Loader\ArrayLoader; - -class StimulusControllersDtoTest extends TestCase -{ - /** - * @var StimulusControllersDto - */ - private $stimulusControllersDto; - - protected function setUp(): void - { - $this->stimulusControllersDto = new StimulusControllersDto(new Environment(new ArrayLoader())); - } - - public function testToStringEscapingAttributeValues(): void - { - $this->stimulusControllersDto->addController('foo', ['bar' => '"'], ['baz' => '"']); - $attributesHtml = (string) $this->stimulusControllersDto; - self::assertSame( - 'data-controller="foo" '. - 'data-foo-bar-value=""" '. - 'data-foo-baz-class="""', - $attributesHtml - ); - } - - public function testToArrayNoEscapingAttributeValues(): void - { - $this->stimulusControllersDto->addController('foo', ['bar' => '"'], ['baz' => '"']); - $attributesArray = $this->stimulusControllersDto->toArray(); - self::assertSame( - [ - 'data-controller' => 'foo', - 'data-foo-bar-value' => '"', - 'data-foo-baz-class' => '"', - ], - $attributesArray - ); - } -} diff --git a/tests/Dto/StimulusTargetsDtoTest.php b/tests/Dto/StimulusTargetsDtoTest.php deleted file mode 100644 index 23fbd6dd..00000000 --- a/tests/Dto/StimulusTargetsDtoTest.php +++ /dev/null @@ -1,42 +0,0 @@ - - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\WebpackEncoreBundle\Tests\Dto; - -use PHPUnit\Framework\TestCase; -use Symfony\WebpackEncoreBundle\Dto\StimulusTargetsDto; -use Twig\Environment; -use Twig\Loader\ArrayLoader; - -class StimulusTargetsDtoTest extends TestCase -{ - /** - * @var StimulusTargetsDto - */ - private $stimulusTargetsDto; - - protected function setUp(): void - { - $this->stimulusTargetsDto = new StimulusTargetsDto(new Environment(new ArrayLoader())); - } - - public function testToStringEscapingAttributeValues(): void - { - $this->stimulusTargetsDto->addTarget('foo', '"'); - $attributesHtml = (string) $this->stimulusTargetsDto; - self::assertSame('data-foo-target="""', $attributesHtml); - } - - public function testToArrayNoEscapingAttributeValues(): void - { - $this->stimulusTargetsDto->addTarget('foo', '"'); - $attributesArray = $this->stimulusTargetsDto->toArray(); - self::assertSame(['data-foo-target' => '"'], $attributesArray); - } -} diff --git a/tests/IntegrationTest.php b/tests/IntegrationTest.php index 80d3658f..d0e9d01d 100644 --- a/tests/IntegrationTest.php +++ b/tests/IntegrationTest.php @@ -24,12 +24,10 @@ use Symfony\Component\HttpKernel\Kernel; use Symfony\Component\HttpKernel\Log\Logger; use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator; -use Symfony\Component\Routing\RouteCollectionBuilder; use Symfony\WebpackEncoreBundle\Asset\EntrypointLookupCollectionInterface; use Symfony\WebpackEncoreBundle\Asset\EntrypointLookupInterface; use Symfony\WebpackEncoreBundle\Asset\TagRenderer; use Symfony\WebpackEncoreBundle\CacheWarmer\EntrypointCacheWarmer; -use Symfony\WebpackEncoreBundle\Twig\StimulusTwigExtension; use Symfony\WebpackEncoreBundle\WebpackEncoreBundle; use Twig\Environment; @@ -202,413 +200,9 @@ public function testAutowireDefaultBuildArgument() $this->assertTrue(true); } - public function provideRenderStimulusController() - { - yield 'normalize-names' => [ - 'dataOrControllerName' => '@symfony/ux-dropzone/dropzone', - 'controllerValues' => [ - 'my"Key"' => true, - ], - 'controllerClasses' => [ - 'second"Key"' => 'loading', - ], - 'expectedString' => 'data-controller="symfony--ux-dropzone--dropzone" data-symfony--ux-dropzone--dropzone-my-key-value="true" data-symfony--ux-dropzone--dropzone-second-key-class="loading"', - 'expectedArray' => ['data-controller' => 'symfony--ux-dropzone--dropzone', 'data-symfony--ux-dropzone--dropzone-my-key-value' => 'true', 'data-symfony--ux-dropzone--dropzone-second-key-class' => 'loading'], - ]; - - yield 'short-single-controller-no-data' => [ - 'dataOrControllerName' => 'my-controller', - 'controllerValues' => [], - 'controllerClasses' => [], - 'expectedString' => 'data-controller="my-controller"', - 'expectedArray' => ['data-controller' => 'my-controller'], - ]; - - yield 'short-single-controller-with-data' => [ - 'dataOrControllerName' => 'my-controller', - 'controllerValues' => ['myValue' => 'scalar-value'], - 'controllerClasses' => [], - 'expectedString' => 'data-controller="my-controller" data-my-controller-my-value-value="scalar-value"', - 'expectedArray' => ['data-controller' => 'my-controller', 'data-my-controller-my-value-value' => 'scalar-value'], - ]; - - yield 'false-attribute-value-renders-false' => [ - 'dataOrControllerName' => 'false-controller', - 'controllerValues' => ['isEnabled' => false], - 'controllerClasses' => [], - 'expectedString' => 'data-controller="false-controller" data-false-controller-is-enabled-value="false"', - 'expectedArray' => ['data-controller' => 'false-controller', 'data-false-controller-is-enabled-value' => 'false'], - ]; - - yield 'true-attribute-value-renders-true' => [ - 'dataOrControllerName' => 'true-controller', - 'controllerValues' => ['isEnabled' => true], - 'controllerClasses' => [], - 'expectedString' => 'data-controller="true-controller" data-true-controller-is-enabled-value="true"', - 'expectedArray' => ['data-controller' => 'true-controller', 'data-true-controller-is-enabled-value' => 'true'], - ]; - - yield 'null-attribute-value-does-not-render' => [ - 'dataOrControllerName' => 'null-controller', - 'controllerValues' => ['firstName' => null], - 'controllerClasses' => [], - 'expectedString' => 'data-controller="null-controller"', - 'expectedArray' => ['data-controller' => 'null-controller'], - ]; - - yield 'short-single-controller-no-data-with-class' => [ - 'dataOrControllerName' => 'my-controller', - 'controllerValues' => [], - 'controllerClasses' => ['loading' => 'spinner'], - 'expectedString' => 'data-controller="my-controller" data-my-controller-loading-class="spinner"', - 'expectedArray' => ['data-controller' => 'my-controller', 'data-my-controller-loading-class' => 'spinner'], - ]; - } - - /** - * @dataProvider provideRenderStimulusController - */ - public function testRenderStimulusController($dataOrControllerName, array $controllerValues, array $controllerClasses, string $expectedString, array $expectedArray) - { - $kernel = new WebpackEncoreIntegrationTestKernel(true); - $kernel->boot(); - $twig = $this->getTwigEnvironmentFromBootedKernel($kernel); - - $extension = new StimulusTwigExtension(); - $dto = $extension->renderStimulusController($twig, $dataOrControllerName, $controllerValues, $controllerClasses); - $this->assertSame($expectedString, (string) $dto); - $this->assertSame($expectedArray, $dto->toArray()); - } - - public function provideLegacyRenderMultipleStimulusControllers() - { - yield 'empty' => [ - 'dataOrControllerName' => [], - 'controllerValues' => [], - 'expectedString' => '', - 'expectedArray' => [], - ]; - - yield 'single-controller-no-data' => [ - 'dataOrControllerName' => [ - 'my-controller' => [], - ], - 'controllerValues' => [], - 'expectedString' => 'data-controller="my-controller"', - 'expectedArray' => ['data-controller' => 'my-controller'], - ]; - - yield 'single-controller-scalar-data' => [ - 'dataOrControllerName' => [ - 'my-controller' => [ - 'myValue' => 'scalar-value', - ], - ], - 'controllerValues' => [], - 'expectedString' => 'data-controller="my-controller" data-my-controller-my-value-value="scalar-value"', - 'expectedArray' => ['data-controller' => 'my-controller', 'data-my-controller-my-value-value' => 'scalar-value'], - ]; - - yield 'single-controller-typed-data' => [ - 'dataOrControllerName' => [ - 'my-controller' => [ - 'boolean' => true, - 'number' => 4, - 'string' => 'str', - ], - ], - 'controllerValues' => [], - 'expectedString' => 'data-controller="my-controller" data-my-controller-boolean-value="true" data-my-controller-number-value="4" data-my-controller-string-value="str"', - 'expectedArray' => ['data-controller' => 'my-controller', 'data-my-controller-boolean-value' => 'true', 'data-my-controller-number-value' => '4', 'data-my-controller-string-value' => 'str'], - ]; - - yield 'single-controller-nested-data' => [ - 'dataOrControllerName' => [ - 'my-controller' => [ - 'myValue' => ['nested' => 'array'], - ], - ], - 'controllerValues' => [], - 'expectedString' => 'data-controller="my-controller" data-my-controller-my-value-value="{"nested":"array"}"', - 'expectedArray' => ['data-controller' => 'my-controller', 'data-my-controller-my-value-value' => '{"nested":"array"}'], - ]; - - yield 'multiple-controllers-scalar-data' => [ - 'dataOrControllerName' => [ - 'my-controller' => [ - 'myValue' => 'scalar-value', - ], - 'another-controller' => [ - 'anotherValue' => 'scalar-value 2', - ], - ], - 'controllerValues' => [], - 'expectedString' => 'data-controller="my-controller another-controller" data-my-controller-my-value-value="scalar-value" data-another-controller-another-value-value="scalar-value 2"', - 'expectedArray' => ['data-controller' => 'my-controller another-controller', 'data-my-controller-my-value-value' => 'scalar-value', 'data-another-controller-another-value-value' => 'scalar-value 2'], - ]; - - yield 'normalize-names' => [ - 'dataOrControllerName' => [ - '@symfony/ux-dropzone/dropzone' => [ - 'my"Key"' => true, - ], - ], - 'controllerValues' => [], - 'expectedString' => 'data-controller="symfony--ux-dropzone--dropzone" data-symfony--ux-dropzone--dropzone-my-key-value="true"', - 'expectedArray' => ['data-controller' => 'symfony--ux-dropzone--dropzone', 'data-symfony--ux-dropzone--dropzone-my-key-value' => 'true'], - ]; - } - - /** - * @dataProvider provideLegacyRenderMultipleStimulusControllers - * - * @legacy - */ - public function testLegacyRenderMultipleStimulusControllers($dataOrControllerName, array $controllerValues, string $expectedString, array $expectedArray) - { - $kernel = new WebpackEncoreIntegrationTestKernel(true); - $kernel->boot(); - $twig = $this->getTwigEnvironmentFromBootedKernel($kernel); - - $extension = new StimulusTwigExtension(); - $dto = $extension->renderStimulusController($twig, $dataOrControllerName, $controllerValues); - $this->assertSame($expectedString, (string) $dto); - $this->assertSame($expectedArray, $dto->toArray()); - } - - public function testAppendStimulusController() - { - $kernel = new WebpackEncoreIntegrationTestKernel(true); - $kernel->boot(); - $twig = $this->getTwigEnvironmentFromBootedKernel($kernel); - - $extension = new StimulusTwigExtension(); - $dto = $extension->renderStimulusController($twig, 'my-controller', ['myValue' => 'scalar-value']); - $this->assertSame( - 'data-controller="my-controller another-controller" data-my-controller-my-value-value="scalar-value" data-another-controller-another-value-value="scalar-value 2"', - (string) $extension->appendStimulusController($dto, 'another-controller', ['another-value' => 'scalar-value 2']) - ); - } - - public function provideRenderStimulusAction() - { - yield 'with default event' => [ - 'dataOrControllerName' => 'my-controller', - 'actionName' => 'onClick', - 'eventName' => null, - 'parameters' => [], - 'expectedString' => 'data-action="my-controller#onClick"', - 'expectedArray' => ['data-action' => 'my-controller#onClick'], - ]; - - yield 'with custom event' => [ - 'dataOrControllerName' => 'my-controller', - 'actionName' => 'onClick', - 'eventName' => 'click', - 'parameters' => [], - 'expectedString' => 'data-action="click->my-controller#onClick"', - 'expectedArray' => ['data-action' => 'click->my-controller#onClick'], - ]; - - yield 'with parameters' => [ - 'dataOrControllerName' => 'my-controller', - 'actionName' => 'onClick', - 'eventName' => null, - 'parameters' => ['bool-param' => true, 'int-param' => 4, 'string-param' => 'test'], - 'expectedString' => 'data-action="my-controller#onClick" data-my-controller-bool-param-param="true" data-my-controller-int-param-param="4" data-my-controller-string-param-param="test"', - 'expectedArray' => ['data-action' => 'my-controller#onClick', 'data-my-controller-bool-param-param' => 'true', 'data-my-controller-int-param-param' => '4', 'data-my-controller-string-param-param' => 'test'], - ]; - - yield 'normalize-name, with default event' => [ - 'dataOrControllerName' => '@symfony/ux-dropzone/dropzone', - 'actionName' => 'onClick', - 'eventName' => null, - 'parameters' => [], - 'expectedString' => 'data-action="symfony--ux-dropzone--dropzone#onClick"', - 'expectedArray' => ['data-action' => 'symfony--ux-dropzone--dropzone#onClick'], - ]; - - yield 'normalize-name, with custom event' => [ - 'dataOrControllerName' => '@symfony/ux-dropzone/dropzone', - 'actionName' => 'onClick', - 'eventName' => 'click', - 'parameters' => [], - 'expectedString' => 'data-action="click->symfony--ux-dropzone--dropzone#onClick"', - 'expectedArray' => ['data-action' => 'click->symfony--ux-dropzone--dropzone#onClick'], - ]; - } - - /** - * @dataProvider provideRenderStimulusAction - */ - public function testRenderStimulusAction($dataOrControllerName, ?string $actionName, ?string $eventName, array $parameters, string $expectedString, array $expectedArray) - { - $kernel = new WebpackEncoreIntegrationTestKernel(true); - $kernel->boot(); - $twig = $this->getTwigEnvironmentFromBootedKernel($kernel); - - $extension = new StimulusTwigExtension(); - $dto = $extension->renderStimulusAction($twig, $dataOrControllerName, $actionName, $eventName, $parameters); - $this->assertSame($expectedString, (string) $dto); - $this->assertSame($expectedArray, $dto->toArray()); - } - - public function provideLegacyRenderMultipleStimulusAction(): \Generator - { - yield 'multiple actions, with default event' => [ - 'dataOrControllerName' => [ - 'my-controller' => 'onClick', - 'my-second-controller' => ['onClick', 'onSomethingElse'], - 'foo/bar-controller' => 'onClick', - ], - 'actionName' => null, - 'eventName' => null, - 'parameters' => [], - 'expectedString' => 'data-action="my-controller#onClick my-second-controller#onClick my-second-controller#onSomethingElse foo--bar-controller#onClick"', - 'expectedArray' => ['data-action' => 'my-controller#onClick my-second-controller#onClick my-second-controller#onSomethingElse foo--bar-controller#onClick'], - ]; - - yield 'multiple actions, with custom event' => [ - 'dataOrControllerName' => [ - 'my-controller' => ['click' => 'onClick'], - 'my-second-controller' => [['click' => 'onClick'], ['change' => 'onSomethingElse']], - 'resize-controller' => ['resize@window' => 'onWindowResize'], - 'foo/bar-controller' => ['click' => 'onClick'], - ], - 'actionName' => null, - 'eventName' => null, - 'parameters' => [], - 'expectedString' => 'data-action="click->my-controller#onClick click->my-second-controller#onClick change->my-second-controller#onSomethingElse resize@window->resize-controller#onWindowResize click->foo--bar-controller#onClick"', - 'expectedArray' => ['data-action' => 'click->my-controller#onClick click->my-second-controller#onClick change->my-second-controller#onSomethingElse resize@window->resize-controller#onWindowResize click->foo--bar-controller#onClick'], - ]; - - yield 'multiple actions, with default and custom event' => [ - 'dataOrControllerName' => [ - 'my-controller' => ['click' => 'onClick'], - 'my-second-controller' => ['onClick', ['click' => 'onAnotherClick'], ['change' => 'onSomethingElse']], - 'resize-controller' => ['resize@window' => 'onWindowResize'], - 'foo/bar-controller' => ['click' => 'onClick'], - ], - 'actionName' => null, - 'eventName' => null, - 'parameters' => [], - 'expectedString' => 'data-action="click->my-controller#onClick my-second-controller#onClick click->my-second-controller#onAnotherClick change->my-second-controller#onSomethingElse resize@window->resize-controller#onWindowResize click->foo--bar-controller#onClick"', - 'expectedArray' => ['data-action' => 'click->my-controller#onClick my-second-controller#onClick click->my-second-controller#onAnotherClick change->my-second-controller#onSomethingElse resize@window->resize-controller#onWindowResize click->foo--bar-controller#onClick'], - ]; - } - - /** - * @dataProvider provideLegacyRenderMultipleStimulusAction - * - * @legacy - */ - public function testLegacyRenderMultipleStimulusActions($dataOrControllerName, ?string $actionName, ?string $eventName, array $parameters, string $expectedString, array $expectedArray) - { - $kernel = new WebpackEncoreIntegrationTestKernel(true); - $kernel->boot(); - $twig = $this->getTwigEnvironmentFromBootedKernel($kernel); - - $extension = new StimulusTwigExtension(); - $dto = $extension->renderStimulusAction($twig, $dataOrControllerName, $actionName, $eventName, $parameters); - $this->assertSame($expectedString, (string) $dto); - $this->assertSame($expectedArray, $dto->toArray()); - } - - public function testAppendStimulusAction() - { - $kernel = new WebpackEncoreIntegrationTestKernel(true); - $kernel->boot(); - $twig = $this->getTwigEnvironmentFromBootedKernel($kernel); - - $extension = new StimulusTwigExtension(); - $dto = $extension->renderStimulusAction($twig, 'my-controller', 'onClick', 'click'); - $this->assertSame( - 'data-action="click->my-controller#onClick change->my-second-controller#onSomethingElse"', - (string) $extension->appendStimulusAction($dto, 'my-second-controller', 'onSomethingElse', 'change') - ); - } - - public function provideRenderStimulusTarget() - { - yield 'simple' => [ - 'dataOrControllerName' => 'my-controller', - 'targetName' => 'myTarget', - 'expectedString' => 'data-my-controller-target="myTarget"', - 'expectedArray' => ['data-my-controller-target' => 'myTarget'], - ]; - - yield 'normalize-name' => [ - 'dataOrControllerName' => '@symfony/ux-dropzone/dropzone', - 'targetName' => 'myTarget', - 'expectedString' => 'data-symfony--ux-dropzone--dropzone-target="myTarget"', - 'expectedArray' => ['data-symfony--ux-dropzone--dropzone-target' => 'myTarget'], - ]; - } - - /** - * @dataProvider provideRenderStimulusTarget - */ - public function testRenderStimulusTarget($dataOrControllerName, ?string $targetName, string $expectedString, array $expectedArray) - { - $kernel = new WebpackEncoreIntegrationTestKernel(true); - $kernel->boot(); - $twig = $this->getTwigEnvironmentFromBootedKernel($kernel); - - $extension = new StimulusTwigExtension(); - $dto = $extension->renderStimulusTarget($twig, $dataOrControllerName, $targetName); - $this->assertSame($expectedString, (string) $dto); - $this->assertSame($expectedArray, $dto->toArray()); - } - - /** - * @legacy - */ - public function testLegacyRenderMultipleStimulusTargets() - { - $kernel = new WebpackEncoreIntegrationTestKernel(true); - $kernel->boot(); - $twig = $this->getTwigEnvironmentFromBootedKernel($kernel); - - $extension = new StimulusTwigExtension(); - $dto = $extension->renderStimulusTarget($twig, [ - 'my-controller' => 'myTarget', - '@symfony/ux-dropzone/dropzone' => 'anotherTarget fooTarget', - ]); - - $this->assertSame( - 'data-my-controller-target="myTarget" data-symfony--ux-dropzone--dropzone-target="anotherTarget fooTarget"', - (string) $dto - ); - - $this->assertSame([ - 'data-my-controller-target' => 'myTarget', - 'data-symfony--ux-dropzone--dropzone-target' => 'anotherTarget fooTarget', - ], - $dto->toArray() - ); - } - - public function testAppendStimulusTarget() - { - $kernel = new WebpackEncoreIntegrationTestKernel(true); - $kernel->boot(); - $twig = $this->getTwigEnvironmentFromBootedKernel($kernel); - - $extension = new StimulusTwigExtension(); - $dto = $extension->renderStimulusTarget($twig, 'my-controller', 'myTarget'); - $this->assertSame( - 'data-my-controller-target="myTarget" data-symfony--ux-dropzone--dropzone-target="anotherTarget fooTarget"', - (string) $extension->appendStimulusTarget($dto, '@symfony/ux-dropzone/dropzone', 'anotherTarget fooTarget') - ); - } - private function getContainerFromBootedKernel(WebpackEncoreIntegrationTestKernel $kernel) { - if ($kernel::VERSION_ID >= 40100) { - return $kernel->getContainer()->get('test.service_container'); - } - - return $kernel->getContainer(); + return $kernel->getContainer()->get('test.service_container'); } private function getTwigEnvironmentFromBootedKernel(WebpackEncoreIntegrationTestKernel $kernel) @@ -623,7 +217,7 @@ private function getTwigEnvironmentFromBootedKernel(WebpackEncoreIntegrationTest } } -abstract class AbstractWebpackEncoreIntegrationTestKernel extends Kernel +class WebpackEncoreIntegrationTestKernel extends Kernel { use MicroKernelTrait; @@ -650,6 +244,13 @@ public function registerBundles(): array ]; } + protected function configureRoutes(RoutingConfigurator $routes): void + { + $routes->add('foo', '/foo')->controller('kernel::renderFoo'); + $routes->add('render', '/render')->controller('kernel::renderTwig'); + $routes->add('render_sub_requests', '/render-sub-requests')->controller('kernel::renderSubRequests'); + } + protected function configureContainer(ContainerBuilder $container, LoaderInterface $loader) { $frameworkConfig = [ @@ -741,28 +342,6 @@ public function renderTwig(Environment $twig, Request $request) } } -if (AbstractWebpackEncoreIntegrationTestKernel::VERSION_ID >= 50100) { - class WebpackEncoreIntegrationTestKernel extends AbstractWebpackEncoreIntegrationTestKernel - { - protected function configureRoutes(RoutingConfigurator $routes): void - { - $routes->add('foo', '/foo')->controller('kernel::renderFoo'); - $routes->add('render', '/render')->controller('kernel::renderTwig'); - $routes->add('render_sub_requests', '/render-sub-requests')->controller('kernel::renderSubRequests'); - } - } -} else { - class WebpackEncoreIntegrationTestKernel extends AbstractWebpackEncoreIntegrationTestKernel - { - protected function configureRoutes(RouteCollectionBuilder $routes) - { - $routes->add('/foo', 'kernel::renderFoo'); - $routes->add('/render', 'kernel::renderTwig'); - $routes->add('/render-sub-requests', 'kernel::renderSubRequests'); - } - } -} - class WebpackEncoreCacheWarmerTester { private $entrypointCacheWarmer;