Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ExtensionGridTest.php
Go to the documentation of this file.
1 <?php
7 
12 use PHPUnit_Framework_MockObject_MockObject as MockObject;
13 
17 class ExtensionGridTest extends \PHPUnit\Framework\TestCase
18 {
22  private $gridExtensionMock;
23 
29  private $controller;
30 
34  private $packagesDataMock;
35 
39  private $packagesAuthMock;
40 
44  private $extensionData = [];
45 
49  private $lastSyncData = [];
50 
54  const FORMATTED_DATE = 'Jan 15 1980';
55  const FORMATTED_TIME = '01:55PM';
58  public function setUp()
59  {
60  $this->lastSyncData = [
61  "lastSyncDate" => [
62  'date' => self::FORMATTED_DATE,
63  'time' => self::FORMATTED_TIME,
64  ],
65  "packages" => [
66  'magento/sample-module-one' => [
67  'name' => 'magento/sample-module-one',
68  'type' => 'magento2-module',
69  'version' => '1.0.0'
70  ]
71  ],
72  'countOfInstall' => 0,
73  'countOfUpdate' => 1
74  ];
75  $this->extensionData = [
76  [
77  'name' => 'magento/sample-module-one',
78  'type' => 'magento2-module',
79  'version' => '1.0.0',
80  'update' => false,
81  'uninstall' => true,
82  'vendor' => 'magento',
83  ]
84  ];
85 
86  $this->packagesDataMock = $this->createMock(PackagesData::class);
87  $this->packagesAuthMock = $this->createMock(PackagesAuth::class);
88  $this->gridExtensionMock = $this->createMock(Extension::class);
89 
90  $this->controller = new ExtensionGrid(
91  $this->packagesDataMock,
92  $this->packagesAuthMock,
93  $this->gridExtensionMock
94  );
95  }
96 
97  public function testIndexAction()
98  {
99  $viewModel = $this->controller->indexAction();
100  $this->assertInstanceOf(\Zend\View\Model\ViewModel::class, $viewModel);
101  $this->assertTrue($viewModel->terminate());
102  }
103 
104  public function testExtensionsAction()
105  {
106  $this->gridExtensionMock->expects($this->once())
107  ->method('getList')
108  ->willReturn($this->extensionData);
109  $this->packagesDataMock->expects($this->once())
110  ->method('syncPackagesData')
111  ->willReturn($this->lastSyncData);
112  $this->packagesAuthMock->expects($this->once())
113  ->method('getAuthJsonData')
114  ->willReturn(
115  [
116  'username' => 'someusername',
117  'password' => 'somepassword'
118  ]
119  );
120 
121  $jsonModel = $this->controller->extensionsAction();
122  $this->assertInstanceOf(\Zend\View\Model\JsonModel::class, $jsonModel);
123  $variables = $jsonModel->getVariables();
124  $this->assertArrayHasKey('success', $variables);
125  $this->assertTrue($variables['success']);
126  $this->assertEquals($this->extensionData, $variables['extensions']);
127  $this->assertArrayHasKey('total', $variables);
128  $this->assertEquals(1, $variables['total']);
129  $this->assertEquals($this->lastSyncData, $variables['lastSyncData']);
130  }
131 
132  public function testSyncAction()
133  {
134  $authDataJson = ['username' => 'admin', 'password' => '12345'];
135 
136  $this->packagesDataMock->expects($this->once())
137  ->method('syncPackagesData')
138  ->willReturn($this->lastSyncData);
139  $this->packagesAuthMock->expects($this->once())
140  ->method('getAuthJsonData')
141  ->willReturn($authDataJson);
142  $this->packagesAuthMock->expects($this->once())
143  ->method('checkCredentials')
144  ->with(
145  $authDataJson['username'],
146  $authDataJson['password']
147  );
148 
149  $jsonModel = $this->controller->syncAction();
150  $this->assertInstanceOf(\Zend\View\Model\JsonModel::class, $jsonModel);
151  $variables = $jsonModel->getVariables();
152  $this->assertArrayHasKey('success', $variables);
153  $this->assertTrue($variables['success']);
154  $this->assertEquals($this->lastSyncData, $variables['lastSyncData']);
155  }
156 }
$viewModel