Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
DataSourcePoolTest.php
Go to the documentation of this file.
1 <?php
8 
9 use \Magento\Framework\View\DataSourcePool;
10 
14 class DataSourcePoolTest extends \PHPUnit\Framework\TestCase
15 {
19  protected $dataSourcePool;
20 
24  protected $blockFactory;
25 
26  protected function setUp()
27  {
28  $this->blockFactory = $this->getMockBuilder(\Magento\Framework\View\Element\BlockFactory::class)
29  ->disableOriginalConstructor()
30  ->getMock();
31 
32  $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
33  $this->dataSourcePool = $objectManager->getObject(
34  \Magento\Framework\View\DataSourcePool::class,
35  ['blockFactory' => $this->blockFactory]
36  );
37  }
38 
43  public function testAddWithException()
44  {
45  $this->dataSourcePool->add('DataSourcePoolTestBlock', 'NotExistingBlockClass');
46  }
47 
52  protected function createBlock($blockClass)
53  {
54  $block = $this->createMock(\Magento\Framework\View\Element\BlockInterface::class);
55 
56  $this->blockFactory->expects($this->once())
57  ->method('createBlock')
58  ->with($blockClass)
59  ->will($this->returnValue($block));
60  return $block;
61  }
62 
63  public function testAdd()
64  {
65  $blockName = 'DataSourcePoolTestBlock';
66  $blockClass = \Magento\Framework\View\Test\Unit\DataSourcePoolTestBlock::class;
67 
68  $block = $this->createBlock($blockClass);
69 
70  $this->assertSame($block, $this->dataSourcePool->add($blockName, $blockClass));
71  }
72 
73  public function testGet()
74  {
75  $blockName = 'DataSourcePoolTestBlock';
76  $blockClass = \Magento\Framework\View\Test\Unit\DataSourcePoolTestBlock::class;
77 
78  $block = $this->createBlock($blockClass);
79  $this->dataSourcePool->add($blockName, $blockClass);
80 
81  $this->assertSame($block, $this->dataSourcePool->get($blockName));
82  $this->assertEquals([$blockName => $block], $this->dataSourcePool->get());
83  $this->assertNull($this->dataSourcePool->get('WrongName'));
84  }
85 
86  public function testGetEmpty()
87  {
88  $this->assertEquals([], $this->dataSourcePool->get());
89  }
90 
92  {
93  $blockName = 'DataSourcePoolTestBlock';
94  $blockClass = \Magento\Framework\View\Test\Unit\DataSourcePoolTestBlock::class;
95 
96  $block = $this->createBlock($blockClass);
97  $this->dataSourcePool->add($blockName, $blockClass);
98 
99  $namespace = 'namespace';
100  $alias = 'alias';
101  $this->dataSourcePool->assign($blockName, $namespace, $alias);
102 
103  $this->assertEquals(['alias' => $block], $this->dataSourcePool->getNamespaceData($namespace));
104  $this->assertEquals([], $this->dataSourcePool->getNamespaceData('WrongNamespace'));
105  }
106 }
$objectManager
Definition: bootstrap.php:17
$block
Definition: block.php:8
if(!trim($html)) $alias
Definition: details.phtml:20