Skip to content

Commit 852146d

Browse files
author
Bohdan Korablov
committed
MAGETWO-63382: CLI Improvements: Configuration management - Command config:show
1 parent bc5b02d commit 852146d

File tree

5 files changed

+251
-4
lines changed

5 files changed

+251
-4
lines changed

app/code/Magento/Config/App/Config/Source/EnvironmentConfigSource.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,23 @@
1717
class EnvironmentConfigSource implements ConfigSourceInterface
1818
{
1919
/**
20+
* Library for working with arrays.
21+
*
2022
* @var ArrayManager
2123
*/
2224
private $arrayManager;
2325

2426
/**
27+
* Object for working with placeholders for environment variables.
28+
*
2529
* @var PlaceholderInterface
2630
*/
2731
private $placeholder;
2832

33+
/**
34+
* @param ArrayManager $arrayManager
35+
* @param PlaceholderFactory $placeholderFactory
36+
*/
2937
public function __construct(
3038
ArrayManager $arrayManager,
3139
PlaceholderFactory $placeholderFactory

app/code/Magento/Config/Console/Command/ConfigShow/ValueProcessor.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ class ValueProcessor
3333
private $configValueFactory;
3434

3535
/**
36+
* Object for managing configuration scope.
37+
*
3638
* @var ScopeInterface
3739
*/
3840
private $scope;
@@ -55,10 +57,10 @@ public function __construct(
5557
/**
5658
* Processes value using backend model.
5759
*
58-
* @param string $scope
59-
* @param string $scopeCode
60-
* @param string $value
61-
* @param string $path
60+
* @param string $scope The scope of configuration
61+
* @param string $scopeCode The scope code of configuration
62+
* @param string $value The value to process
63+
* @param string $path The configuration path for getting backend model
6264
* @return string
6365
*/
6466
public function process($scope, $scopeCode, $value, $path)

app/code/Magento/Config/Console/Command/ConfigShowCommand.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,36 +32,50 @@ class ConfigShowCommand extends Command
3232
/**#@-*/
3333

3434
/**
35+
* Scope validator.
36+
*
3537
* @var ValidatorInterface
3638
*/
3739
private $scopeValidator;
3840

3941
/**
42+
* Source of configurations.
43+
*
4044
* @var ConfigSourceInterface
4145
*/
4246
private $configSource;
4347

4448
/**
49+
* Config path resolver.
50+
*
4551
* @var ConfigPathResolver
4652
*/
4753
private $pathResolver;
4854

4955
/**
56+
* Class for processing value using backend model.
57+
*
5058
* @var ValueProcessor
5159
*/
5260
private $valueProcessor;
5361

5462
/**
63+
* The scope of configuration.
64+
*
5565
* @var string
5666
*/
5767
private $scope;
5868

5969
/**
70+
* The scope code of configuration.
71+
*
6072
* @var string
6173
*/
6274
private $scopeCode;
6375

6476
/**
77+
* The path of configuration.
78+
*
6579
* @var string
6680
*/
6781
private $inputPath;
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
/**
3+
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Config\Test\Unit\App\Config\Source;
7+
8+
use Magento\Config\App\Config\Source\EnvironmentConfigSource;
9+
use Magento\Config\Model\Placeholder\PlaceholderFactory;
10+
use Magento\Config\Model\Placeholder\PlaceholderInterface;
11+
use Magento\Framework\Stdlib\ArrayManager;
12+
13+
class EnvironmentConfigSourceTest extends \PHPUnit_Framework_TestCase
14+
{
15+
/**
16+
* @var ArrayManager|\PHPUnit_Framework_MockObject_MockObject
17+
*/
18+
private $arrayManagerMock;
19+
20+
/**
21+
* @var PlaceholderInterface|\PHPUnit_Framework_MockObject_MockObject
22+
*/
23+
private $placeholderMock;
24+
25+
/**
26+
* @var EnvironmentConfigSource
27+
*/
28+
private $source;
29+
30+
protected function setUp()
31+
{
32+
$this->arrayManagerMock = $this->getMockBuilder(ArrayManager::class)
33+
->disableOriginalConstructor()
34+
->getMock();
35+
$this->placeholderMock = $this->getMockBuilder(PlaceholderInterface::class)
36+
->getMockForAbstractClass();
37+
38+
/** @var PlaceholderFactory|\PHPUnit_Framework_MockObject_MockObject $placeholderFactoryMock */
39+
$placeholderFactoryMock = $this->getMockBuilder(PlaceholderFactory::class)
40+
->disableOriginalConstructor()
41+
->getMock();
42+
$placeholderFactoryMock->expects($this->once())
43+
->method('create')
44+
->with(PlaceholderFactory::TYPE_ENVIRONMENT)
45+
->willReturn($this->placeholderMock);
46+
47+
$this->source = new EnvironmentConfigSource($this->arrayManagerMock, $placeholderFactoryMock);
48+
}
49+
50+
public function testGet()
51+
{
52+
$placeholder = 'CONFIG__UNIT__TEST__VALUE';
53+
$value = 'test_value';
54+
$path = 'unit/test/value';
55+
$expectedArray = ['unit' => ['test' => ['value' => $value]]];
56+
$_ENV[$placeholder] = $value;
57+
58+
$this->placeholderMock->expects($this->any())
59+
->method('isApplicable')
60+
->willReturnMap([
61+
[$placeholder, true]
62+
]);
63+
$this->placeholderMock->expects($this->once())
64+
->method('restore')
65+
->with($placeholder)
66+
->willReturn($path);
67+
$this->arrayManagerMock->expects($this->once())
68+
->method('set')
69+
->with($path, [], $value)
70+
->willReturn($expectedArray);
71+
72+
$this->assertSame($expectedArray, $this->source->get());
73+
}
74+
}
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
<?php
2+
/**
3+
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Config\Test\Unit\Console\Command\ConfigShow;
7+
8+
use Magento\Framework\App\Config\Value;
9+
use Magento\Framework\App\Config\ValueFactory;
10+
use Magento\Config\Model\Config\Structure;
11+
use Magento\Config\Model\Config\StructureFactory;
12+
use Magento\Config\Model\Config\Structure\Element\Field;
13+
use Magento\Framework\Config\ScopeInterface;
14+
use Magento\Framework\App\Area;
15+
use Magento\Config\Console\Command\ConfigShow\ValueProcessor;
16+
17+
class ValueProcessorTest extends \PHPUnit_Framework_TestCase
18+
{
19+
/**
20+
* @var ValueFactory|\PHPUnit_Framework_MockObject_MockObject
21+
*/
22+
private $valueFactoryMock;
23+
24+
/**
25+
* @var ScopeInterface|\PHPUnit_Framework_MockObject_MockObject
26+
*/
27+
private $scopeMock;
28+
29+
/**
30+
* @var StructureFactory|\PHPUnit_Framework_MockObject_MockObject
31+
*/
32+
private $structureFactoryMock;
33+
34+
/**
35+
* @var ValueProcessor
36+
*/
37+
private $valueProcessor;
38+
39+
protected function setUp()
40+
{
41+
$this->valueFactoryMock = $this->getMockBuilder(ValueFactory::class)
42+
->disableOriginalConstructor()
43+
->getMock();
44+
$this->scopeMock = $this->getMockBuilder(ScopeInterface::class)
45+
->getMockForAbstractClass();
46+
$this->structureFactoryMock = $this->getMockBuilder(StructureFactory::class)
47+
->disableOriginalConstructor()
48+
->getMock();
49+
50+
$this->valueProcessor = new ValueProcessor(
51+
$this->scopeMock,
52+
$this->structureFactoryMock,
53+
$this->valueFactoryMock
54+
);
55+
}
56+
57+
/**
58+
* @param bool $hasBackendModel
59+
* @param \PHPUnit_Framework_MockObject_Matcher_InvokedCount $expectsGetBackendModel
60+
* @param \PHPUnit_Framework_MockObject_Matcher_InvokedCount $expectsCreate
61+
* @dataProvider processDataProvider
62+
*/
63+
public function testProcess($hasBackendModel, $expectsGetBackendModel, $expectsCreate)
64+
{
65+
$scope = 'someScope';
66+
$scopeCode = 'someScopeCode';
67+
$value = 'someValue';
68+
$path = 'some/config/path';
69+
$oldConfigScope = 'oldConfigScope';
70+
71+
$this->scopeMock->expects($this->once())
72+
->method('getCurrentScope')
73+
->willReturn($oldConfigScope);
74+
$this->scopeMock->expects($this->at(1))
75+
->method('setCurrentScope')
76+
->with(Area::AREA_ADMINHTML);
77+
$this->scopeMock->expects($this->at(2))
78+
->method('setCurrentScope')
79+
->with($oldConfigScope);
80+
81+
/** @var Structure|\PHPUnit_Framework_MockObject_MockObject $structureMock */
82+
$structureMock = $this->getMockBuilder(Structure::class)
83+
->disableOriginalConstructor()
84+
->getMock();
85+
$this->structureFactoryMock->expects($this->once())
86+
->method('create')
87+
->willReturn($structureMock);
88+
89+
/** @var Value|\PHPUnit_Framework_MockObject_MockObject $valueMock */
90+
$backendModelMock = $this->getMockBuilder(Value::class)
91+
->disableOriginalConstructor()
92+
->setMethods(['setPath', 'setScope', 'setScopeId', 'setValue', 'getValue', 'afterLoad'])
93+
->getMock();
94+
$backendModelMock->expects($this->once())
95+
->method('setPath')
96+
->with($path)
97+
->willReturnSelf();
98+
$backendModelMock->expects($this->once())
99+
->method('setScope')
100+
->with($scope)
101+
->willReturnSelf();
102+
$backendModelMock->expects($this->once())
103+
->method('setScopeId')
104+
->with($scopeCode)
105+
->willReturnSelf();
106+
$backendModelMock->expects($this->once())
107+
->method('setValue')
108+
->with($value)
109+
->willReturnSelf();
110+
$backendModelMock->expects($this->once())
111+
->method('afterLoad')
112+
->willReturnSelf();
113+
$backendModelMock->expects($this->once())
114+
->method('getValue')
115+
->willReturn($value);
116+
117+
/** @var Field|\PHPUnit_Framework_MockObject_MockObject $fieldMock */
118+
$fieldMock = $this->getMockBuilder(Field::class)
119+
->disableOriginalConstructor()
120+
->getMock();
121+
$fieldMock->expects($this->once())
122+
->method('hasBackendModel')
123+
->willReturn($hasBackendModel);
124+
$fieldMock->expects($expectsGetBackendModel)
125+
->method('getBackendModel')
126+
->willReturn($backendModelMock);
127+
$this->valueFactoryMock->expects($expectsCreate)
128+
->method('create')
129+
->willReturn($backendModelMock);
130+
131+
$structureMock->expects($this->once())
132+
->method('getElement')
133+
->with($path)
134+
->willReturn($fieldMock);
135+
136+
$this->assertSame($value, $this->valueProcessor->process($scope, $scopeCode, $value, $path));
137+
}
138+
139+
/**
140+
* @return array
141+
*/
142+
public function processDataProvider()
143+
{
144+
return [
145+
['hasBackendModel' => true, 'expectsGetBackendModel' => $this->once(), 'expectsCreate' => $this->never()],
146+
['hasBackendModel' => false, 'expectsGetBackendModel' => $this->never(), 'expectsCreate' => $this->once()],
147+
];
148+
}
149+
}

0 commit comments

Comments
 (0)