Skip to content

Commit eb10a64

Browse files
author
Magento CICD
authored
MAGETWO-64704: [GitHub] [PR] Add bin/magento commands to list store/website data #7982
2 parents c68784d + 0997d2f commit eb10a64

File tree

5 files changed

+387
-0
lines changed

5 files changed

+387
-0
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
/**
3+
*
4+
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
namespace Magento\Store\Console\Command;
8+
9+
use Symfony\Component\Console\Input\InputInterface;
10+
use Symfony\Component\Console\Output\OutputInterface;
11+
use Symfony\Component\Console\Command\Command;
12+
13+
/**
14+
* Class StoreListCommand
15+
*
16+
* Command for listing the configured stores
17+
*/
18+
class StoreListCommand extends Command
19+
{
20+
/** @var \Magento\Store\Model\StoreManagerInterface $storeManager */
21+
private $storeManager;
22+
23+
public function __construct(
24+
\Magento\Store\Model\StoreManagerInterface $storeManager
25+
) {
26+
$this->storeManager = $storeManager;
27+
parent::__construct();
28+
}
29+
30+
/**
31+
* {@inheritdoc}
32+
*/
33+
protected function configure()
34+
{
35+
$this->setName('store:list')
36+
->setDescription('Displays the list of stores');
37+
38+
parent::configure();
39+
}
40+
41+
/**
42+
* {@inheritdoc}
43+
*/
44+
protected function execute(InputInterface $input, OutputInterface $output)
45+
{
46+
try {
47+
$table = $this->getHelperSet()->get('table');
48+
$table->setHeaders(['ID', 'Website ID', 'Group ID', 'Name', 'Code', 'Sort Order', 'Is Active']);
49+
50+
foreach ($this->storeManager->getStores(true, true) as $store) {
51+
$table->addRow([
52+
$store->getId(),
53+
$store->getWebsiteId(),
54+
$store->getStoreGroupId(),
55+
$store->getName(),
56+
$store->getCode(),
57+
$store->getData('sort_order'),
58+
$store->getData('is_active'),
59+
]);
60+
}
61+
62+
$table->render($output);
63+
64+
return \Magento\Framework\Console\Cli::RETURN_SUCCESS;
65+
} catch (\Exception $e) {
66+
$output->writeln('<error>' . $e->getMessage() . '</error>');
67+
if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
68+
$output->writeln($e->getTraceAsString());
69+
}
70+
71+
return \Magento\Framework\Console\Cli::RETURN_FAILURE;
72+
}
73+
}
74+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
/**
3+
*
4+
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
namespace Magento\Store\Console\Command;
8+
9+
use Symfony\Component\Console\Input\InputInterface;
10+
use Symfony\Component\Console\Output\OutputInterface;
11+
use Symfony\Component\Console\Command\Command;
12+
13+
/**
14+
* Class WebsiteListCommand
15+
*
16+
* Command for listing the configured websites
17+
*/
18+
class WebsiteListCommand extends Command
19+
{
20+
/** @var \Magento\Store\Api\WebsiteManagementInterface $storeManager */
21+
private $manager;
22+
23+
public function __construct(
24+
\Magento\Store\Api\WebsiteRepositoryInterface $websiteManagement
25+
) {
26+
$this->manager = $websiteManagement;
27+
parent::__construct();
28+
}
29+
30+
/**
31+
* {@inheritdoc}
32+
*/
33+
protected function configure()
34+
{
35+
$this->setName('store:website:list')
36+
->setDescription('Displays the list of websites');
37+
38+
parent::configure();
39+
}
40+
41+
/**
42+
* {@inheritdoc}
43+
*/
44+
protected function execute(InputInterface $input, OutputInterface $output)
45+
{
46+
try {
47+
$table = $this->getHelperSet()->get('table');
48+
$table->setHeaders(['ID', 'Default Group Id', 'Name', 'Code', 'Sort Order', 'Is Default']);
49+
50+
foreach ($this->manager->getList() as $website) {
51+
$table->addRow([
52+
$website->getId(),
53+
$website->getDefaultGroupId(),
54+
$website->getName(),
55+
$website->getCode(),
56+
$website->getData('sort_order'),
57+
$website->getData('is_default'),
58+
]);
59+
}
60+
61+
$table->render($output);
62+
63+
return \Magento\Framework\Console\Cli::RETURN_SUCCESS;
64+
} catch (\Exception $e) {
65+
$output->writeln('<error>' . $e->getMessage() . '</error>');
66+
if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
67+
$output->writeln($e->getTraceAsString());
68+
}
69+
70+
return \Magento\Framework\Console\Cli::RETURN_FAILURE;
71+
}
72+
}
73+
}
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
<?php
2+
/**
3+
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Store\Test\Unit\Console\Command;
7+
8+
use Magento\Store\Console\Command\StoreListCommand;
9+
use Symfony\Component\Console\Tester\CommandTester;
10+
use Symfony\Component\Console\Helper\HelperSet;
11+
use Symfony\Component\Console\Helper\TableHelper;
12+
use Magento\Store\Model\Store;
13+
use Magento\Framework\Console\Cli;
14+
15+
/**
16+
* @package Magento\Store\Test\Unit\Console\Command
17+
*/
18+
class StoreListCommandTest extends \PHPUnit_Framework_TestCase
19+
{
20+
/**
21+
* @var StoreListCommand
22+
*/
23+
private $command;
24+
25+
/**
26+
* @var \Magento\Store\Model\StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject
27+
*/
28+
private $storeManagerMock;
29+
30+
/**
31+
* @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
32+
*/
33+
private $objectManager;
34+
35+
protected function setUp()
36+
{
37+
$this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
38+
39+
$this->storeManagerMock = $this->getMockForAbstractClass(\Magento\Store\Model\StoreManagerInterface::class);
40+
41+
$this->command = $this->objectManager->getObject(
42+
StoreListCommand::class,
43+
['storeManager' => $this->storeManagerMock]
44+
);
45+
46+
/** @var HelperSet $helperSet */
47+
$helperSet = $this->objectManager->getObject(
48+
HelperSet::class,
49+
['helpers' => [$this->objectManager->getObject(TableHelper::class)]]
50+
);
51+
52+
//Inject table helper for output
53+
$this->command->setHelperSet($helperSet);
54+
}
55+
56+
public function testExecuteExceptionNoVerbosity()
57+
{
58+
$this->storeManagerMock->expects($this->any())
59+
->method('getStores')
60+
->willThrowException(new \Exception("Dummy test exception"));
61+
62+
$tester = new CommandTester($this->command);
63+
$this->assertEquals(Cli::RETURN_FAILURE, $tester->execute([]));
64+
65+
$linesOutput = array_filter(explode(PHP_EOL, $tester->getDisplay()));
66+
$this->assertEquals('Dummy test exception', $linesOutput[0]);
67+
}
68+
69+
public function testExecute()
70+
{
71+
$storeData = [
72+
'store_id' => '999',
73+
'group_id' => '777',
74+
'website_id' => '888',
75+
'name' => 'unit test store',
76+
'code' => 'unit_test_store',
77+
'is_active' => '1',
78+
'sort_order' => '123',
79+
];
80+
81+
$stores = [
82+
$this->objectManager->getObject(Store::class)->setData($storeData),
83+
];
84+
85+
$this->storeManagerMock->expects($this->any())
86+
->method('getStores')
87+
->willReturn($stores);
88+
89+
$tester = new CommandTester($this->command);
90+
$this->assertEquals(Cli::RETURN_SUCCESS, $tester->execute([]));
91+
92+
$linesOutput = array_filter(explode(PHP_EOL, $tester->getDisplay()));
93+
$this->assertCount(5, $linesOutput, 'There should be 5 lines output. 3 Spacers, 1 header, 1 content.');
94+
95+
$this->assertEquals($linesOutput[0], $linesOutput[2], "Lines 0, 2, 4 should be spacer lines");
96+
$this->assertEquals($linesOutput[2], $linesOutput[4], "Lines 0, 2, 4 should be spacer lines");
97+
98+
$headerValues = array_values(array_filter(explode('|', $linesOutput[1])));
99+
//trim to remove the whitespace left from the exploding pipe separation
100+
$this->assertEquals('ID', trim($headerValues[0]));
101+
$this->assertEquals('Website ID', trim($headerValues[1]));
102+
$this->assertEquals('Group ID', trim($headerValues[2]));
103+
$this->assertEquals('Name', trim($headerValues[3]));
104+
$this->assertEquals('Code', trim($headerValues[4]));
105+
$this->assertEquals('Sort Order', trim($headerValues[5]));
106+
$this->assertEquals('Is Active', trim($headerValues[6]));
107+
108+
$storeValues = array_values(array_filter(explode('|', $linesOutput[3])));
109+
$this->assertEquals('999', trim($storeValues[0]));
110+
$this->assertEquals('888', trim($storeValues[1]));
111+
$this->assertEquals('777', trim($storeValues[2]));
112+
$this->assertEquals('unit test store', trim($storeValues[3]));
113+
$this->assertEquals('unit_test_store', trim($storeValues[4]));
114+
$this->assertEquals('123', trim($storeValues[5]));
115+
$this->assertEquals('1', trim($storeValues[6]));
116+
}
117+
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<?php
2+
/**
3+
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Store\Test\Unit\Console\Command;
7+
8+
use Magento\Store\Console\Command\WebsiteListCommand;
9+
use Symfony\Component\Console\Tester\CommandTester;
10+
use Symfony\Component\Console\Helper\HelperSet;
11+
use Symfony\Component\Console\Helper\TableHelper;
12+
use Magento\Store\Model\Website;
13+
use Magento\Framework\Console\Cli;
14+
use Magento\Store\Api\WebsiteRepositoryInterface;
15+
16+
/**
17+
* @package Magento\Store\Test\Unit\Console\Command
18+
*/
19+
class WebsiteListCommandTest extends \PHPUnit_Framework_TestCase
20+
{
21+
/**
22+
* @var WebsiteListCommand
23+
*/
24+
private $command;
25+
26+
/**
27+
* @var \Magento\Store\Api\WebsiteRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
28+
*/
29+
private $websiteRepositoryMock;
30+
31+
/**
32+
* @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
33+
*/
34+
private $objectManager;
35+
36+
protected function setUp()
37+
{
38+
$this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
39+
40+
$this->websiteRepositoryMock = $this->getMockForAbstractClass(WebsiteRepositoryInterface::class);
41+
42+
$this->command = $this->objectManager->getObject(
43+
WebsiteListCommand::class,
44+
['websiteManagement' => $this->websiteRepositoryMock]
45+
);
46+
47+
/** @var HelperSet $helperSet */
48+
$helperSet = $this->objectManager->getObject(
49+
HelperSet::class,
50+
['helpers' => [$this->objectManager->getObject(TableHelper::class)]]
51+
);
52+
53+
//Inject table helper for output
54+
$this->command->setHelperSet($helperSet);
55+
}
56+
57+
public function testExecuteExceptionNoVerbosity()
58+
{
59+
$this->websiteRepositoryMock->expects($this->any())
60+
->method('getList')
61+
->willThrowException(new \Exception("Dummy test exception"));
62+
63+
$tester = new CommandTester($this->command);
64+
$this->assertEquals(Cli::RETURN_FAILURE, $tester->execute([]));
65+
66+
$linesOutput = array_filter(explode(PHP_EOL, $tester->getDisplay()));
67+
$this->assertEquals('Dummy test exception', $linesOutput[0]);
68+
}
69+
70+
public function testExecute()
71+
{
72+
$websiteData = [
73+
'id' => '444',
74+
'default_group_id' => '555',
75+
'name' => 'unit test website',
76+
'code' => 'unit_test_website',
77+
'is_default' => '0',
78+
'sort_order' => '987',
79+
];
80+
81+
$websites = [
82+
$this->objectManager->getObject(Website::class)->setData($websiteData),
83+
];
84+
85+
$this->websiteRepositoryMock->expects($this->any())
86+
->method('getList')
87+
->willReturn($websites);
88+
89+
$tester = new CommandTester($this->command);
90+
$this->assertEquals(Cli::RETURN_SUCCESS, $tester->execute([]));
91+
92+
$linesOutput = array_filter(explode(PHP_EOL, $tester->getDisplay()));
93+
$this->assertCount(5, $linesOutput, 'There should be 5 lines output. 3 Spacers, 1 header, 1 content.');
94+
95+
$this->assertEquals($linesOutput[0], $linesOutput[2], "Lines 0, 2, 4 should be spacer lines");
96+
$this->assertEquals($linesOutput[2], $linesOutput[4], "Lines 0, 2, 4 should be spacer lines");
97+
98+
$headerValues = array_values(array_filter(explode('|', $linesOutput[1])));
99+
//trim to remove the whitespace left from the exploding pipe separation
100+
$this->assertEquals('ID', trim($headerValues[0]));
101+
$this->assertEquals('Default Group Id', trim($headerValues[1]));
102+
$this->assertEquals('Name', trim($headerValues[2]));
103+
$this->assertEquals('Code', trim($headerValues[3]));
104+
$this->assertEquals('Sort Order', trim($headerValues[4]));
105+
$this->assertEquals('Is Default', trim($headerValues[5]));
106+
107+
$websiteValues = array_values(array_filter(explode('|', $linesOutput[3])));
108+
$this->assertEquals('444', trim($websiteValues[0]));
109+
$this->assertEquals('555', trim($websiteValues[1]));
110+
$this->assertEquals('unit test website', trim($websiteValues[2]));
111+
$this->assertEquals('unit_test_website', trim($websiteValues[3]));
112+
$this->assertEquals('987', trim($websiteValues[4]));
113+
$this->assertEquals('0', trim($websiteValues[5]));
114+
}
115+
}

app/code/Magento/Store/etc/di.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,4 +373,12 @@
373373
</argument>
374374
</arguments>
375375
</type>
376+
<type name="Magento\Framework\Console\CommandListInterface">
377+
<arguments>
378+
<argument name="commands" xsi:type="array">
379+
<item name="commandStoreList" xsi:type="object">Magento\Store\Console\Command\StoreListCommand</item>
380+
<item name="commandWebsiteList" xsi:type="object">Magento\Store\Console\Command\WebsiteListCommand</item>
381+
</argument>
382+
</arguments>
383+
</type>
376384
</config>

0 commit comments

Comments
 (0)