diff --git a/composer.json b/composer.json index e353f3d4..87bf5369 100644 --- a/composer.json +++ b/composer.json @@ -13,6 +13,7 @@ "gedmo/doctrine-extensions": "^3.0", "knplabs/doctrine-behaviors": "^2.0.6", "phpunit/phpunit": "^9.5", + "psr/container": "^1.1 || ^2.0", "sonata-project/admin-bundle": "^3.103 || ^4.0@dev", "sonata-project/doctrine-extensions": "^1.13", "sonata-project/doctrine-orm-admin-bundle": "^3.34 || ^4.0@dev", @@ -70,6 +71,7 @@ "psalm/plugin-phpunit": "^0.16", "psalm/plugin-symfony": "^2.0", "psr/event-dispatcher": "^1.0", + "symfony/browser-kit": "^5.3", "symfony/phpunit-bridge": "^5.3", "symplify/monorepo-builder": "^9.3", "theofidry/alice-data-fixtures": "^1.3", diff --git a/packages/basic-page-bundle/composer.json b/packages/basic-page-bundle/composer.json index 91894ff1..a342d98b 100644 --- a/packages/basic-page-bundle/composer.json +++ b/packages/basic-page-bundle/composer.json @@ -26,6 +26,7 @@ "friendsofsymfony/ckeditor-bundle": "^2.2", "gedmo/doctrine-extensions": "^3.0", "knplabs/doctrine-behaviors": "^2.0.6", + "psr/container": "^1.1 || ^2.0", "runroom-packages/seo-bundle": "^0.11@dev", "sonata-project/admin-bundle": "^3.103 || ^4.0@dev", "sonata-project/doctrine-orm-admin-bundle": "^3.34 || ^4.0@dev", @@ -35,7 +36,8 @@ "symfony/form": "^4.4 || ^5.3", "symfony/http-foundation": "^4.4.7 || ^5.3", "symfony/http-kernel": "^4.4.13 || ^5.3", - "symfony/validator": "^4.4 || ^5.3" + "symfony/validator": "^4.4 || ^5.3", + "twig/twig": "^2.12 || ^3.0" }, "require-dev": { "a2lix/auto-form-bundle": "^0.3.1", @@ -43,6 +45,7 @@ "matthiasnoback/symfony-dependency-injection-test": "^4.1", "phpunit/phpunit": "^9.5", "runroom-packages/testing": "^0.11@dev", + "symfony/browser-kit": "^5.3", "symfony/framework-bundle": "^4.4 || ^5.3", "symfony/phpunit-bridge": "^5.3", "symfony/routing": "^4.4 || ^5.3", diff --git a/packages/basic-page-bundle/src/Resources/config/services.php b/packages/basic-page-bundle/src/Resources/config/services.php index 51ee4357..a9d23d29 100644 --- a/packages/basic-page-bundle/src/Resources/config/services.php +++ b/packages/basic-page-bundle/src/Resources/config/services.php @@ -11,6 +11,7 @@ * file that was distributed with this source code. */ +use Psr\Container\ContainerInterface; use Runroom\BasicPageBundle\Admin\BasicPageAdmin; use Runroom\BasicPageBundle\Controller\BasicPageController; use Runroom\BasicPageBundle\Entity\BasicPage; @@ -18,6 +19,8 @@ use Runroom\BasicPageBundle\Service\BasicPageAlternateLinksProvider; use Runroom\BasicPageBundle\Service\BasicPageMetaInformationProvider; use Runroom\BasicPageBundle\Service\BasicPageService; +use Runroom\BasicPageBundle\Twig\BasicPageExtension; +use Runroom\BasicPageBundle\Twig\BasicPageRuntime; use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; use Symfony\Component\DependencyInjection\Loader\Configurator\ReferenceConfigurator; @@ -31,9 +34,11 @@ ->tag('sonata.admin', ['manager_type' => 'orm', 'label' => 'Basic pages']); $services->set(BasicPageController::class) + ->public() ->arg('$service', new ReferenceConfigurator(BasicPageService::class)) - ->call('setContainer', [new ReferenceConfigurator('service_container')]) - ->tag('controller.service_subscriber'); + ->call('setContainer', [new ReferenceConfigurator(ContainerInterface::class)]) + ->tag('container.service_subscriber') + ->tag('controller.service_arguments'); $services->set(BasicPageService::class) ->arg('$repository', new ReferenceConfigurator(BasicPageRepository::class)); @@ -48,4 +53,11 @@ ->arg('$registry', new ReferenceConfigurator('doctrine')) ->arg('$requestStack', new ReferenceConfigurator('request_stack')) ->tag('doctrine.repository_service'); + + $services->set(BasicPageExtension::class) + ->tag('twig.extension'); + + $services->set(BasicPageRuntime::class) + ->arg('$repository', new ReferenceConfigurator(BasicPageRepository::class)) + ->tag('twig.runtime'); }; diff --git a/packages/basic-page-bundle/src/Resources/views/show.html.twig b/packages/basic-page-bundle/src/Resources/views/show.html.twig index 7f747fa4..aa0e7fb5 100644 --- a/packages/basic-page-bundle/src/Resources/views/show.html.twig +++ b/packages/basic-page-bundle/src/Resources/views/show.html.twig @@ -1,2 +1,2 @@ -

{{ page.content.basicPage.title }}

-
{{ page.content.basicPage.content|raw }}
+

{{ model.basicPage.title }}

+
{{ model.basicPage.content|raw }}
diff --git a/packages/basic-page-bundle/src/Twig/BasicPageExtension.php b/packages/basic-page-bundle/src/Twig/BasicPageExtension.php new file mode 100644 index 00000000..ac23a233 --- /dev/null +++ b/packages/basic-page-bundle/src/Twig/BasicPageExtension.php @@ -0,0 +1,27 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Runroom\BasicPageBundle\Twig; + +use Twig\Extension\AbstractExtension; +use Twig\TwigFunction; + +class BasicPageExtension extends AbstractExtension +{ + public function getFunctions(): array + { + return [ + new TwigFunction('get_basic_pages', [BasicPageRuntime::class, 'getBasicPages']), + ]; + } +} diff --git a/packages/basic-page-bundle/src/Twig/BasicPageRuntime.php b/packages/basic-page-bundle/src/Twig/BasicPageRuntime.php new file mode 100644 index 00000000..cc1e5ace --- /dev/null +++ b/packages/basic-page-bundle/src/Twig/BasicPageRuntime.php @@ -0,0 +1,40 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Runroom\BasicPageBundle\Twig; + +use Runroom\BasicPageBundle\Entity\BasicPage; +use Runroom\BasicPageBundle\Repository\BasicPageRepository; +use Twig\Extension\RuntimeExtensionInterface; + +class BasicPageRuntime implements RuntimeExtensionInterface +{ + private BasicPageRepository $repository; + + public function __construct(BasicPageRepository $repository) + { + $this->repository = $repository; + } + + /** @return BasicPage[] */ + public function getBasicPages(?string $location = null): array + { + $criteria = ['publish' => true]; + + if (null !== $location) { + $criteria['location'] = $location; + } + + return $this->repository->findBy($criteria) ?? []; + } +} diff --git a/packages/basic-page-bundle/tests/Fixtures/Twig/get_basic_pages.test b/packages/basic-page-bundle/tests/Fixtures/Twig/get_basic_pages.test new file mode 100644 index 00000000..a95a5ca4 --- /dev/null +++ b/packages/basic-page-bundle/tests/Fixtures/Twig/get_basic_pages.test @@ -0,0 +1,13 @@ +--TEST-- +"get_basic_pages" function +--TEMPLATE-- +{% set basicPages = get_basic_pages('footer') %} + +{% for basicPage in basicPages %} +{{ basicPage.getLocation }} +{% endfor %} +--DATA-- +return [] +--EXPECT-- +footer +footer diff --git a/packages/basic-page-bundle/tests/Functional/BasicPageControllerTest.php b/packages/basic-page-bundle/tests/Functional/BasicPageControllerTest.php new file mode 100644 index 00000000..2889b274 --- /dev/null +++ b/packages/basic-page-bundle/tests/Functional/BasicPageControllerTest.php @@ -0,0 +1,37 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Runroom\BasicPageBundle\Tests\Functional; + +use Runroom\BasicPageBundle\Factory\BasicPageFactory; +use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; +use Zenstruck\Foundry\Test\Factories; +use Zenstruck\Foundry\Test\ResetDatabase; + +class BasicPageControllerTest extends WebTestCase +{ + use Factories; + use ResetDatabase; + + /** @test */ + public function itRendersStatic(): void + { + $client = static::createClient(); + + $basicPage = BasicPageFactory::new(['publish' => true])->withTranslations(['en'])->create()->object(); + + $client->request('GET', '/' . $basicPage->getSlug()); + + self::assertResponseIsSuccessful(); + } +} diff --git a/packages/basic-page-bundle/tests/Integration/BasicPageExtensionTest.php b/packages/basic-page-bundle/tests/Integration/BasicPageExtensionTest.php new file mode 100644 index 00000000..2a5afd85 --- /dev/null +++ b/packages/basic-page-bundle/tests/Integration/BasicPageExtensionTest.php @@ -0,0 +1,58 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Runroom\BasicPageBundle\Tests\Integration; + +use Runroom\BasicPageBundle\Entity\BasicPage; +use Runroom\BasicPageBundle\Factory\BasicPageFactory; +use Runroom\BasicPageBundle\Repository\BasicPageRepository; +use Runroom\BasicPageBundle\Twig\BasicPageExtension; +use Runroom\BasicPageBundle\Twig\BasicPageRuntime; +use Twig\RuntimeLoader\RuntimeLoaderInterface; +use Twig\Test\IntegrationTestCase; +use Zenstruck\Foundry\Test\Factories; + +class BasicPageExtensionTest extends IntegrationTestCase +{ + use Factories; + + public function getFixturesDir() + { + return __DIR__ . '/../Fixtures/Twig/'; + } + + protected function getExtensions() + { + return [ + new BasicPageExtension(), + ]; + } + + protected function getRuntimeLoaders() + { + $basicPages = BasicPageFactory::createMany(2, [ + 'publish' => true, + 'location' => BasicPage::LOCATION_FOOTER, + ]); + + $basicPageRepository = $this->createStub(BasicPageRepository::class); + $basicPageRepository->method('findBy')->willReturn($basicPages); + + $basicPageRuntime = new BasicPageRuntime($basicPageRepository); + + $runtimeLoader = $this->createMock(RuntimeLoaderInterface::class); + $runtimeLoader->method('load')->with(BasicPageRuntime::class)->willReturn($basicPageRuntime); + + return [$runtimeLoader]; + } +} diff --git a/packages/cookies-bundle/composer.json b/packages/cookies-bundle/composer.json index 646385ed..d31a1b88 100644 --- a/packages/cookies-bundle/composer.json +++ b/packages/cookies-bundle/composer.json @@ -24,6 +24,7 @@ "doctrine/persistence": "^2.0", "friendsofsymfony/ckeditor-bundle": "^2.2", "knplabs/doctrine-behaviors": "^2.0.6", + "psr/container": "^1.1 || ^2.0", "runroom-packages/form-handler-bundle": "^0.11@dev", "sonata-project/admin-bundle": "^3.103 || ^4.0@dev", "sonata-project/doctrine-orm-admin-bundle": "^3.34 || ^4.0@dev", @@ -33,7 +34,8 @@ "symfony/form": "^4.4 || ^5.3", "symfony/http-foundation": "^4.4.7 || ^5.3", "symfony/http-kernel": "^4.4.13 || ^5.3", - "symfony/validator": "^4.4 || ^5.3" + "symfony/validator": "^4.4 || ^5.3", + "twig/twig": "^2.12 || ^3.0" }, "require-dev": { "a2lix/auto-form-bundle": "^0.3.1", @@ -41,6 +43,7 @@ "matthiasnoback/symfony-dependency-injection-test": "^4.1", "phpunit/phpunit": "^9.5", "runroom-packages/testing": "^0.11@dev", + "symfony/browser-kit": "^5.3", "symfony/phpunit-bridge": "^5.3", "zenstruck/foundry": "^1.10" }, diff --git a/packages/cookies-bundle/src/DependencyInjection/RunroomCookiesExtension.php b/packages/cookies-bundle/src/DependencyInjection/RunroomCookiesExtension.php index cdaf086d..0fbdf5a2 100644 --- a/packages/cookies-bundle/src/DependencyInjection/RunroomCookiesExtension.php +++ b/packages/cookies-bundle/src/DependencyInjection/RunroomCookiesExtension.php @@ -14,7 +14,7 @@ namespace Runroom\CookiesBundle\DependencyInjection; use Runroom\CookiesBundle\Service\CookiesPageService; -use Runroom\CookiesBundle\Service\CookiesService; +use Runroom\CookiesBundle\Twig\CookiesRuntime; use Symfony\Component\Config\FileLocator; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Loader\PhpFileLoader; @@ -30,7 +30,7 @@ public function load(array $configs, ContainerBuilder $container): void $configuration = new Configuration(); $config = $this->processConfiguration($configuration, $configs); - $definition = $container->getDefinition(CookiesService::class); + $definition = $container->getDefinition(CookiesRuntime::class); $definition->setArgument('$cookies', $config['cookies']); $definition = $container->getDefinition(CookiesPageService::class); diff --git a/packages/cookies-bundle/src/Resources/config/services.php b/packages/cookies-bundle/src/Resources/config/services.php index eb6cd8dd..e8d4da89 100644 --- a/packages/cookies-bundle/src/Resources/config/services.php +++ b/packages/cookies-bundle/src/Resources/config/services.php @@ -11,18 +11,21 @@ * file that was distributed with this source code. */ +use Psr\Container\ContainerInterface; use Runroom\CookiesBundle\Admin\CookiesPageAdmin; use Runroom\CookiesBundle\Controller\CookiesPageController; use Runroom\CookiesBundle\Entity\CookiesPage; use Runroom\CookiesBundle\Repository\CookiesPageRepository; use Runroom\CookiesBundle\Service\CookiesPageService; -use Runroom\CookiesBundle\Service\CookiesService; +use Runroom\CookiesBundle\Twig\CookiesExtension; +use Runroom\CookiesBundle\Twig\CookiesRuntime; use Runroom\FormHandlerBundle\FormHandler; use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; use Symfony\Component\DependencyInjection\Loader\Configurator\ReferenceConfigurator; return static function (ContainerConfigurator $containerConfigurator): void { // Use "service" function for creating references to services when dropping support for Symfony 4.4 + // Use "abstract_arg" function for creating references to arguments without value when dropping support for Symfony 4.4 $services = $containerConfigurator->services(); $services->set(CookiesPageAdmin::class) @@ -31,7 +34,10 @@ ->tag('sonata.admin', ['manager_type' => 'orm', 'label' => 'Cookies']); $services->set(CookiesPageController::class) + ->public() ->arg('$service', new ReferenceConfigurator(CookiesPageService::class)) + ->call('setContainer', [new ReferenceConfigurator(ContainerInterface::class)]) + ->tag('container.service_subscriber') ->tag('controller.service_arguments'); $services->set(CookiesPageService::class) @@ -39,11 +45,14 @@ ->arg('$handler', new ReferenceConfigurator(FormHandler::class)) ->arg('$cookies', null); - $services->set(CookiesService::class) - ->arg('$cookies', null) - ->tag('kernel.event_subscriber'); - $services->set(CookiesPageRepository::class) ->arg('$registry', new ReferenceConfigurator('doctrine')) ->tag('doctrine.repository_service'); + + $services->set(CookiesExtension::class) + ->tag('twig.extension'); + + $services->set(CookiesRuntime::class) + ->arg('$cookies', null) + ->tag('twig.runtime'); }; diff --git a/packages/cookies-bundle/src/Resources/views/show.html.twig b/packages/cookies-bundle/src/Resources/views/show.html.twig index 8281088e..79f5b7d9 100644 --- a/packages/cookies-bundle/src/Resources/views/show.html.twig +++ b/packages/cookies-bundle/src/Resources/views/show.html.twig @@ -1,27 +1,27 @@ -

{{ page.content.cookiesPage.title }}

-
{{ page.content.cookiesPage.content|raw }}
+

{{ model.cookiesPage.title }}

+
{{ model.cookiesPage.content|raw }}
-{{- form_start(page.content.formView) -}} +{{- form_start(model.formView) -}}

Mandatory Cookies

- {{- form_row(page.content.formView.mandatoryCookies) -}} + {{- form_row(model.formView.mandatoryCookies) -}} {% include '@RunroomCookies/_cookies-list.html.twig' with { - cookiesList: page.content.cookies.mandatory_cookies + cookiesList: model.cookies.mandatory_cookies } %} -

Performance Cookies

- {{- form_row(page.content.formView.performanceCookies) -}} +

Performance Cookie s

+ {{- form_row(model.formView.performanceCookies) -}} {% include '@RunroomCookies/_cookies-list.html.twig' with { - cookiesList: page.content.cookies.performance_cookies + cookiesList: model.cookies.performance_cookies } %}

Targeting Cookies

- {{- form_row(page.content.formView.targetingCookies) -}} + {{- form_row(model.formView.targetingCookies) -}} {% include '@RunroomCookies/_cookies-list.html.twig' with { - cookiesList: page.content.cookies.targeting_cookies + cookiesList: model.cookies.targeting_cookies } %} - {{- form_row(page.content.formView.send) -}} -{{- form_end(page.content.formView) -}} + {{- form_row(model.formView.send) -}} +{{- form_end(model.formView) -}} diff --git a/packages/cookies-bundle/src/Service/CookiesPageService.php b/packages/cookies-bundle/src/Service/CookiesPageService.php index 14932e78..b0de6218 100644 --- a/packages/cookies-bundle/src/Service/CookiesPageService.php +++ b/packages/cookies-bundle/src/Service/CookiesPageService.php @@ -45,12 +45,12 @@ public function getCookiesPageViewModel(): FormAwareInterface { $cookiesPage = $this->repository->find(self::COOKIES_PAGE_ID); - $viewModel = new CookiesPageViewModel(); - - if (null !== $cookiesPage) { - $viewModel->setCookiesPage($cookiesPage); + if (null === $cookiesPage) { + throw new \RuntimeException('Cookies page not found, did you forget to generate it?'); } + $viewModel = new CookiesPageViewModel(); + $viewModel->setCookiesPage($cookiesPage); $viewModel->setCookies($this->cookies); return $this->handler->handleForm(CookiesFormType::class, [], $viewModel); diff --git a/packages/cookies-bundle/src/Twig/CookiesExtension.php b/packages/cookies-bundle/src/Twig/CookiesExtension.php new file mode 100644 index 00000000..c0460c37 --- /dev/null +++ b/packages/cookies-bundle/src/Twig/CookiesExtension.php @@ -0,0 +1,27 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Runroom\CookiesBundle\Twig; + +use Twig\Extension\AbstractExtension; +use Twig\TwigFunction; + +class CookiesExtension extends AbstractExtension +{ + public function getFunctions(): array + { + return [ + new TwigFunction('build_cookies', [CookiesRuntime::class, 'buildCookies']), + ]; + } +} diff --git a/packages/cookies-bundle/src/Service/CookiesService.php b/packages/cookies-bundle/src/Twig/CookiesRuntime.php similarity index 65% rename from packages/cookies-bundle/src/Service/CookiesService.php rename to packages/cookies-bundle/src/Twig/CookiesRuntime.php index 0c8bb416..a6550382 100644 --- a/packages/cookies-bundle/src/Service/CookiesService.php +++ b/packages/cookies-bundle/src/Twig/CookiesRuntime.php @@ -11,13 +11,12 @@ * file that was distributed with this source code. */ -namespace Runroom\CookiesBundle\Service; +namespace Runroom\CookiesBundle\Twig; use Runroom\CookiesBundle\ViewModel\CookiesViewModel; -use Runroom\RenderEventBundle\Event\PageRenderEvent; -use Symfony\Component\EventDispatcher\EventSubscriberInterface; +use Twig\Extension\RuntimeExtensionInterface; -final class CookiesService implements EventSubscriberInterface +class CookiesRuntime implements RuntimeExtensionInterface { private const TYPE_PERFORMANCE = 'performance_cookies'; private const TYPE_TARGETING = 'targeting_cookies'; @@ -31,21 +30,7 @@ public function __construct(array $cookies) $this->cookies = $cookies; } - public function onPageRender(PageRenderEvent $event): void - { - $page = $event->getPageViewModel(); - $page->addContext('cookies', $this->buildCookiesViewModel()); - $event->setPageViewModel($page); - } - - public static function getSubscribedEvents() - { - return [ - PageRenderEvent::EVENT_NAME => 'onPageRender', - ]; - } - - private function buildCookiesViewModel(): CookiesViewModel + public function buildCookies(): CookiesViewModel { $model = new CookiesViewModel(); $model->setPerformanceCookies($this->getCookies(self::TYPE_PERFORMANCE)); diff --git a/packages/cookies-bundle/tests/Fixtures/Twig/build_cookies.test b/packages/cookies-bundle/tests/Fixtures/Twig/build_cookies.test new file mode 100644 index 00000000..37fb8c80 --- /dev/null +++ b/packages/cookies-bundle/tests/Fixtures/Twig/build_cookies.test @@ -0,0 +1,11 @@ +--TEST-- +"build_cookies" function +--TEMPLATE-- +{% set cookies = build_cookies() %} +{{ cookies.performanceCookies|json_encode|raw }} +{{ cookies.targetingCookies|json_encode|raw }} +--DATA-- +return [] +--EXPECT-- +["test1","test2","test3","test4","test5","test6"] +["test1","test2","test3","test4","test5","test6"] diff --git a/packages/cookies-bundle/tests/Functional/CookiesPageControllerTest.php b/packages/cookies-bundle/tests/Functional/CookiesPageControllerTest.php new file mode 100644 index 00000000..a990445c --- /dev/null +++ b/packages/cookies-bundle/tests/Functional/CookiesPageControllerTest.php @@ -0,0 +1,45 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Runroom\CookiesBundle\Tests\Functional; + +use Runroom\CookiesBundle\Factory\CookiesPageFactory; +use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; +use Zenstruck\Foundry\Test\Factories; +use Zenstruck\Foundry\Test\ResetDatabase; + +class CookiesPageControllerTest extends WebTestCase +{ + use Factories; + use ResetDatabase; + + /** @test */ + public function itRendersCookiesPage(): void + { + $client = static::createClient(); + + CookiesPageFactory::createOne(); + + $client->request('GET', '/cookies-policy'); + + self::assertResponseIsSuccessful(); + + $client->request('GET', '/politica-de-cookies'); + + self::assertResponseIsSuccessful(); + + $client->request('GET', '/cookies-policy'); + + self::assertResponseIsSuccessful(); + } +} diff --git a/packages/cookies-bundle/tests/Integration/CookiesExtensionTest.php b/packages/cookies-bundle/tests/Integration/CookiesExtensionTest.php new file mode 100644 index 00000000..d9f3f187 --- /dev/null +++ b/packages/cookies-bundle/tests/Integration/CookiesExtensionTest.php @@ -0,0 +1,67 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Runroom\CookiesBundle\Tests\Integration; + +use Runroom\CookiesBundle\Twig\CookiesExtension; +use Runroom\CookiesBundle\Twig\CookiesRuntime; +use Twig\RuntimeLoader\RuntimeLoaderInterface; +use Twig\Test\IntegrationTestCase; + +class CookiesExtensionTest extends IntegrationTestCase +{ + public function getFixturesDir() + { + return __DIR__ . '/../Fixtures/Twig/'; + } + + protected function getExtensions() + { + return [ + new CookiesExtension(), + ]; + } + + protected function getRuntimeLoaders() + { + $cookies = [ + 'performance_cookies' => [ + 'category1' => [ + 'name' => 'test', + 'cookies' => ['test1', 'test2', 'test3'], + ], + 'category2' => [ + 'name' => 'test2', + 'cookies' => ['test4', 'test5', 'test6'], + ], + ], + 'targeting_cookies' => [ + 'category1' => [ + 'name' => 'test', + 'cookies' => ['test1', 'test2', 'test3'], + ], + 'category2' => [ + 'name' => 'test2', + 'cookies' => ['test4', 'test5', 'test6'], + ], + ], + ]; + + $cookiesRuntime = new CookiesRuntime($cookies); + + $runtimeLoader = $this->createMock(RuntimeLoaderInterface::class); + $runtimeLoader->method('load')->with(CookiesRuntime::class)->willReturn($cookiesRuntime); + + return [$runtimeLoader]; + } +} diff --git a/packages/cookies-bundle/tests/Integration/RunroomCookiesExtensionTest.php b/packages/cookies-bundle/tests/Integration/RunroomCookiesExtensionTest.php index 3db7827f..fc92d770 100644 --- a/packages/cookies-bundle/tests/Integration/RunroomCookiesExtensionTest.php +++ b/packages/cookies-bundle/tests/Integration/RunroomCookiesExtensionTest.php @@ -19,7 +19,8 @@ use Runroom\CookiesBundle\DependencyInjection\RunroomCookiesExtension; use Runroom\CookiesBundle\Repository\CookiesPageRepository; use Runroom\CookiesBundle\Service\CookiesPageService; -use Runroom\CookiesBundle\Service\CookiesService; +use Runroom\CookiesBundle\Twig\CookiesExtension; +use Runroom\CookiesBundle\Twig\CookiesRuntime; class RunroomCookiesExtensionTest extends AbstractExtensionTestCase { @@ -46,8 +47,9 @@ public function itHasCoreServicesAlias(): void $this->assertContainerBuilderHasService(CookiesPageAdmin::class); $this->assertContainerBuilderHasService(CookiesPageController::class); $this->assertContainerBuilderHasService(CookiesPageService::class); - $this->assertContainerBuilderHasService(CookiesService::class); $this->assertContainerBuilderHasService(CookiesPageRepository::class); + $this->assertContainerBuilderHasService(CookiesExtension::class); + $this->assertContainerBuilderHasService(CookiesRuntime::class); } protected function getContainerExtensions(): array diff --git a/packages/cookies-bundle/tests/Unit/CookiesPageServiceTest.php b/packages/cookies-bundle/tests/Unit/CookiesPageServiceTest.php index 674848d1..45ca9280 100644 --- a/packages/cookies-bundle/tests/Unit/CookiesPageServiceTest.php +++ b/packages/cookies-bundle/tests/Unit/CookiesPageServiceTest.php @@ -47,6 +47,15 @@ protected function setUp(): void ); } + /** @test */ + public function itThrowsExceptionIfCoookiesPageNotFound(): void + { + $this->expectException(\RuntimeException::class); + $this->expectExceptionMessage('Cookies page not found, did you forget to generate it?'); + + $this->service->getCookiesPageViewModel(); + } + /** @test */ public function itGetsCookiesPage(): void { diff --git a/packages/cookies-bundle/tests/Unit/CookiesServiceTest.php b/packages/cookies-bundle/tests/Unit/CookiesServiceTest.php deleted file mode 100644 index 5d43172b..00000000 --- a/packages/cookies-bundle/tests/Unit/CookiesServiceTest.php +++ /dev/null @@ -1,60 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Runroom\CookiesBundle\Tests\Unit; - -use PHPUnit\Framework\TestCase; -use Runroom\CookiesBundle\Service\CookiesService; -use Runroom\CookiesBundle\ViewModel\CookiesViewModel; -use Runroom\RenderEventBundle\Event\PageRenderEvent; -use Runroom\RenderEventBundle\ViewModel\PageViewModel; -use Symfony\Component\HttpFoundation\Response; - -class CookiesServiceTest extends TestCase -{ - private CookiesService $service; - - protected function setUp(): void - { - $this->service = new CookiesService($this->buildCookiesArray()); - } - - /** @test */ - public function itSetsCookies(): void - { - $event = new PageRenderEvent('view', new PageViewModel(), new Response()); - - $this->service->onPageRender($event); - - $cookies = $event->getPageViewModel()->getContext('cookies'); - - self::assertInstanceOf(CookiesViewModel::class, $cookies); - self::assertSame(['cookie 1', 'cookie 2', 'cookie 3', 'cookie 4'], $cookies->getPerformanceCookies()); - self::assertSame(['cookie 5', 'cookie 6', 'cookie 7', 'cookie 8'], $cookies->getTargetingCookies()); - } - - /** @return array */ - private function buildCookiesArray(): array - { - return [ - 'performance_cookies' => [ - ['name' => 'category 1', 'cookies' => ['cookie 1', 'cookie 2']], - ['name' => 'category 2', 'cookies' => ['cookie 3', 'cookie 4']], - ], - 'targeting_cookies' => [ - ['name' => 'category 3', 'cookies' => ['cookie 5', 'cookie 6']], - ['name' => 'category 4', 'cookies' => ['cookie 7', 'cookie 8']], - ], - ]; - } -} diff --git a/packages/seo-bundle/src/Context/DefaultContextExtractor.php b/packages/seo-bundle/src/Context/DefaultContextExtractor.php index ba83a441..400c8d52 100644 --- a/packages/seo-bundle/src/Context/DefaultContextExtractor.php +++ b/packages/seo-bundle/src/Context/DefaultContextExtractor.php @@ -24,7 +24,11 @@ public function __construct(string $modelKey) $this->modelKey = $modelKey; } - /** @param array $context */ + /** + * @param array $context + * + * @throws \RuntimeException if model is not found or not instance of SeoModelInterface + */ public function extract(array $context): SeoModelInterface { if (!isset($context[$this->modelKey])) { diff --git a/packages/seo-bundle/src/Resources/config/services.php b/packages/seo-bundle/src/Resources/config/services.php index d4dced20..228b9ab8 100644 --- a/packages/seo-bundle/src/Resources/config/services.php +++ b/packages/seo-bundle/src/Resources/config/services.php @@ -32,6 +32,7 @@ return static function (ContainerConfigurator $containerConfigurator): void { // Use "service" function for creating references to services when dropping support for Symfony 4.4 + // Use "abstract_arg" function for creating references to arguments without value when dropping support for Symfony 4.4 $services = $containerConfigurator->services(); $services->set(MetaInformationAdmin::class) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index d672311d..ab5b80b4 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -10,11 +10,6 @@ parameters: count: 1 path: packages/basic-page-bundle/src/Factory/BasicPageTranslationFactory.php - - - message: "#^Possibly invalid array key type int\\|object\\|string\\.$#" - count: 1 - path: packages/ckeditor-sonata-media-bundle/src/Controller/MediaAdminController.php - - message: "#^Call to an undefined method Faker\\\\UniqueGenerator\\:\\:languageCode\\(\\)\\.$#" count: 1