Layout.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Core layout utility
  8. */
  9. namespace Magento\Framework\View\Utility;
  10. /**
  11. * Class Layout
  12. *
  13. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  14. */
  15. class Layout
  16. {
  17. /**
  18. * @var \PHPUnit\Framework\TestCase
  19. */
  20. protected $_testCase;
  21. /**
  22. * @param \PHPUnit\Framework\TestCase $testCase
  23. */
  24. public function __construct(\PHPUnit\Framework\TestCase $testCase)
  25. {
  26. $this->_testCase = $testCase;
  27. }
  28. /**
  29. * Retrieve new layout update model instance with XML data from a fixture file
  30. *
  31. * @param string|array $layoutUpdatesFile
  32. * @return \Magento\Framework\View\Layout\ProcessorInterface
  33. */
  34. public function getLayoutUpdateFromFixture($layoutUpdatesFile)
  35. {
  36. $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  37. /** @var \Magento\Framework\View\File\Factory $fileFactory */
  38. $fileFactory = $objectManager->get(\Magento\Framework\View\File\Factory::class);
  39. $files = [];
  40. foreach ((array)$layoutUpdatesFile as $filename) {
  41. $files[] = $fileFactory->create($filename, 'Magento_View');
  42. }
  43. $fileSource = $this->_testCase
  44. ->getMockBuilder(\Magento\Framework\View\File\CollectorInterface::class)->getMockForAbstractClass();
  45. $fileSource->expects(
  46. \PHPUnit\Framework\TestCase::any()
  47. )->method(
  48. 'getFiles'
  49. )->will(
  50. \PHPUnit\Framework\TestCase::returnValue($files)
  51. );
  52. $pageLayoutFileSource = $this->_testCase
  53. ->getMockBuilder(\Magento\Framework\View\File\CollectorInterface::class)->getMockForAbstractClass();
  54. $pageLayoutFileSource->expects(\PHPUnit\Framework\TestCase::any())
  55. ->method('getFiles')
  56. ->willReturn([]);
  57. $cache = $this->_testCase
  58. ->getMockBuilder(\Magento\Framework\Cache\FrontendInterface::class)->getMockForAbstractClass();
  59. return $objectManager->create(
  60. \Magento\Framework\View\Layout\ProcessorInterface::class,
  61. ['fileSource' => $fileSource, 'pageLayoutFileSource' => $pageLayoutFileSource, 'cache' => $cache]
  62. );
  63. }
  64. /**
  65. * Retrieve new layout model instance with layout updates from a fixture file
  66. *
  67. * @param string|array $layoutUpdatesFile
  68. * @param array $args
  69. * @return \Magento\Framework\View\Layout|\PHPUnit_Framework_MockObject_MockObject
  70. */
  71. public function getLayoutFromFixture($layoutUpdatesFile, array $args = [])
  72. {
  73. $layout = $this->_testCase->getMockBuilder(\Magento\Framework\View\Layout::class)
  74. ->setMethods(['getUpdate'])
  75. ->setConstructorArgs($args)
  76. ->getMock();
  77. $layoutUpdate = $this->getLayoutUpdateFromFixture($layoutUpdatesFile);
  78. $layoutUpdate->asSimplexml();
  79. $layout->expects(
  80. \PHPUnit\Framework\TestCase::any()
  81. )->method(
  82. 'getUpdate'
  83. )->will(
  84. \PHPUnit\Framework\TestCase::returnValue($layoutUpdate)
  85. );
  86. return $layout;
  87. }
  88. /**
  89. * Retrieve object that will be used for layout instantiation
  90. *
  91. * @return array
  92. */
  93. public function getLayoutDependencies()
  94. {
  95. $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  96. return [
  97. 'processorFactory' => $objectManager->get(\Magento\Framework\View\Layout\ProcessorFactory::class),
  98. 'eventManager' => $objectManager->get(\Magento\Framework\Event\ManagerInterface::class),
  99. 'structure' => $objectManager->create(\Magento\Framework\View\Layout\Data\Structure::class, []),
  100. 'messageManager' => $objectManager->get(\Magento\Framework\Message\ManagerInterface::class),
  101. 'themeResolver' => $objectManager->get(\Magento\Framework\View\Design\Theme\ResolverInterface::class),
  102. 'reader' => $objectManager->get('commonRenderPool'),
  103. 'generatorPool' => $objectManager->get(\Magento\Framework\View\Layout\GeneratorPool::class),
  104. 'cache' => $objectManager->get(\Magento\Framework\App\Cache\Type\Layout::class),
  105. 'readerContextFactory' => $objectManager->get(\Magento\Framework\View\Layout\Reader\ContextFactory::class),
  106. 'generatorContextFactory' => $objectManager->get(
  107. \Magento\Framework\View\Layout\Generator\ContextFactory::class
  108. ),
  109. 'appState' => $objectManager->get(\Magento\Framework\App\State::class),
  110. 'logger' => $objectManager->get(\Psr\Log\LoggerInterface::class),
  111. ];
  112. }
  113. }