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