Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
WebsiteListCommandTest.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;
14 
18 class WebsiteListCommandTest extends \PHPUnit\Framework\TestCase
19 {
23  private $command;
24 
28  private $websiteRepositoryMock;
29 
33  private $objectManager;
34 
35  protected function setUp()
36  {
37  $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
38 
39  $this->websiteRepositoryMock = $this->getMockForAbstractClass(WebsiteRepositoryInterface::class);
40 
41  $this->command = $this->objectManager->getObject(
42  WebsiteListCommand::class,
43  ['websiteManagement' => $this->websiteRepositoryMock]
44  );
45  }
46 
48  {
49  $this->websiteRepositoryMock->expects($this->any())
50  ->method('getList')
51  ->willThrowException(new \Exception("Dummy test exception"));
52 
53  $tester = new CommandTester($this->command);
54  $this->assertEquals(Cli::RETURN_FAILURE, $tester->execute([]));
55 
56  $linesOutput = array_filter(explode(PHP_EOL, $tester->getDisplay()));
57  $this->assertEquals('Dummy test exception', $linesOutput[0]);
58  }
59 
60  public function testExecute()
61  {
62  $websiteData = [
63  'id' => '444',
64  'default_group_id' => '555',
65  'name' => 'unit test website',
66  'code' => 'unit_test_website',
67  'is_default' => '0',
68  'sort_order' => '987',
69  ];
70 
71  $websites = [
72  $this->objectManager->getObject(Website::class)->setData($websiteData),
73  ];
74 
75  $this->websiteRepositoryMock->expects($this->any())
76  ->method('getList')
77  ->willReturn($websites);
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('Default Group Id', trim($headerValues[1]));
92  $this->assertEquals('Name', trim($headerValues[2]));
93  $this->assertEquals('Code', trim($headerValues[3]));
94  $this->assertEquals('Sort Order', trim($headerValues[4]));
95  $this->assertEquals('Is Default', trim($headerValues[5]));
96 
97  $websiteValues = array_values(array_filter(explode('|', $linesOutput[3])));
98  $this->assertEquals('444', trim($websiteValues[0]));
99  $this->assertEquals('555', trim($websiteValues[1]));
100  $this->assertEquals('unit test website', trim($websiteValues[2]));
101  $this->assertEquals('unit_test_website', trim($websiteValues[3]));
102  $this->assertEquals('987', trim($websiteValues[4]));
103  $this->assertEquals('0', trim($websiteValues[5]));
104  }
105 }