RuntimeConfigSourceTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Config\Test\Unit\App\Config\Source;
  7. use Magento\Config\App\Config\Source\RuntimeConfigSource;
  8. use Magento\Config\Model\ResourceModel\Config\Data\CollectionFactory;
  9. use Magento\Framework\App\Config\Scope\Converter;
  10. use Magento\Framework\App\Config\ScopeCodeResolver;
  11. use Magento\Framework\App\Config\ScopeConfigInterface;
  12. use Magento\Framework\App\Config\Value;
  13. /**
  14. * Test Class for retrieving runtime configuration from database.
  15. * @package Magento\Config\Test\Unit\App\Config\Source
  16. */
  17. class RuntimeConfigSourceTest extends \PHPUnit\Framework\TestCase
  18. {
  19. /**
  20. * @var CollectionFactory|\PHPUnit_Framework_MockObject_MockObject
  21. */
  22. private $collectionFactory;
  23. /**
  24. * @var ScopeCodeResolver|\PHPUnit_Framework_MockObject_MockObject
  25. */
  26. private $scopeCodeResolver;
  27. /**
  28. * @var Converter|\PHPUnit_Framework_MockObject_MockObject
  29. */
  30. private $converter;
  31. /**
  32. * @var Value|\PHPUnit_Framework_MockObject_MockObject
  33. */
  34. private $configItem;
  35. /**
  36. * @var Value|\PHPUnit_Framework_MockObject_MockObject
  37. */
  38. private $configItemTwo;
  39. /**
  40. * @var RuntimeConfigSource
  41. */
  42. private $configSource;
  43. public function setUp()
  44. {
  45. $this->collectionFactory = $this->getMockBuilder(CollectionFactory::class)
  46. ->disableOriginalConstructor()
  47. ->setMethods(['create'])
  48. ->getMock();
  49. $this->scopeCodeResolver = $this->getMockBuilder(ScopeCodeResolver::class)
  50. ->disableOriginalConstructor()
  51. ->getMock();
  52. $this->converter = $this->getMockBuilder(Converter::class)
  53. ->disableOriginalConstructor()
  54. ->getMock();
  55. $this->configItem = $this->getMockBuilder(Value::class)
  56. ->disableOriginalConstructor()
  57. ->setMethods(['getScope', 'getPath', 'getValue'])
  58. ->getMock();
  59. $this->configItemTwo = $this->getMockBuilder(Value::class)
  60. ->disableOriginalConstructor()
  61. ->setMethods(['getScope', 'getPath', 'getValue', 'getScopeId'])
  62. ->getMock();
  63. $this->configSource = new RuntimeConfigSource(
  64. $this->collectionFactory,
  65. $this->scopeCodeResolver,
  66. $this->converter
  67. );
  68. }
  69. public function testGet()
  70. {
  71. $scope = 'websites';
  72. $scopeCode = 'myWebsites';
  73. $this->collectionFactory->expects($this->once())
  74. ->method('create')
  75. ->willReturn([$this->configItem, $this->configItemTwo]);
  76. $this->configItem->expects($this->exactly(2))
  77. ->method('getScope')
  78. ->willReturn(ScopeConfigInterface::SCOPE_TYPE_DEFAULT);
  79. $this->configItem->expects($this->once())
  80. ->method('getPath')
  81. ->willReturn('dev/test/setting');
  82. $this->configItem->expects($this->once())
  83. ->method('getValue')
  84. ->willReturn(true);
  85. $this->configItemTwo->expects($this->exactly(3))
  86. ->method('getScope')
  87. ->willReturn($scope);
  88. $this->configItemTwo->expects($this->once())
  89. ->method('getScopeId')
  90. ->willReturn($scopeCode);
  91. $this->configItemTwo->expects($this->once())
  92. ->method('getPath')
  93. ->willReturn('dev/test/setting2');
  94. $this->configItemTwo->expects($this->once())
  95. ->method('getValue')
  96. ->willReturn(false);
  97. $this->scopeCodeResolver->expects($this->once())
  98. ->method('resolve')
  99. ->with($scope, $scopeCode)
  100. ->willReturnArgument(1);
  101. $this->converter->expects($this->exactly(2))
  102. ->method('convert')
  103. ->withConsecutive(
  104. [['dev/test/setting' => true]],
  105. [['dev/test/setting2' => false]]
  106. )
  107. ->willReturnOnConsecutiveCalls(
  108. ['dev/test/setting' => true],
  109. ['dev/test/setting2' => false]
  110. );
  111. $this->assertEquals(
  112. [
  113. 'default' => [
  114. 'dev/test/setting' => true
  115. ],
  116. 'websites' => [
  117. 'myWebsites' => [
  118. 'dev/test/setting2' => false
  119. ]
  120. ]
  121. ],
  122. $this->configSource->get()
  123. );
  124. }
  125. }