Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
StoreListCommandTest.php
Go to the documentation of this file.
1 <?php
7 
9 use Symfony\Component\Console\Tester\CommandTester;
10 use Symfony\Component\Console\Helper\HelperSet;
13 
17 class StoreListCommandTest extends \PHPUnit\Framework\TestCase
18 {
22  private $command;
23 
27  private $storeManagerMock;
28 
32  private $objectManager;
33 
34  protected function setUp()
35  {
36  $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
37 
38  $this->storeManagerMock = $this->getMockForAbstractClass(\Magento\Store\Model\StoreManagerInterface::class);
39 
40  $this->command = $this->objectManager->getObject(
41  StoreListCommand::class,
42  ['storeManager' => $this->storeManagerMock]
43  );
44  }
45 
47  {
48  $this->storeManagerMock->expects($this->any())
49  ->method('getStores')
50  ->willThrowException(new \Exception("Dummy test exception"));
51 
52  $tester = new CommandTester($this->command);
53  $this->assertEquals(Cli::RETURN_FAILURE, $tester->execute([]));
54 
55  $linesOutput = array_filter(explode(PHP_EOL, $tester->getDisplay()));
56  $this->assertEquals('Dummy test exception', $linesOutput[0]);
57  }
58 
59  public function testExecute()
60  {
61  $storeData = [
62  'store_id' => '999',
63  'group_id' => '777',
64  'website_id' => '888',
65  'name' => 'unit test store',
66  'code' => 'unit_test_store',
67  'is_active' => '1',
68  'sort_order' => '123',
69  ];
70 
71  $stores = [
72  $this->objectManager->getObject(Store::class)->setData($storeData),
73  ];
74 
75  $this->storeManagerMock->expects($this->any())
76  ->method('getStores')
77  ->willReturn($stores);
78 
79  $tester = new CommandTester($this->command);
80  $this->assertEquals(Cli::RETURN_SUCCESS, $tester->execute([]));
81 
82  $linesOutput = array_filter(explode(PHP_EOL, $tester->getDisplay()));
83  $this->assertCount(5, $linesOutput, 'There should be 5 lines output. 3 Spacers, 1 header, 1 content.');
84 
85  $this->assertEquals($linesOutput[0], $linesOutput[2], "Lines 0, 2, 4 should be spacer lines");
86  $this->assertEquals($linesOutput[2], $linesOutput[4], "Lines 0, 2, 4 should be spacer lines");
87 
88  $headerValues = array_values(array_filter(explode('|', $linesOutput[1])));
89  //trim to remove the whitespace left from the exploding pipe separation
90  $this->assertEquals('ID', trim($headerValues[0]));
91  $this->assertEquals('Website ID', trim($headerValues[1]));
92  $this->assertEquals('Group ID', trim($headerValues[2]));
93  $this->assertEquals('Name', trim($headerValues[3]));
94  $this->assertEquals('Code', trim($headerValues[4]));
95  $this->assertEquals('Sort Order', trim($headerValues[5]));
96  $this->assertEquals('Is Active', trim($headerValues[6]));
97 
98  $storeValues = array_values(array_filter(explode('|', $linesOutput[3])));
99  $this->assertEquals('999', trim($storeValues[0]));
100  $this->assertEquals('888', trim($storeValues[1]));
101  $this->assertEquals('777', trim($storeValues[2]));
102  $this->assertEquals('unit test store', trim($storeValues[3]));
103  $this->assertEquals('unit_test_store', trim($storeValues[4]));
104  $this->assertEquals('123', trim($storeValues[5]));
105  $this->assertEquals('1', trim($storeValues[6]));
106  }
107 }