BlockPoolTest.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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\BlockPool;
  8. /**
  9. * Test for view BlockPool model
  10. */
  11. class BlockPoolTest extends \PHPUnit\Framework\TestCase
  12. {
  13. /**
  14. * @var BlockPool
  15. */
  16. protected $blockPool;
  17. /**
  18. * Block factory
  19. * @var \Magento\Framework\View\Element\BlockFactory|\PHPUnit_Framework_MockObject_MockObject
  20. */
  21. protected $blockFactory;
  22. protected function setUp()
  23. {
  24. $this->blockFactory = $this->getMockBuilder(\Magento\Framework\View\Element\BlockFactory::class)
  25. ->disableOriginalConstructor()
  26. ->setMethods(['createBlock'])
  27. ->getMock();
  28. $this->blockPool = new BlockPool($this->blockFactory);
  29. }
  30. public function testAdd()
  31. {
  32. $blockName = 'testName';
  33. $blockClass = \Magento\Framework\View\Test\Unit\BlockPoolTestBlock::class;
  34. $arguments = ['key' => 'value'];
  35. $block = $this->createMock(\Magento\Framework\View\Test\Unit\BlockPoolTestBlock::class);
  36. $this->blockFactory->expects($this->atLeastOnce())
  37. ->method('createBlock')
  38. ->with($blockClass, $arguments)
  39. ->will($this->returnValue($block));
  40. $this->assertEquals($this->blockPool, $this->blockPool->add($blockName, $blockClass, $arguments));
  41. $this->assertEquals([$blockName => $block], $this->blockPool->get());
  42. $this->assertEquals($block, $this->blockPool->get($blockName));
  43. $this->assertNull($this->blockPool->get('someWrongName'));
  44. }
  45. /**
  46. * @expectedException \InvalidArgumentException
  47. * @expectedExceptionMessage Invalid Block class name: NotExistingBlockClass
  48. */
  49. public function testAddWithException()
  50. {
  51. $this->blockPool->add('BlockPoolTestBlock', 'NotExistingBlockClass');
  52. }
  53. }