ConfigTest.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Interception\Test\Unit\Config;
  7. use Magento\Framework\Serialize\SerializerInterface;
  8. require_once __DIR__ . '/../Custom/Module/Model/Item.php';
  9. require_once __DIR__ . '/../Custom/Module/Model/Item/Enhanced.php';
  10. require_once __DIR__ . '/../Custom/Module/Model/ItemContainer.php';
  11. require_once __DIR__ . '/../Custom/Module/Model/ItemContainer/Enhanced.php';
  12. require_once __DIR__ . '/../Custom/Module/Model/ItemContainerPlugin/Simple.php';
  13. require_once __DIR__ . '/../Custom/Module/Model/ItemPlugin/Simple.php';
  14. require_once __DIR__ . '/../Custom/Module/Model/ItemPlugin/Advanced.php';
  15. /**
  16. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  17. */
  18. class ConfigTest extends \PHPUnit\Framework\TestCase
  19. {
  20. /**
  21. * @var \PHPUnit_Framework_MockObject_MockObject
  22. */
  23. private $configScopeMock;
  24. /**
  25. * @var \PHPUnit_Framework_MockObject_MockObject
  26. */
  27. private $readerMock;
  28. /**
  29. * @var \PHPUnit_Framework_MockObject_MockObject
  30. */
  31. private $omConfigMock;
  32. /**
  33. * @var \PHPUnit_Framework_MockObject_MockObject
  34. */
  35. private $definitionMock;
  36. /**
  37. * @var \PHPUnit_Framework_MockObject_MockObject
  38. */
  39. private $relationsMock;
  40. /**
  41. * @var \Magento\Framework\Interception\Config\CacheManager|\PHPUnit_Framework_MockObject_MockObject
  42. */
  43. private $cacheManagerMock;
  44. /** @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager */
  45. private $objectManagerHelper;
  46. protected function setUp()
  47. {
  48. $this->readerMock = $this->createMock(\Magento\Framework\ObjectManager\Config\Reader\Dom::class);
  49. $this->configScopeMock = $this->createMock(\Magento\Framework\Config\ScopeListInterface::class);
  50. $this->omConfigMock = $this->getMockForAbstractClass(
  51. \Magento\Framework\Interception\ObjectManager\ConfigInterface::class
  52. );
  53. $this->definitionMock = $this->createMock(\Magento\Framework\ObjectManager\DefinitionInterface::class);
  54. $this->relationsMock = $this->getMockForAbstractClass(
  55. \Magento\Framework\ObjectManager\RelationsInterface::class
  56. );
  57. $this->cacheManagerMock = $this->createMock(\Magento\Framework\Interception\Config\CacheManager::class);
  58. $this->objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  59. }
  60. /**
  61. * @param boolean $expectedResult
  62. * @param string $type
  63. * @dataProvider hasPluginsDataProvider
  64. */
  65. public function testHasPluginsWhenDataIsNotCached($expectedResult, $type, $entityParents)
  66. {
  67. $readerMap = include __DIR__ . '/../_files/reader_mock_map.php';
  68. $this->readerMock->expects($this->any())
  69. ->method('read')
  70. ->will($this->returnValueMap($readerMap));
  71. $this->configScopeMock->expects($this->any())
  72. ->method('getAllScopes')
  73. ->will($this->returnValue(['global', 'backend', 'frontend']));
  74. // turn cache off
  75. $this->cacheManagerMock->expects($this->any())
  76. ->method('load')
  77. ->will($this->returnValue(null));
  78. $this->omConfigMock->expects($this->any())
  79. ->method('getOriginalInstanceType')
  80. ->will($this->returnValueMap(
  81. [
  82. [
  83. \Magento\Framework\Interception\Test\Unit\Custom\Module\Model\ItemContainer::class,
  84. \Magento\Framework\Interception\Test\Unit\Custom\Module\Model\ItemContainer::class,
  85. ],
  86. [
  87. \Magento\Framework\Interception\Test\Unit\Custom\Module\Model\Item::class,
  88. \Magento\Framework\Interception\Test\Unit\Custom\Module\Model\Item::class,
  89. ],
  90. [
  91. \Magento\Framework\Interception\Test\Unit\Custom\Module\Model\Item\Enhanced::class,
  92. \Magento\Framework\Interception\Test\Unit\Custom\Module\Model\Item\Enhanced::class,
  93. ],
  94. [
  95. \Magento\Framework\Interception\Test\Unit\Custom\Module\Model\ItemContainer\Enhanced::class,
  96. \Magento\Framework\Interception\Test\Unit\Custom\Module\Model\ItemContainer\Enhanced::class,
  97. ],
  98. [
  99. \Magento\Framework\Interception\Test\Unit\Custom\Module\Model\ItemContainer\Proxy::class,
  100. \Magento\Framework\Interception\Test\Unit\Custom\Module\Model\ItemContainer\Proxy::class,
  101. ],
  102. [
  103. \Magento\Framework\Interception\Test\Unit\Custom\Module\Model\ItemProxy::class,
  104. \Magento\Framework\Interception\Test\Unit\Custom\Module\Model\ItemProxy::class,
  105. ],
  106. [
  107. \Magento\Framework\Interception\Custom\Module\Model\Backslash\ItemProxy::class,
  108. \Magento\Framework\Interception\Custom\Module\Model\Backslash\ItemProxy::class
  109. ],
  110. [
  111. 'virtual_custom_item',
  112. \Magento\Framework\Interception\Test\Unit\Custom\Module\Model\Item::class
  113. ],
  114. ]
  115. ));
  116. $this->definitionMock->expects($this->any())->method('getClasses')->will($this->returnValue(
  117. [
  118. \Magento\Framework\Interception\Test\Unit\Custom\Module\Model\ItemProxy::class,
  119. \Magento\Framework\Interception\Custom\Module\Model\Backslash\ItemProxy::class
  120. ]
  121. ));
  122. $this->relationsMock->expects($this->any())->method('has')->will($this->returnValue($expectedResult));
  123. $this->relationsMock->expects($this->any())->method('getParents')->will($this->returnValue($entityParents));
  124. $model = $this->objectManagerHelper->getObject(
  125. \Magento\Framework\Interception\Config\Config::class,
  126. [
  127. 'reader' => $this->readerMock,
  128. 'scopeList' => $this->configScopeMock,
  129. 'cacheManager' => $this->cacheManagerMock,
  130. 'relations' => $this->relationsMock,
  131. 'omConfig' => $this->omConfigMock,
  132. 'classDefinitions' => $this->definitionMock,
  133. ]
  134. );
  135. $this->assertEquals($expectedResult, $model->hasPlugins($type));
  136. }
  137. /**
  138. * @param boolean $expectedResult
  139. * @param string $type
  140. * @dataProvider hasPluginsDataProvider
  141. */
  142. public function testHasPluginsWhenDataIsCached($expectedResult, $type)
  143. {
  144. $cacheId = 'interception';
  145. $interceptionData = [
  146. \Magento\Framework\Interception\Test\Unit\Custom\Module\Model\ItemContainer::class => true,
  147. \Magento\Framework\Interception\Test\Unit\Custom\Module\Model\Item::class => true,
  148. \Magento\Framework\Interception\Test\Unit\Custom\Module\Model\Item\Enhanced::class => true,
  149. \Magento\Framework\Interception\Test\Unit\Custom\Module\Model\ItemContainer\Enhanced::class => true,
  150. \Magento\Framework\Interception\Test\Unit\Custom\Module\Model\ItemContainer\Proxy::class => true,
  151. \Magento\Framework\Interception\Test\Unit\Custom\Module\Model\ItemProxy::class => false,
  152. 'virtual_custom_item' => true
  153. ];
  154. $this->readerMock->expects($this->never())->method('read');
  155. $this->cacheManagerMock->expects($this->never())->method('save');
  156. $this->cacheManagerMock->expects($this->any())
  157. ->method('load')
  158. ->with($cacheId)
  159. ->will($this->returnValue($interceptionData));
  160. $model = $this->objectManagerHelper->getObject(
  161. \Magento\Framework\Interception\Config\Config::class,
  162. [
  163. 'reader' => $this->readerMock,
  164. 'scopeList' => $this->configScopeMock,
  165. 'cacheManager' => $this->cacheManagerMock,
  166. 'relations' => $this->objectManagerHelper->getObject(
  167. \Magento\Framework\ObjectManager\Relations\Runtime::class
  168. ),
  169. 'omConfig' => $this->omConfigMock,
  170. 'classDefinitions' => $this->definitionMock,
  171. 'cacheId' => $cacheId,
  172. ]
  173. );
  174. $this->assertEquals($expectedResult, $model->hasPlugins($type));
  175. }
  176. /**
  177. * @return array
  178. */
  179. public function hasPluginsDataProvider()
  180. {
  181. return [
  182. // item container has plugins only in the backend scope
  183. [
  184. true, \Magento\Framework\Interception\Test\Unit\Custom\Module\Model\ItemContainer::class,
  185. []
  186. ],
  187. [
  188. true, \Magento\Framework\Interception\Test\Unit\Custom\Module\Model\Item::class,
  189. []
  190. ],
  191. [
  192. true, \Magento\Framework\Interception\Test\Unit\Custom\Module\Model\Item\Enhanced::class,
  193. []
  194. ],
  195. [
  196. // the following model has only inherited plugins
  197. true, \Magento\Framework\Interception\Test\Unit\Custom\Module\Model\ItemContainer\Proxy::class,
  198. [\Magento\Framework\Interception\Test\Unit\Custom\Module\Model\ItemContainer::class]
  199. ],
  200. [
  201. // the following model has only inherited plugins
  202. true, \Magento\Framework\Interception\Test\Unit\Custom\Module\Model\ItemContainer\Proxy::class,
  203. [\Magento\Framework\Interception\Test\Unit\Custom\Module\Model\ItemContainer::class]
  204. ],
  205. [
  206. false, \Magento\Framework\Interception\Test\Unit\Custom\Module\Model\ItemProxy::class,
  207. []
  208. ],
  209. [
  210. true,
  211. 'virtual_custom_item',
  212. []
  213. ]
  214. ];
  215. }
  216. }