Skip to content

Commit

Permalink
MAGETWO-41938: Creation of M2 Sample Command: Demonstrate how to add …
Browse files Browse the repository at this point in the history
…new Commands to Command line interface

- addressed review feedback
  • Loading branch information
eddielau committed Oct 22, 2015
1 parent 5f96615 commit fb7ecb6
Show file tree
Hide file tree
Showing 11 changed files with 173 additions and 117 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use Symfony\Component\Console\Output\OutputInterface;

/**
* Class ShowCustomDeploymentConfigCommand
* Command to show the custom option set in the deployment configuration
*/
class ShowCustomDeploymentConfigCommand extends Command
{
Expand All @@ -39,8 +39,8 @@ public function __construct(\Magento\Framework\App\DeploymentConfig $deploymentC
*/
protected function configure()
{
$this->setName('example:show-custom-deployment-config')
->setDescription('Show custom deployment configuration option');
$this->setName('example:custom-deployment-config:show')
->setDescription('Shows custom deployment configuration option');

parent::configure();
}
Expand All @@ -56,7 +56,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
'<info>The custom deployment configuration value is ' . $value . '</info>'
);
} else {
$output->writeln('<info>The custom deployment configuration value is not yet set.</info>');
$output->writeln('<info>The custom deployment configuration value is not set.</info>');
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@
* <a href="#lic">License</a>

<h2 id="syn">Synopsis</h2>
This module contains one command-line command under `example:show-custom-deployment-config` that enables you to display custom options you added to the Magento deployment configuration.
This module contains a ConfigOptionsList class which adds a custom option to the deployment configuration.
It also contains one command-line command named `example:custom-deployment-config:show` that enables you to display custom options you added to the Magento deployment configuration.

Creating custom options is discussed in <a href="#add-options">Add custom options to <code>setup:config:set</code></a>.

<h2 id="over">Overview</h2>
`setup:config:set` is a command for managing Magento deployment configuration. Modules store their custom
configuration in the Magento deployment configuration and later retrieve from it. Custom options also display in the
`setup:install` command, allowing the user to specify custom configuration during installation.
Modules can store their custom configuration in the Magento deployment configuration and later retrieve from it.
`setup:config:set` is a command for managing Magento deployment configuration.
Custom options also display in the `setup:install` command, allowing the user to specify custom configuration during installation.

<h2 id="install">Install the sample module</h2>
You'll find it useful to install this sample module so you can refer to it when you're coding your own custom commands. If you'd prefer not to, continue with <a href="#add-options">Add custom options to <code>setup:config:set</code></a>.
Expand All @@ -30,7 +31,7 @@ Create a directory for the sample module and copy `magento2-samples/sample-modul
cp -R <magento2-samples clone dir>/sample-module-custom-deploymentconfig/* <your Magento install dir>/app/code/Magento/CustomDeploymentConfigExample

### Update the Magento database and schema
If you haven't installed the Magento application yet, install it now. After it's installed, run the following command:
If you added the module to an existing Magento installation, run the following command:

php <your Magento install dir>/bin/magento setup:upgrade

Expand All @@ -42,28 +43,28 @@ Enter the following command:
The following confirms you installed the module correctly:

example
example:show-custom-deployment-config Show custom deployment configuration option
example:custom-deployment-config:show Show custom deployment configuration option

### Command usage
To use the sample command:

cd <your Magento install dir>/bin
php magento example:show-custom-deployment-config
php magento example:custom-deployment-config:show
php magento setup:config:set --help

`magento example:show-custom-deployment-config` displays the value assigned to the custom option defined in `Magento\CustomDeploymentConfigExample\Setup\ConfigOptionsList` while `magento setup:config:set --help` displays all options available for set.
`magento example:custom-deployment-config:show` displays the value assigned to the custom option defined in `Magento\CustomDeploymentConfigExample\Setup\ConfigOptionsList` while `magento setup:config:set --help` displays all options available for set.

<h2 id="add-options">Add custom options to <code>setup:config:set</code></h2>
To implement this command:
<h2 id="add-options">Add custom options to the deployment Configuration</h2>
To add custom options to the deployment configuration:

1. Create class `ConfigOptionsList` in `<module_dir>/Setup` that implements
`Magento\Framework\Setup\ConfigOptionsListInterface`

2. Implement required methods:

* `getOptions()`: Returns list of custom options that should be added to the `setup:config:set` command
* `getOptions()`: Returns list of custom options that should be added to the deployment configuration
* `createConfigData()`: Creates the required array structure to be stored in the deployment configuration
* `validate()`: Validates user input
* `validate()`: Validates option values

3. Clean the Magento cache.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
use Magento\Framework\Setup\ConfigOptionsListInterface;
use Magento\Framework\Setup\Option\TextConfigOption;

/**
* This class adds the custom option to the deployment configuration.
* A DeploymentConfig object is available to be used when creating config data and validating option values.
*/
class ConfigOptionsList implements ConfigOptionsListInterface
{
/**
Expand All @@ -19,7 +23,8 @@ class ConfigOptionsList implements ConfigOptionsListInterface
const INPUT_KEY_CUSTOM_OPTION = 'custom-option';

/**
* Path to the custom configuration in deployment config
* Path to the custom configuration in deployment configuration.
* This path will be used to retrieve the option value
*/
const CONFIG_PATH_CUSTOM_OPTION = 'example/custom-option';

Expand All @@ -40,18 +45,22 @@ public function getOptions()

/**
* {@inheritdoc}
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function createConfig(array $options, DeploymentConfig $deploymentConfig)
{
$configData = new ConfigData(ConfigFilePool::APP_CONFIG);
if (isset($options[self::INPUT_KEY_CUSTOM_OPTION])) {
$configData->set(self::CONFIG_PATH_CUSTOM_OPTION, $options[self::INPUT_KEY_CUSTOM_OPTION]);
} elseif ($deploymentConfig->get(self::CONFIG_PATH_CUSTOM_OPTION) === null) {
// set to default value if it is not already set in deployment configuration
$configData->set(self::CONFIG_PATH_CUSTOM_OPTION, 'default custom value');
}
return [$configData];
}

/**
* Suppress warning added because we are not using DeploymentConfig to validate user input here
*
* {@inheritdoc}
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\CustomDeploymentConfigExample\Test\Unit\Console\Command;

use Magento\CustomDeploymentConfigExample\Console\Command\ShowCustomDeploymentConfigCommand;
use Magento\CustomDeploymentConfigExample\Setup\ConfigOptionsList;
use Symfony\Component\Console\Tester\CommandTester;

class ShowCustomDeploymentConfigCommandTest extends \PHPUnit_Framework_TestCase
{
/**
* @var \Magento\Framework\App\DeploymentConfig|\PHPUnit_Framework_MockObject_MockObject
*/
private $deploymentConfigMock;

/**
* @var CommandTester|\PHPUnit_Framework_MockObject_MockObject
*/
private $commandTester;

public function setUp()
{
$this->deploymentConfigMock = $this->getMock('Magento\Framework\App\DeploymentConfig', [], [], '', false);
$command = new ShowCustomDeploymentConfigCommand($this->deploymentConfigMock);
$this->commandTester = new CommandTester($command);
}

public function testExecute()
{

$this->deploymentConfigMock->expects($this->once())
->method('get')
->with(ConfigOptionsList::CONFIG_PATH_CUSTOM_OPTION)
->willReturn('value');

$this->commandTester->execute([]);
$this->assertContains('value', $this->commandTester->getDisplay());
}

public function testExecuteNoValue()
{
$this->deploymentConfigMock->expects($this->once())
->method('get')
->with(ConfigOptionsList::CONFIG_PATH_CUSTOM_OPTION)
->willReturn(null);

$this->commandTester->execute([]);
$this->assertContains('is not set', $this->commandTester->getDisplay());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\CustomDeploymentConfigExample\Test\Unit\Setup;

use Magento\CustomDeploymentConfigExample\Setup\ConfigOptionsList;

class ConfigOptionsListTest extends \PHPUnit_Framework_TestCase
{
/**
* @var ConfigOptionsList
*/
private $configOptionsList;

/**
* @var \Magento\Framework\App\DeploymentConfig|\PHPUnit_Framework_MockObject_MockObject
*/
private $deploymentConfigMock;

public function setUp()
{
$this->configOptionsList = new ConfigOptionsList();
$this->deploymentConfigMock = $this->getMock('Magento\Framework\App\DeploymentConfig', [], [], '', false);
}

public function testGetOptions()
{
$this->assertInstanceOf(
'Magento\Framework\Setup\Option\TextConfigOption',
$this->configOptionsList->getOptions()[0]
);
}

public function testCreateConfig()
{
$data = $this->configOptionsList->createConfig(
[ConfigOptionsList::INPUT_KEY_CUSTOM_OPTION => 'value'],
$this->deploymentConfigMock
);
$this->assertCount(1, $data);
$this->assertEquals(['example' => ['custom-option' => 'value']], $data[0]->getData());
}

public function testCreateConfigNoValue()
{
$this->deploymentConfigMock->expects($this->once())
->method('get')
->with(ConfigOptionsList::CONFIG_PATH_CUSTOM_OPTION)
->willReturn('some value');
$data = $this->configOptionsList->createConfig([], $this->deploymentConfigMock);
$this->assertEquals([], $data[0]->getData());
}

public function testCreateConfigNoValueDefault()
{
$this->deploymentConfigMock->expects($this->once())
->method('get')
->with(ConfigOptionsList::CONFIG_PATH_CUSTOM_OPTION)
->willReturn(null);
$data = $this->configOptionsList->createConfig([], $this->deploymentConfigMock);
$this->assertEquals(['example' => ['custom-option' => 'default custom value']], $data[0]->getData());
}

public function testValidate()
{
$configOptionsList = new ConfigOptionsList();
$this->assertEquals(
[],
$configOptionsList->validate(
[ConfigOptionsList::INPUT_KEY_CUSTOM_OPTION => 'value'],
$this->deploymentConfigMock
)
);
}

public function testValidateInvalid()
{
$configOptionsList = new ConfigOptionsList();
$this->assertEquals(
['Invalid custom option value'],
$configOptionsList->validate(
[ConfigOptionsList::INPUT_KEY_CUSTOM_OPTION => 'invalid'],
$this->deploymentConfigMock
)
);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "magento/sample-module-custom-deploymentconfig",
"name": "magento/sample-module-custom-deployment-config",
"description": "Custom deployment config option example",
"type":"magento2-module",
"require": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* See COPYING.txt for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Framework\Console\CommandList">
<arguments>
<argument name="commands" xsi:type="array">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Magento_CustomDeploymentConfigExample" setup_version="1.0.0">
</module>
<module name="Magento_CustomDeploymentConfigExample" setup_version="1.0.0"></module>
</config>

This file was deleted.

Loading

0 comments on commit fb7ecb6

Please sign in to comment.