public function testRenderEventWithListeners() { $blockService = $this->getMock('Sonata\\BlockBundle\\Block\\BlockServiceInterface'); $blockService->expects($this->once())->method('getJavascripts')->will($this->returnValue(array())); $blockService->expects($this->once())->method('getStylesheets')->will($this->returnValue(array())); $blockServiceManager = $this->getMock('Sonata\\BlockBundle\\Block\\BlockServiceManagerInterface'); $blockServiceManager->expects($this->any())->method('get')->will($this->returnValue($blockService)); $blockRenderer = $this->getMock('Sonata\\BlockBundle\\Block\\BlockRendererInterface'); $blockRenderer->expects($this->once())->method('render')->will($this->returnValue(new Response('<span>test</span>'))); $blockContextManager = $this->getMock('Sonata\\BlockBundle\\Block\\BlockContextManagerInterface'); $blockContextManager->expects($this->once())->method('get')->will($this->returnCallback(function (BlockInterface $block) { $context = new BlockContext($block, $block->getSettings()); return $context; })); $eventDispatcher = $this->getMock('Symfony\\Component\\EventDispatcher\\EventDispatcherInterface'); $eventDispatcher->expects($this->once())->method('dispatch')->will($this->returnCallback(function ($name, BlockEvent $event) { $block = new Block(); $block->setId(1); $block->setSettings(array('use_cache' => false)); $block->setType('test'); $event->addBlock($block); return $event; })); $helper = new BlockHelper($blockServiceManager, array(), $blockRenderer, $blockContextManager, $eventDispatcher); $this->assertEquals('<span>test</span>', $helper->renderEvent('my.event')); }
/** * Executes the block as specified in the content. * * @param array $block An array including the block name * * @return string the rendered block */ protected function embeddedRender($block) { try { return $this->sonataBlock->render(array('name' => trim($block[1]))); } catch (\Exception $e) { if ($this->logger) { $this->logger->warn('Failed to render block "' . $block[1] . '" embedded in content: ' . $e->getTraceAsString()); } } return ''; }
/** * Executes the block as specified in the content. * * @param $name * * @return string */ protected function embeddedRender($name) { $name = trim($name); try { return $this->sonataBlock->render(['name' => $name]); } catch (\Exception $e) { if ($this->logger) { $this->logger->warning(sprintf('Failed to render block "%s" embedded in content: %s', $name, $e->getTraceAsString())); } } return ''; }
/** * {@inheritdoc} */ public function collect(Request $request, Response $response, \Exception $exception = null) { $this->blocks = $this->blocksHelper->getTraces(); // split into containers & real blocks foreach ($this->blocks as $id => $block) { if (!is_array($block)) { return; // something went wrong while collecting information } if ($id == '_events') { foreach ($block as $uniqid => $event) { $this->events[$uniqid] = $event; } continue; } if (in_array($block['type'], $this->containerTypes)) { $this->containers[$id] = $block; } else { $this->realBlocks[$id] = $block; } } }
/** * @param PageBlockInterface $block * @param array $options * * @return string */ public function renderBlock(PageBlockInterface $block, array $options = array()) { if ($block->getEnabled() === false && !$this->cmsManagerSelector->isEditor()) { return ''; } // defined extra default key for the cache $pageCacheKeys = array('manager' => $block->getPage() instanceof SnapshotPageProxy ? 'snapshot' : 'page', 'page_id' => $block->getPage()->getId()); // build the parameters array $options = array_merge(array('use_cache' => isset($options['use_cache']) ? $options['use_cache'] : true, 'extra_cache_keys' => array()), $pageCacheKeys, $options); // make sure the parameters array contains all valid keys $options['extra_cache_keys'] = array_merge($options['extra_cache_keys'], $pageCacheKeys); return $this->blockHelper->render($block, $options); }
public function testRenderEventWithListeners() { $blockService = $this->getMock('Sonata\\BlockBundle\\Block\\BlockServiceInterface'); $blockService->expects($this->once())->method('getJavascripts')->will($this->returnValue(array('/js/base.js'))); $blockService->expects($this->once())->method('getStylesheets')->will($this->returnValue(array('/css/base.css'))); $blockServiceManager = $this->getMock('Sonata\\BlockBundle\\Block\\BlockServiceManagerInterface'); $blockServiceManager->expects($this->any())->method('get')->will($this->returnValue($blockService)); $blockRenderer = $this->getMock('Sonata\\BlockBundle\\Block\\BlockRendererInterface'); $blockRenderer->expects($this->once())->method('render')->will($this->returnValue(new Response('<span>test</span>'))); $blockContextManager = $this->getMock('Sonata\\BlockBundle\\Block\\BlockContextManagerInterface'); $blockContextManager->expects($this->once())->method('get')->will($this->returnCallback(function (BlockInterface $block) { $context = new BlockContext($block, $block->getSettings()); return $context; })); $eventDispatcher = $this->getMock('Symfony\\Component\\EventDispatcher\\EventDispatcherInterface'); $eventDispatcher->expects($this->once())->method('dispatch')->will($this->returnCallback(function ($name, BlockEvent $event) { $block = new Block(); $block->setId(1); $block->setSettings(array('use_cache' => false)); $block->setType('test'); $event->addBlock($block); return $event; })); $helper = new BlockHelper($blockServiceManager, array(), $blockRenderer, $blockContextManager, $eventDispatcher); $this->assertEquals('<span>test</span>', $helper->renderEvent('my.event')); $this->assertEquals(trim($helper->includeJavascripts('screen', '/application')), '<script src="/application/js/base.js" type="text/javascript"></script>'); $this->assertEquals(trim($helper->includeJavascripts('screen', '')), '<script src="/js/base.js" type="text/javascript"></script>'); $this->assertEquals($helper->includeStylesheets('screen', '/application'), <<<EXPECTED <style type='text/css' media='screen'> @import url(/application/css/base.css); </style> EXPECTED ); $this->assertEquals($helper->includeStylesheets('screen', ''), <<<EXPECTED <style type='text/css' media='screen'> @import url(/css/base.css); </style> EXPECTED ); }