DataSourcePoolTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\View\Test\Unit;
  7. use \Magento\Framework\View\DataSourcePool;
  8. /**
  9. * Test for view Context model
  10. */
  11. class DataSourcePoolTest extends \PHPUnit\Framework\TestCase
  12. {
  13. /**
  14. * @var DataSourcePool
  15. */
  16. protected $dataSourcePool;
  17. /**
  18. * @var \Magento\Framework\View\Element\BlockFactory|\PHPUnit_Framework_MockObject_MockObject
  19. */
  20. protected $blockFactory;
  21. protected function setUp()
  22. {
  23. $this->blockFactory = $this->getMockBuilder(\Magento\Framework\View\Element\BlockFactory::class)
  24. ->disableOriginalConstructor()
  25. ->getMock();
  26. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  27. $this->dataSourcePool = $objectManager->getObject(
  28. \Magento\Framework\View\DataSourcePool::class,
  29. ['blockFactory' => $this->blockFactory]
  30. );
  31. }
  32. /**
  33. * @expectedException \InvalidArgumentException
  34. * @expectedExceptionMessage Invalid Data Source class name: NotExistingBlockClass
  35. */
  36. public function testAddWithException()
  37. {
  38. $this->dataSourcePool->add('DataSourcePoolTestBlock', 'NotExistingBlockClass');
  39. }
  40. /**
  41. * @param $blockClass
  42. * @return \PHPUnit_Framework_MockObject_MockObject
  43. */
  44. protected function createBlock($blockClass)
  45. {
  46. $block = $this->createMock(\Magento\Framework\View\Element\BlockInterface::class);
  47. $this->blockFactory->expects($this->once())
  48. ->method('createBlock')
  49. ->with($blockClass)
  50. ->will($this->returnValue($block));
  51. return $block;
  52. }
  53. public function testAdd()
  54. {
  55. $blockName = 'DataSourcePoolTestBlock';
  56. $blockClass = \Magento\Framework\View\Test\Unit\DataSourcePoolTestBlock::class;
  57. $block = $this->createBlock($blockClass);
  58. $this->assertSame($block, $this->dataSourcePool->add($blockName, $blockClass));
  59. }
  60. public function testGet()
  61. {
  62. $blockName = 'DataSourcePoolTestBlock';
  63. $blockClass = \Magento\Framework\View\Test\Unit\DataSourcePoolTestBlock::class;
  64. $block = $this->createBlock($blockClass);
  65. $this->dataSourcePool->add($blockName, $blockClass);
  66. $this->assertSame($block, $this->dataSourcePool->get($blockName));
  67. $this->assertEquals([$blockName => $block], $this->dataSourcePool->get());
  68. $this->assertNull($this->dataSourcePool->get('WrongName'));
  69. }
  70. public function testGetEmpty()
  71. {
  72. $this->assertEquals([], $this->dataSourcePool->get());
  73. }
  74. public function testAssignAndGetNamespaceData()
  75. {
  76. $blockName = 'DataSourcePoolTestBlock';
  77. $blockClass = \Magento\Framework\View\Test\Unit\DataSourcePoolTestBlock::class;
  78. $block = $this->createBlock($blockClass);
  79. $this->dataSourcePool->add($blockName, $blockClass);
  80. $namespace = 'namespace';
  81. $alias = 'alias';
  82. $this->dataSourcePool->assign($blockName, $namespace, $alias);
  83. $this->assertEquals(['alias' => $block], $this->dataSourcePool->getNamespaceData($namespace));
  84. $this->assertEquals([], $this->dataSourcePool->getNamespaceData('WrongNamespace'));
  85. }
  86. }