Layout.php 4.6 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

/**
 * Core layout utility
 */
namespace Magento\Framework\View\Utility;

/**
 * Class Layout
 *
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
 */
class Layout
{
    /**
     * @var \PHPUnit\Framework\TestCase
     */
    protected $_testCase;

    /**
     * @param \PHPUnit\Framework\TestCase $testCase
     */
    public function __construct(\PHPUnit\Framework\TestCase $testCase)
    {
        $this->_testCase = $testCase;
    }

    /**
     * Retrieve new layout update model instance with XML data from a fixture file
     *
     * @param string|array $layoutUpdatesFile
     * @return \Magento\Framework\View\Layout\ProcessorInterface
     */
    public function getLayoutUpdateFromFixture($layoutUpdatesFile)
    {
        $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
        /** @var \Magento\Framework\View\File\Factory $fileFactory */
        $fileFactory = $objectManager->get(\Magento\Framework\View\File\Factory::class);
        $files = [];
        foreach ((array)$layoutUpdatesFile as $filename) {
            $files[] = $fileFactory->create($filename, 'Magento_View');
        }
        $fileSource = $this->_testCase
            ->getMockBuilder(\Magento\Framework\View\File\CollectorInterface::class)->getMockForAbstractClass();
        $fileSource->expects(
            \PHPUnit\Framework\TestCase::any()
        )->method(
            'getFiles'
        )->will(
            \PHPUnit\Framework\TestCase::returnValue($files)
        );
        $pageLayoutFileSource = $this->_testCase
            ->getMockBuilder(\Magento\Framework\View\File\CollectorInterface::class)->getMockForAbstractClass();
        $pageLayoutFileSource->expects(\PHPUnit\Framework\TestCase::any())
            ->method('getFiles')
            ->willReturn([]);
        $cache = $this->_testCase
            ->getMockBuilder(\Magento\Framework\Cache\FrontendInterface::class)->getMockForAbstractClass();
        return $objectManager->create(
            \Magento\Framework\View\Layout\ProcessorInterface::class,
            ['fileSource' => $fileSource, 'pageLayoutFileSource' => $pageLayoutFileSource, 'cache' => $cache]
        );
    }

    /**
     * Retrieve new layout model instance with layout updates from a fixture file
     *
     * @param string|array $layoutUpdatesFile
     * @param array $args
     * @return \Magento\Framework\View\Layout|\PHPUnit_Framework_MockObject_MockObject
     */
    public function getLayoutFromFixture($layoutUpdatesFile, array $args = [])
    {
        $layout = $this->_testCase->getMockBuilder(\Magento\Framework\View\Layout::class)
            ->setMethods(['getUpdate'])
            ->setConstructorArgs($args)
            ->getMock();
        $layoutUpdate = $this->getLayoutUpdateFromFixture($layoutUpdatesFile);
        $layoutUpdate->asSimplexml();
        $layout->expects(
            \PHPUnit\Framework\TestCase::any()
        )->method(
            'getUpdate'
        )->will(
            \PHPUnit\Framework\TestCase::returnValue($layoutUpdate)
        );
        return $layout;
    }

    /**
     * Retrieve object that will be used for layout instantiation
     *
     * @return array
     */
    public function getLayoutDependencies()
    {
        $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
        return [
            'processorFactory' => $objectManager->get(\Magento\Framework\View\Layout\ProcessorFactory::class),
            'eventManager' => $objectManager->get(\Magento\Framework\Event\ManagerInterface::class),
            'structure' => $objectManager->create(\Magento\Framework\View\Layout\Data\Structure::class, []),
            'messageManager' => $objectManager->get(\Magento\Framework\Message\ManagerInterface::class),
            'themeResolver' => $objectManager->get(\Magento\Framework\View\Design\Theme\ResolverInterface::class),
            'reader' => $objectManager->get('commonRenderPool'),
            'generatorPool' => $objectManager->get(\Magento\Framework\View\Layout\GeneratorPool::class),
            'cache' => $objectManager->get(\Magento\Framework\App\Cache\Type\Layout::class),
            'readerContextFactory' => $objectManager->get(\Magento\Framework\View\Layout\Reader\ContextFactory::class),
            'generatorContextFactory' => $objectManager->get(
                \Magento\Framework\View\Layout\Generator\ContextFactory::class
            ),
            'appState' => $objectManager->get(\Magento\Framework\App\State::class),
            'logger' => $objectManager->get(\Psr\Log\LoggerInterface::class),
        ];
    }
}