AbstractPlugin.php 4.43 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 120 121 122 123 124 125 126 127
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Magento\Framework\Interception;

/**
 * Class GeneralTest
 *
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
 */
abstract class AbstractPlugin extends \PHPUnit\Framework\TestCase
{
    /**
     * Config reader
     *
     * @var \PHPUnit_Framework_MockObject_MockObject
     */
    protected $_configReader;

    /**
     * Object Manager
     *
     * @var \Magento\Framework\ObjectManagerInterface
     */
    protected $_objectManager;

    /**
     * Applicartion Object Manager
     *
     * @var \Magento\Framework\ObjectManagerInterface
     */
    private $applicationObjectManager;

    /**
     * Set up
     */
    public function setUp()
    {
        if (!$this->_objectManager) {
            return;
        }

        $this->applicationObjectManager = \Magento\Framework\App\ObjectManager::getInstance();
        \Magento\Framework\App\ObjectManager::setInstance($this->_objectManager);
    }

    /**
     * Tear down
     */
    public function tearDown()
    {
        \Magento\Framework\App\ObjectManager::setInstance($this->applicationObjectManager);
    }

    /**
     * Set up Interception Config
     *
     * @param array $pluginConfig
     */
    public function setUpInterceptionConfig($pluginConfig)
    {
        $config = new \Magento\Framework\Interception\ObjectManager\Config\Developer();
        $factory = new \Magento\Framework\ObjectManager\Factory\Dynamic\Developer($config, null);

        $this->_configReader = $this->createMock(\Magento\Framework\Config\ReaderInterface::class);
        $this->_configReader->expects(
            $this->any()
        )->method(
            'read'
        )->will(
            $this->returnValue($pluginConfig)
        );

        $areaList = $this->createMock(\Magento\Framework\App\AreaList::class);
        $areaList->expects($this->any())->method('getCodes')->will($this->returnValue([]));
        $configScope = new \Magento\Framework\Config\Scope($areaList, 'global');
        $cache = $this->createMock(\Magento\Framework\Config\CacheInterface::class);
        $cacheManager = $this->createMock(\Magento\Framework\Interception\Config\CacheManager::class);
        $cacheManager->method('load')->willReturn(null);
        $definitions = new \Magento\Framework\ObjectManager\Definition\Runtime();
        $relations = new \Magento\Framework\ObjectManager\Relations\Runtime();
        $interceptionConfig = new Config\Config(
            $this->_configReader,
            $configScope,
            $cache,
            $relations,
            $config,
            $definitions,
            'interception',
            null,
            $cacheManager
        );
        $interceptionDefinitions = new Definition\Runtime();
        $json = new \Magento\Framework\Serialize\Serializer\Json();
        $sharedInstances = [
            \Magento\Framework\Config\CacheInterface::class                      => $cache,
            \Magento\Framework\Config\ScopeInterface::class                      => $configScope,
            \Magento\Framework\Config\ReaderInterface::class                     => $this->_configReader,
            \Magento\Framework\ObjectManager\RelationsInterface::class           => $relations,
            \Magento\Framework\ObjectManager\ConfigInterface::class              => $config,
            \Magento\Framework\Interception\ObjectManager\ConfigInterface::class => $config,
            \Magento\Framework\ObjectManager\DefinitionInterface::class          => $definitions,
            \Magento\Framework\Interception\DefinitionInterface::class           => $interceptionDefinitions,
            \Magento\Framework\Serialize\SerializerInterface::class              => $json,
        ];
        $this->_objectManager = new \Magento\Framework\ObjectManager\ObjectManager(
            $factory,
            $config,
            $sharedInstances
        );
        $factory->setObjectManager($this->_objectManager);

        $config->setInterceptionConfig($interceptionConfig);
        $config->extend(
            [
                'preferences' => [
                    \Magento\Framework\Interception\PluginListInterface::class =>
                        \Magento\Framework\Interception\PluginList\PluginList::class,
                    \Magento\Framework\Interception\ChainInterface::class =>
                        \Magento\Framework\Interception\Chain\Chain::class,
                ],
            ]
        );
    }
}