PhpTest.php 3.2 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\TemplateEngine;
  7. class PhpTest extends \PHPUnit\Framework\TestCase
  8. {
  9. const TEST_PROP_VALUE = 'TEST_PROP_VALUE';
  10. /** @var \Magento\Framework\View\TemplateEngine\Php */
  11. protected $_phpEngine;
  12. /**
  13. * @var \PHPUnit_Framework_MockObject_MockObject
  14. */
  15. protected $_helperFactoryMock;
  16. /**
  17. * Create a PHP template engine to test.
  18. */
  19. protected function setUp()
  20. {
  21. $this->_helperFactoryMock = $this->createMock(\Magento\Framework\ObjectManagerInterface::class);
  22. $this->_phpEngine = new \Magento\Framework\View\TemplateEngine\Php($this->_helperFactoryMock);
  23. }
  24. /**
  25. * Test the render() function with a very simple .phtml file.
  26. *
  27. * Note: the call() function will be covered because simple.phtml has a call to the block.
  28. */
  29. public function testRender()
  30. {
  31. $blockMock = $this->getMockBuilder(
  32. \Magento\Framework\View\Element\Template::class
  33. )->setMethods(
  34. ['testMethod']
  35. )->disableOriginalConstructor()->getMock();
  36. $blockMock->expects($this->once())->method('testMethod');
  37. $blockMock->property = self::TEST_PROP_VALUE;
  38. $filename = __DIR__ . '/_files/simple.phtml';
  39. $actualOutput = $this->_phpEngine->render($blockMock, $filename);
  40. $this->assertAttributeEquals(null, '_currentBlock', $this->_phpEngine);
  41. $expectedOutput = '<html>' . self::TEST_PROP_VALUE . '</html>' . PHP_EOL;
  42. $this->assertSame($expectedOutput, $actualOutput, 'phtml file did not render correctly');
  43. }
  44. /**
  45. * Test the render() function with a nonexistent filename.
  46. *
  47. * Expect an exception if the specified file does not exist.
  48. * @expectedException \Exception
  49. */
  50. public function testRenderException()
  51. {
  52. $blockMock = $this->getMockBuilder(
  53. \Magento\Framework\View\Element\Template::class
  54. )->setMethods(
  55. ['testMethod']
  56. )->disableOriginalConstructor()->getMock();
  57. $filename = 'This_is_not_a_file';
  58. $this->_phpEngine->render($blockMock, $filename);
  59. }
  60. /**
  61. * @expectedException \LogicException
  62. */
  63. public function testHelperWithInvalidClass()
  64. {
  65. $class = \Magento\Framework\DataObject::class;
  66. $object = $this->createMock($class);
  67. $this->_helperFactoryMock->expects(
  68. $this->once()
  69. )->method(
  70. 'get'
  71. )->with(
  72. $class
  73. )->will(
  74. $this->returnValue($object)
  75. );
  76. $this->_phpEngine->helper($class);
  77. }
  78. public function testHelperWithValidClass()
  79. {
  80. $class = \Magento\Framework\App\Helper\AbstractHelper::class;
  81. $object = $this->getMockForAbstractClass($class, [], '', false);
  82. $this->_helperFactoryMock->expects(
  83. $this->once()
  84. )->method(
  85. 'get'
  86. )->with(
  87. $class
  88. )->will(
  89. $this->returnValue($object)
  90. );
  91. $this->assertEquals($object, $this->_phpEngine->helper($class));
  92. }
  93. }