/** * Add a block * * @param string $name * @param string $class * @param array $arguments [optional] * @return BlockPool * @throws \InvalidArgumentException */ public function add($name, $class, array $arguments = []) { if (!class_exists($class)) { throw new \InvalidArgumentException((string) new \Magento\Framework\Phrase('Invalid Block class name: %1', [$class])); } $block = $this->blockFactory->createBlock($class, $arguments); $this->blocks[$name] = $block; return $this; }
/** * Add a block * * @param string $name * @param string $class * @param array $arguments [optional] * @return BlockPool * @throws \InvalidArgumentException */ public function add($name, $class, array $arguments = array()) { if (!class_exists($class)) { throw new \InvalidArgumentException(__('Invalid Block class name: ' . $class)); } $block = $this->blockFactory->createBlock($class, $arguments); $this->blocks[$name] = $block; return $this; }
/** * Add data source * * @param string $name * @param string $class * @return object * @throws \Exception */ public function add($name, $class) { if (!isset($this->dataSources[$name])) { if (!class_exists($class)) { throw new \Exception(__('Invalid Data Source class name: ' . $class)); } $data = $this->blockFactory->createBlock($class); $this->dataSources[$name] = $data; } return $this->dataSources[$name]; }
/** * Add data source * * @param string $name * @param string $class * @return object * @throws \Exception */ public function add($name, $class) { if (!isset($this->dataSources[$name])) { if (!class_exists($class)) { throw new \InvalidArgumentException((string) new \Magento\Framework\Phrase('Invalid Data Source class name: %1', [$class])); } $data = $this->blockFactory->createBlock($class); $this->dataSources[$name] = $data; } return $this->dataSources[$name]; }
public function testAdd() { $blockName = 'testName'; $blockClass = '\\Magento\\Framework\\View\\BlockPoolTestBlock'; $arguments = ['key' => 'value']; $block = $this->getMock('Magento\\Framework\\View\\BlockPoolTestBlock'); $this->blockFactory->expects($this->atLeastOnce())->method('createBlock')->with($blockClass, $arguments)->will($this->returnValue($block)); $this->assertEquals($this->blockPool, $this->blockPool->add($blockName, $blockClass, $arguments)); $this->assertEquals([$blockName => $block], $this->blockPool->get()); $this->assertEquals($block, $this->blockPool->get($blockName)); $this->assertNull($this->blockPool->get('someWrongName')); }
public function testRenderRecurringPaymentForm() { $blockMock = $this->getMock('Magento\\Framework\\View\\Element\\BlockInterface', array('setNameInLayout', 'setParentElement', 'setProductEntity', 'toHtml', 'addFieldMap', 'addFieldDependence', 'addConfigOptions')); $map = array(array('Magento\\RecurringPayment\\Block\\Adminhtml\\Payment\\Edit\\Form', array(), $blockMock), array('Magento\\Backend\\Block\\Widget\\Form\\Element\\Dependence', array(), $blockMock)); $paymentElement = $this->getMock('Magento\\Framework\\Data\\Form\\Element\\AbstractElement', array(), array(), '', false); $this->_scopeConfig->expects($this->any())->method('getValue')->will($this->returnValue(true)); $this->_testModel->render($paymentElement); $product = $this->getMock('Magento\\Catalog\\Model\\Product', array(), array(), '', false); $this->_registry->expects($this->once())->method('registry')->will($this->returnValue($product)); $this->_blockFactory->expects($this->any())->method('createBlock')->will($this->returnValueMap($map)); $blockMock->expects($this->any())->method('setNameInLayout'); $blockMock->expects($this->once())->method('setProductEntity')->with($product); $blockMock->expects($this->exactly(2))->method('toHtml')->will($this->returnValue('html')); $blockMock->expects($this->once())->method('addConfigOptions')->with(array('levels_up' => 2)); $this->assertEquals('htmlhtml', $this->_testModel->getElementHtml()); }
/** * @param string $renderer * @return \Magento\Framework\View\Element\BlockInterface */ public function getRenderer($renderer) { if (is_string($renderer) && $renderer) { return $this->_blockFactory->createBlock($renderer, []); } else { return $renderer; } }
/** * Retrieve tooltip text * * @return string */ public function getTooltip() { if (isset($this->_data['tooltip'])) { return $this->_getTranslatedAttribute('tooltip'); } elseif (isset($this->_data['tooltip_block'])) { return $this->_blockFactory->createBlock($this->_data['tooltip_block'])->toHtml(); } return ''; }
/** * Element output getter * * @return string */ public function getElementHtml() { $product = $this->_coreRegistry->registry('current_product'); /** @var $formBlock \Magento\RecurringPayment\Block\Adminhtml\Payment\Edit\Form */ $formBlock = $this->_blockFactory->createBlock('Magento\\RecurringPayment\\Block\\Adminhtml\\Payment\\Edit\\Form'); $formBlock->setNameInLayout('adminhtml_recurring_payment_edit_form'); $formBlock->setParentElement($this->_element); $formBlock->setProductEntity($product); $output = $formBlock->toHtml(); // make the payment element dependent on is_recurring /** @var $dependencies \Magento\Backend\Block\Widget\Form\Element\Dependence */ $dependencies = $this->_blockFactory->createBlock('Magento\\Backend\\Block\\Widget\\Form\\Element\\Dependence'); $dependencies->setNameInLayout('adminhtml_recurring_payment_edit_form_dependence'); $dependencies->addFieldMap('is_recurring', 'product[is_recurring]'); $dependencies->addFieldMap($this->_element->getHtmlId(), $this->_element->getName()); $dependencies->addFieldDependence($this->_element->getName(), 'product[is_recurring]', '1'); $dependencies->addConfigOptions(array('levels_up' => 2)); $output .= $dependencies->toHtml(); return $output; }
public function testProcess() { $this->prepareScheduledStructure(); $this->readerContextMock = $this->getMockBuilder('Magento\\Framework\\View\\Layout\\Reader\\Context')->disableOriginalConstructor()->getMock(); $this->readerContextMock->expects($this->any())->method('getScheduledStructure')->willReturn($this->scheduledStructureMock); $generatorContextMock = $this->getMockBuilder('Magento\\Framework\\View\\Layout\\Generator\\Context')->disableOriginalConstructor()->getMock(); $structureMock = $this->getMockBuilder('Magento\\Framework\\View\\Layout\\Data\\Structure')->disableOriginalConstructor()->getMock(); $structureMock->expects($this->once())->method('addToParentGroup')->with(UiComponent::TYPE, 'new_group')->willReturnSelf(); $layoutMock = $this->getMockBuilder('Magento\\Framework\\View\\LayoutInterface')->getMockForAbstractClass(); $generatorContextMock->expects($this->any())->method('getStructure')->willReturn($structureMock); $generatorContextMock->expects($this->any())->method('getLayout')->willReturn($layoutMock); $this->uiComponentFactoryMock->expects($this->any())->method('setLayout')->with($layoutMock)->willReturnSelf(); $componentMock = $this->getMockForAbstractClass('Magento\\Framework\\View\\Element\\UiComponentInterface', [], '', false, true, true, []); $contextMock = $this->getMockForAbstractClass('Magento\\Framework\\View\\Element\\UiComponent\\ContextInterface', [], '', false); $blockMock = $this->getMockForAbstractClass('Magento\\Framework\\View\\Element\\BlockInterface', [], '', false); $this->contextFactoryMock->expects($this->once())->method('create')->with(['namespace' => 'uiComponent', 'pageLayout' => $layoutMock])->willReturn($contextMock); $this->uiComponentFactoryMock->expects($this->any())->method('create')->with('uiComponent', null, ['context' => $contextMock])->willReturn($componentMock); $this->blockFactoryMock->expects($this->once())->method('createBlock')->with(UiComponent::CONTAINER, ['component' => $componentMock])->willReturn($blockMock); $this->argumentInterpreterMock->expects($this->any())->method('evaluate')->will($this->returnValueMap([[['key_1' => 'value_1'], 'value_1'], [['key_2' => 'value_2'], 'value_2']])); $layoutMock->expects($this->any())->method('setBlock')->with(UiComponent::TYPE, $blockMock)->willReturnSelf(); $this->uiComponent->process($this->readerContextMock, $generatorContextMock); }
/** * Create block object instance based on block type * * @param string|\Magento\Framework\View\Element\AbstractBlock $block * @param array $arguments * @throws \Magento\Framework\Exception\LocalizedException * @return \Magento\Framework\View\Element\AbstractBlock */ protected function getBlockInstance($block, array $arguments = []) { if ($block && is_string($block)) { try { $block = $this->blockFactory->createBlock($block, $arguments); } catch (\ReflectionException $e) { $this->logger->critical($e->getMessage()); } } if (!$block instanceof \Magento\Framework\View\Element\AbstractBlock) { throw new \Magento\Framework\Exception\LocalizedException(new \Magento\Framework\Phrase('Invalid block type: %1', [$block])); } return $block; }
/** * Get block singleton * * @param string $type * @return \Magento\Framework\App\Helper\AbstractHelper * @throws \Magento\Framework\Model\Exception */ public function getBlockSingleton($type) { if (!isset($this->_helpers[$type])) { if (!$type) { throw new \Magento\Framework\Model\Exception('Invalid block type'); } $helper = $this->_blockFactory->createBlock($type); if ($helper) { if ($helper instanceof \Magento\Framework\View\Element\AbstractBlock) { $helper->setLayout($this); } $this->_helpers[$type] = $helper; } } return $this->_helpers[$type]; }
protected function createBlock($blockClass) { $block = $this->getMock('Magento\\Framework\\View\\Element\\BlockInterface'); $this->blockFactory->expects($this->once())->method('createBlock')->with($blockClass)->will($this->returnValue($block)); return $block; }
/** * @expectedException \LogicException */ public function testCreateBlockWithException() { $this->blockFactory->createBlock('invalid_class_name'); }