AbstractPlugin.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Interception;
  7. /**
  8. * Class GeneralTest
  9. *
  10. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  11. */
  12. abstract class AbstractPlugin extends \PHPUnit\Framework\TestCase
  13. {
  14. /**
  15. * Config reader
  16. *
  17. * @var \PHPUnit_Framework_MockObject_MockObject
  18. */
  19. protected $_configReader;
  20. /**
  21. * Object Manager
  22. *
  23. * @var \Magento\Framework\ObjectManagerInterface
  24. */
  25. protected $_objectManager;
  26. /**
  27. * Applicartion Object Manager
  28. *
  29. * @var \Magento\Framework\ObjectManagerInterface
  30. */
  31. private $applicationObjectManager;
  32. /**
  33. * Set up
  34. */
  35. public function setUp()
  36. {
  37. if (!$this->_objectManager) {
  38. return;
  39. }
  40. $this->applicationObjectManager = \Magento\Framework\App\ObjectManager::getInstance();
  41. \Magento\Framework\App\ObjectManager::setInstance($this->_objectManager);
  42. }
  43. /**
  44. * Tear down
  45. */
  46. public function tearDown()
  47. {
  48. \Magento\Framework\App\ObjectManager::setInstance($this->applicationObjectManager);
  49. }
  50. /**
  51. * Set up Interception Config
  52. *
  53. * @param array $pluginConfig
  54. */
  55. public function setUpInterceptionConfig($pluginConfig)
  56. {
  57. $config = new \Magento\Framework\Interception\ObjectManager\Config\Developer();
  58. $factory = new \Magento\Framework\ObjectManager\Factory\Dynamic\Developer($config, null);
  59. $this->_configReader = $this->createMock(\Magento\Framework\Config\ReaderInterface::class);
  60. $this->_configReader->expects(
  61. $this->any()
  62. )->method(
  63. 'read'
  64. )->will(
  65. $this->returnValue($pluginConfig)
  66. );
  67. $areaList = $this->createMock(\Magento\Framework\App\AreaList::class);
  68. $areaList->expects($this->any())->method('getCodes')->will($this->returnValue([]));
  69. $configScope = new \Magento\Framework\Config\Scope($areaList, 'global');
  70. $cache = $this->createMock(\Magento\Framework\Config\CacheInterface::class);
  71. $cacheManager = $this->createMock(\Magento\Framework\Interception\Config\CacheManager::class);
  72. $cacheManager->method('load')->willReturn(null);
  73. $definitions = new \Magento\Framework\ObjectManager\Definition\Runtime();
  74. $relations = new \Magento\Framework\ObjectManager\Relations\Runtime();
  75. $interceptionConfig = new Config\Config(
  76. $this->_configReader,
  77. $configScope,
  78. $cache,
  79. $relations,
  80. $config,
  81. $definitions,
  82. 'interception',
  83. null,
  84. $cacheManager
  85. );
  86. $interceptionDefinitions = new Definition\Runtime();
  87. $json = new \Magento\Framework\Serialize\Serializer\Json();
  88. $sharedInstances = [
  89. \Magento\Framework\Config\CacheInterface::class => $cache,
  90. \Magento\Framework\Config\ScopeInterface::class => $configScope,
  91. \Magento\Framework\Config\ReaderInterface::class => $this->_configReader,
  92. \Magento\Framework\ObjectManager\RelationsInterface::class => $relations,
  93. \Magento\Framework\ObjectManager\ConfigInterface::class => $config,
  94. \Magento\Framework\Interception\ObjectManager\ConfigInterface::class => $config,
  95. \Magento\Framework\ObjectManager\DefinitionInterface::class => $definitions,
  96. \Magento\Framework\Interception\DefinitionInterface::class => $interceptionDefinitions,
  97. \Magento\Framework\Serialize\SerializerInterface::class => $json,
  98. ];
  99. $this->_objectManager = new \Magento\Framework\ObjectManager\ObjectManager(
  100. $factory,
  101. $config,
  102. $sharedInstances
  103. );
  104. $factory->setObjectManager($this->_objectManager);
  105. $config->setInterceptionConfig($interceptionConfig);
  106. $config->extend(
  107. [
  108. 'preferences' => [
  109. \Magento\Framework\Interception\PluginListInterface::class =>
  110. \Magento\Framework\Interception\PluginList\PluginList::class,
  111. \Magento\Framework\Interception\ChainInterface::class =>
  112. \Magento\Framework\Interception\Chain\Chain::class,
  113. ],
  114. ]
  115. );
  116. }
  117. }