Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
OtherComponentsGridTest.php
Go to the documentation of this file.
1 <?php
8 
9 use \Magento\Setup\Controller\OtherComponentsGrid;
10 use \Magento\Setup\Controller\ResponseTypeInterface;
12 
13 class OtherComponentsGridTest extends \PHPUnit\Framework\TestCase
14 {
18  private $composerInformation;
19 
23  private $infoCommand;
24 
30  private $controller;
31 
32  public function setUp()
33  {
34  $this->composerInformation = $this->createMock(\Magento\Framework\Composer\ComposerInformation::class);
35  $this->infoCommand = $this->createMock(\Magento\Composer\InfoCommand::class);
36  $magentoComposerApplicationFactory =
37  $this->createMock(\Magento\Framework\Composer\MagentoComposerApplicationFactory::class);
38  $magentoComposerApplicationFactory->expects($this->once())
39  ->method('createInfoCommand')
40  ->willReturn($this->infoCommand);
41  $this->controller = new OtherComponentsGrid(
42  $this->composerInformation,
43  $magentoComposerApplicationFactory
44  );
45  }
46 
47  public function testComponentsAction()
48  {
49  $this->composerInformation->expects($this->once())
50  ->method('getInstalledMagentoPackages')
51  ->willReturn([
52  'magento/sample-module1' => [
53  'name' => 'magento/sample-module1',
54  'type' => 'magento2-module',
55  'version' => '1.0.0'
56  ]
57  ]);
58  $this->composerInformation->expects($this->once())
59  ->method('isPackageInComposerJson')
60  ->willReturn(true);
61  $this->infoCommand->expects($this->once())
62  ->method('run')
63  ->willReturn([
64  'versions' => '3.0.0, 2.0.0',
65  'current_version' => '1.0.0',
66  'new_versions' => [
67  '3.0.0',
68  '2.0.0'
69  ]
70  ]);
71  $jsonModel = $this->controller->componentsAction();
72  $this->assertInstanceOf(\Zend\View\Model\JsonModel::class, $jsonModel);
73  $variables = $jsonModel->getVariables();
74  $this->assertArrayHasKey('responseType', $variables);
75  $this->assertEquals(ResponseTypeInterface::RESPONSE_TYPE_SUCCESS, $variables['responseType']);
76  $this->assertArrayHasKey('components', $variables);
77  $expected = [
78  '0' => [
79  'name' => 'magento/sample-module1',
80  'type' => 'magento2-module',
81  'version' => '1.0.0',
82  'vendor' => 'magento',
83  'updates' => [
84  [
85  'id' => '3.0.0',
86  'name' => '3.0.0 (latest)'
87  ],
88  [
89  'id' => '2.0.0',
90  'name' => '2.0.0'
91  ],
92  [
93  'id' => '1.0.0',
94  'name' => '1.0.0 (current)'
95  ]
96  ],
97  'dropdownId' => 'dd_magento/sample-module1',
98  'checkboxId' => 'cb_magento/sample-module1'
99  ]
100  ];
101  $this->assertEquals($expected, $variables['components']);
102  $this->assertArrayHasKey('total', $variables);
103  $this->assertEquals(1, $variables['total']);
104  }
105 
107  {
108  $this->composerInformation->expects($this->once())
109  ->method('getInstalledMagentoPackages')
110  ->will($this->throwException(new \Exception("Test error message")));
111  $jsonModel = $this->controller->componentsAction();
112  $this->assertInstanceOf(\Zend\View\Model\JsonModel::class, $jsonModel);
113  $variables = $jsonModel->getVariables();
114  $this->assertArrayHasKey('responseType', $variables);
115  $this->assertEquals(ResponseTypeInterface::RESPONSE_TYPE_ERROR, $variables['responseType']);
116  }
117 
118  public function testIndexAction()
119  {
120  $model = $this->controller->indexAction();
121  $this->assertInstanceOf(\Zend\View\Model\ViewModel::class, $model);
122  }
123 }