'[Hello, weather!]'], $zone = RequestZone::BACKEND): Widgets { $queueProcessor = new QueueProcessor(new HtmlInjector()); $requestStack = new RequestStack(); $request = Request::createFromGlobals(); RequestZone::setToRequest($request, $zone); $requestStack->push($request); $loader = new ArrayLoader($templates); $twig = new Environment($loader); $cache = new Psr6Cache(new TraceableAdapter(new FilesystemAdapter())); $stopwatch = new Stopwatch(); return new Widgets($requestStack, $queueProcessor, $twig, $cache, $stopwatch); } public function testProcessWidgetsInQueue(): void { $widgets = $this->getWidgetsObject(); $response = new Response('foo'); $snippet = (new SnippetWidget()) ->setTemplate('*foo*') ->setZone(RequestZone::EVERYWHERE) ->setTarget(Target::END_OF_BODY); $widgets->registerWidget($snippet); $widgets->processQueue($response); $this->assertSameHtml("foo*foo*", $response->getContent()); } public function testRenderWidget(): void { $widgets = $this->getWidgetsObject(); $weatherWidget = new WeatherWidget(); $weatherWidget->setTemplate('weather.twig'); $widgets->registerWidget($weatherWidget); $this->assertSameHtml( '
[Hello, weather!]
', $widgets->renderWidgetByName('Weather Widget') ); } public function testRenderWidgetWithExtraParameters(): void { $widgets = $this->getWidgetsObject(['dummy.twig' => '[Hello, {{ foo }}!]']); $widget = new DummyWidget(); $widget->setTemplate('dummy.twig'); $widgets->registerWidget($widget); $this->assertSameHtml( '
[Hello, Bar!]
', $widgets->renderWidgetByName('Dummy Widget', ['foo' => 'Bar']) ); } public function testProcessHeaderWidget(): void { $widgets = $this->getWidgetsObject([], RequestZone::FRONTEND); $response = new Response('foo'); $headerWidget = new BoltHeaderWidget(); $widgets->registerWidget($headerWidget); $widgets->processQueue($response); $this->assertSameHtml('Bolt', $response->headers->get('X-Powered-By')); } public function testProcessWeatherWidgetInTarget(): void { $widgets = $this->getWidgetsObject(); $response = new Response('foo'); $weather = new WeatherWidget(); // overwrite things just to simplify test $weather->setTarget(Target::END_OF_BODY); $weather->setTemplate('weather.twig'); $widgets->registerWidget($weather); $widgets->processQueue($response); $this->assertSameHtml( 'foo
[Hello, weather!]
', $response->getContent() ); } public function testProcessWeatherWidgetInTarget2(): void { $widgets = $this->getWidgetsObject(); $response = new Response('foo'); $weather = new WeatherWidget(); // overwrite things just to simplify test $weather->setTarget(Target::START_OF_BODY); $weather->setTemplate('weather.twig'); $widgets->registerWidget($weather); $widgets->processQueue($response); $this->assertSameHtml( '
[Hello, weather!]
foo', $response->getContent() ); } }