PreparedValueFactoryTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Config\Test\Unit\Model;
  7. use Magento\Config\Model\Config\BackendFactory;
  8. use Magento\Config\Model\PreparedValueFactory;
  9. use Magento\Framework\App\Config\ScopeConfigInterface;
  10. use Magento\Config\Model\Config\StructureFactory;
  11. use Magento\Framework\App\Config\Value;
  12. use Magento\Config\Model\Config\Structure;
  13. use Magento\Config\Model\Config\Structure\Element\Field;
  14. use Magento\Config\Model\Config\Structure\Element\Group;
  15. use Magento\Framework\App\ScopeInterface;
  16. use Magento\Store\Model\ScopeInterface as StoreScopeInterface;
  17. use Magento\Framework\App\ScopeResolver;
  18. use Magento\Framework\App\ScopeResolverPool;
  19. use Magento\Store\Model\ScopeTypeNormalizer;
  20. use PHPUnit_Framework_MockObject_MockObject as Mock;
  21. /**
  22. * @inheritdoc
  23. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  24. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  25. */
  26. class PreparedValueFactoryTest extends \PHPUnit\Framework\TestCase
  27. {
  28. /**
  29. * @var StructureFactory|Mock
  30. */
  31. private $structureFactoryMock;
  32. /**
  33. * @var BackendFactory|Mock
  34. */
  35. private $valueFactoryMock;
  36. /**
  37. * @var Value|Mock
  38. */
  39. private $valueMock;
  40. /**
  41. * @var Structure|Mock
  42. */
  43. private $structureMock;
  44. /**
  45. * @var Field|Mock
  46. */
  47. private $fieldMock;
  48. /**
  49. * @var ScopeConfigInterface|Mock
  50. */
  51. private $configMock;
  52. /**
  53. * @var ScopeResolverPool|Mock
  54. */
  55. private $scopeResolverPoolMock;
  56. /**
  57. * @var ScopeResolver|Mock
  58. */
  59. private $scopeResolverMock;
  60. /**
  61. * @var ScopeInterface|Mock
  62. */
  63. private $scopeMock;
  64. /**
  65. * @var ScopeTypeNormalizer|Mock
  66. */
  67. private $scopeTypeNormalizer;
  68. /**
  69. * @var PreparedValueFactory
  70. */
  71. private $preparedValueFactory;
  72. /**
  73. * @inheritdoc
  74. */
  75. protected function setUp()
  76. {
  77. $this->structureFactoryMock = $this->getMockBuilder(StructureFactory::class)
  78. ->disableOriginalConstructor()
  79. ->setMethods(['create'])
  80. ->getMock();
  81. $this->valueFactoryMock = $this->getMockBuilder(BackendFactory::class)
  82. ->disableOriginalConstructor()
  83. ->setMethods(['create'])
  84. ->getMock();
  85. $this->structureMock = $this->getMockBuilder(Structure::class)
  86. ->disableOriginalConstructor()
  87. ->getMock();
  88. $this->fieldMock = $this->getMockBuilder(Field::class)
  89. ->disableOriginalConstructor()
  90. ->getMock();
  91. $this->valueMock = $this->getMockBuilder(Value::class)
  92. ->disableOriginalConstructor()
  93. ->setMethods([
  94. 'setPath', 'setScope', 'setScopeId', 'setValue', 'setField',
  95. 'setGroupId', 'setFieldConfig', 'setScopeCode'
  96. ])
  97. ->getMock();
  98. $this->configMock = $this->getMockBuilder(ScopeConfigInterface::class)
  99. ->getMockForAbstractClass();
  100. $this->scopeResolverPoolMock = $this->getMockBuilder(ScopeResolverPool::class)
  101. ->disableOriginalConstructor()
  102. ->getMock();
  103. $this->scopeResolverMock = $this->getMockBuilder(ScopeResolver::class)
  104. ->disableOriginalConstructor()
  105. ->getMock();
  106. $this->scopeMock = $this->getMockBuilder(ScopeInterface::class)
  107. ->getMockForAbstractClass();
  108. $this->scopeTypeNormalizer = $this->getMockBuilder(ScopeTypeNormalizer::class)
  109. ->disableOriginalConstructor()
  110. ->getMock();
  111. $this->preparedValueFactory = new PreparedValueFactory(
  112. $this->scopeResolverPoolMock,
  113. $this->structureFactoryMock,
  114. $this->valueFactoryMock,
  115. $this->configMock,
  116. $this->scopeTypeNormalizer
  117. );
  118. }
  119. /**
  120. * @param string $path
  121. * @param string|null $configPath
  122. * @param string $value
  123. * @param string $scope
  124. * @param string|int|null $scopeCode
  125. * @param int $scopeId
  126. * @dataProvider createDataProvider
  127. */
  128. public function testCreate(
  129. $path,
  130. $configPath,
  131. $value,
  132. $scope,
  133. $scopeCode,
  134. $scopeId
  135. ) {
  136. $groupPath = 'some/group';
  137. $groupId = 'some_group';
  138. $fieldId = 'some_field';
  139. $fieldData = ['backend_model' => 'some_model'];
  140. if (ScopeInterface::SCOPE_DEFAULT !== $scope) {
  141. $this->scopeResolverPoolMock->expects($this->once())
  142. ->method('get')
  143. ->with($scope)
  144. ->willReturn($this->scopeResolverMock);
  145. $this->scopeResolverMock->expects($this->once())
  146. ->method('getScope')
  147. ->with($scopeCode)
  148. ->willReturn($this->scopeMock);
  149. $this->scopeMock->expects($this->once())
  150. ->method('getId')
  151. ->willReturn($scopeId);
  152. }
  153. /** @var Group|Mock $groupMock */
  154. $groupMock = $this->getMockBuilder(Group::class)
  155. ->disableOriginalConstructor()
  156. ->getMock();
  157. $groupMock->expects($this->once())
  158. ->method('getId')
  159. ->willReturn($groupId);
  160. $this->scopeTypeNormalizer->expects($this->once())
  161. ->method('normalize')
  162. ->with($scope, true)
  163. ->willReturnArgument(0);
  164. $this->structureFactoryMock->expects($this->once())
  165. ->method('create')
  166. ->willReturn($this->structureMock);
  167. $this->structureMock->expects($this->once())
  168. ->method('getElementByConfigPath')
  169. ->willReturn($this->fieldMock);
  170. $this->structureMock->expects($this->once())
  171. ->method('getElement')
  172. ->with($groupPath)
  173. ->willReturn($groupMock);
  174. $this->fieldMock->expects($this->once())
  175. ->method('hasBackendModel')
  176. ->willReturn(true);
  177. $this->fieldMock
  178. ->method('getConfigPath')
  179. ->willReturn($configPath);
  180. $this->fieldMock
  181. ->method('getId')
  182. ->willReturn($fieldId);
  183. $this->fieldMock
  184. ->method('getData')
  185. ->willReturn($fieldData);
  186. $this->fieldMock->expects($this->once())
  187. ->method('getGroupPath')
  188. ->willReturn($groupPath);
  189. $this->valueFactoryMock->expects($this->once())
  190. ->method('create')
  191. ->willReturn($this->valueMock);
  192. $this->valueMock->expects($this->once())
  193. ->method('setPath')
  194. ->with($configPath ?: $path)
  195. ->willReturnSelf();
  196. $this->valueMock->expects($this->once())
  197. ->method('setScope')
  198. ->with($scope)
  199. ->willReturnSelf();
  200. $this->valueMock->expects($this->once())
  201. ->method('setScopeId')
  202. ->with($scopeId)
  203. ->willReturnSelf();
  204. $this->valueMock->expects($this->once())
  205. ->method('setScopeCode')
  206. ->with($scopeCode)
  207. ->willReturnSelf();
  208. $this->valueMock->expects($this->once())
  209. ->method('setValue')
  210. ->with($value)
  211. ->willReturnSelf();
  212. $this->valueMock->expects($this->once())
  213. ->method('setField')
  214. ->with($fieldId)
  215. ->willReturnSelf();
  216. $this->valueMock->expects($this->once())
  217. ->method('setGroupId')
  218. ->with($groupId)
  219. ->willReturnSelf();
  220. $this->valueMock->expects($this->once())
  221. ->method('setFieldConfig')
  222. ->with($fieldData)
  223. ->willReturnSelf();
  224. $this->assertInstanceOf(
  225. Value::class,
  226. $this->preparedValueFactory->create($path, $value, $scope, $scopeCode)
  227. );
  228. }
  229. /**
  230. * @return array
  231. */
  232. public function createDataProvider()
  233. {
  234. return [
  235. 'standard flow' => [
  236. '/some/path',
  237. null,
  238. 'someValue',
  239. 'someScope',
  240. 'someScopeCode',
  241. 1,
  242. ],
  243. 'standard flow with custom config path' => [
  244. '/some/path',
  245. '/custom/config_path',
  246. 'someValue',
  247. 'someScope',
  248. 'someScope',
  249. 'someScopeCode',
  250. 1,
  251. ],
  252. 'default scope flow' => [
  253. '/some/path',
  254. null,
  255. 'someValue',
  256. ScopeInterface::SCOPE_DEFAULT,
  257. ScopeInterface::SCOPE_DEFAULT,
  258. null,
  259. 0,
  260. ],
  261. 'website scope flow' => [
  262. '/some/path',
  263. 'someValue',
  264. StoreScopeInterface::SCOPE_WEBSITE,
  265. StoreScopeInterface::SCOPE_WEBSITES,
  266. null,
  267. 0,
  268. ],
  269. 'websites scope flow' => [
  270. '/some/path',
  271. 'someValue',
  272. StoreScopeInterface::SCOPE_WEBSITES,
  273. StoreScopeInterface::SCOPE_WEBSITES,
  274. null,
  275. 0,
  276. ],
  277. 'store scope flow' => [
  278. '/some/path',
  279. 'someValue',
  280. StoreScopeInterface::SCOPE_STORE,
  281. StoreScopeInterface::SCOPE_STORES,
  282. null,
  283. 0,
  284. ],
  285. 'stores scope flow' => [
  286. '/some/path',
  287. 'someValue',
  288. StoreScopeInterface::SCOPE_STORES,
  289. StoreScopeInterface::SCOPE_STORES,
  290. null,
  291. 0,
  292. ],
  293. ];
  294. }
  295. /**
  296. * @param string $path
  297. * @param string $scope
  298. * @param string|int|null $scopeCode
  299. * @dataProvider createDataProvider
  300. */
  301. public function testCreateNotInstanceOfValue(
  302. $path,
  303. $scope,
  304. $scopeCode
  305. ) {
  306. $this->scopeResolverPoolMock->expects($this->never())
  307. ->method('get');
  308. $this->scopeResolverMock->expects($this->never())
  309. ->method('getScope');
  310. $this->scopeMock->expects($this->never())
  311. ->method('getId');
  312. $value = new \stdClass();
  313. $this->structureFactoryMock->expects($this->once())
  314. ->method('create')
  315. ->willReturn($this->structureMock);
  316. $this->structureMock->expects($this->once())
  317. ->method('getElementByConfigPath')
  318. ->willReturn($this->fieldMock);
  319. $this->fieldMock->expects($this->once())
  320. ->method('hasBackendModel')
  321. ->willReturn(false);
  322. $this->fieldMock->expects($this->never())
  323. ->method('getBackendModel');
  324. $this->valueFactoryMock->expects($this->once())
  325. ->method('create')
  326. ->willReturn($value);
  327. $this->valueMock->expects($this->never())
  328. ->method('setPath');
  329. $this->valueMock->expects($this->never())
  330. ->method('setScope');
  331. $this->valueMock->expects($this->never())
  332. ->method('setScopeId');
  333. $this->valueMock->expects($this->never())
  334. ->method('setValue');
  335. $this->assertSame(
  336. $value,
  337. $this->preparedValueFactory->create($path, $value, $scope, $scopeCode)
  338. );
  339. }
  340. /**
  341. * @return array
  342. */
  343. public function createNotInstanceOfValueDataProvider()
  344. {
  345. return [
  346. 'standard flow' => [
  347. '/some/path',
  348. 'someScope',
  349. 'someScopeCode',
  350. 1,
  351. ],
  352. 'default scope flow' => [
  353. '/some/path',
  354. ScopeInterface::SCOPE_DEFAULT,
  355. null,
  356. ],
  357. ];
  358. }
  359. /**
  360. * @expectedException \Magento\Framework\Exception\RuntimeException
  361. * @expectedExceptionMessage Some exception
  362. */
  363. public function testCreateWithException()
  364. {
  365. $this->structureFactoryMock->expects($this->once())
  366. ->method('create')
  367. ->willThrowException(new \Exception('Some exception'));
  368. $this->preparedValueFactory->create('path', 'value', ScopeInterface::SCOPE_DEFAULT);
  369. }
  370. }