Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
MarketplaceTest.php
Go to the documentation of this file.
1 <?php
8 
9 use \Magento\Setup\Controller\Marketplace;
10 
11 class MarketplaceTest extends \PHPUnit\Framework\TestCase
12 {
16  private $packagesAuth;
17 
21  private $packagesData;
22 
28  private $controller;
29 
30  public function setUp()
31  {
32  $this->packagesAuth = $this->createMock(\Magento\Setup\Model\PackagesAuth::class);
33  $this->packagesData = $this->createMock(\Magento\Setup\Model\PackagesData::class);
34  $this->controller = new Marketplace($this->packagesAuth, $this->packagesData);
35  }
36 
37  public function testSaveAuthJsonAction()
38  {
39  $this->packagesAuth
40  ->expects($this->once())
41  ->method('checkCredentials')
42  ->will($this->returnValue(json_encode(['success' => true])));
43  $this->packagesAuth
44  ->expects($this->once())
45  ->method('saveAuthJson')
46  ->willReturn(true);
47  $jsonModel = $this->controller->saveAuthJsonAction();
48  $this->assertInstanceOf(\Zend\View\Model\ViewModel::class, $jsonModel);
49  $variables = $jsonModel->getVariables();
50  $this->assertArrayHasKey('success', $variables);
51  $this->assertTrue($variables['success']);
52  }
53 
55  {
56  $this->packagesAuth
57  ->expects($this->once())
58  ->method('checkCredentials')
59  ->will($this->throwException(new \Exception));
60  $this->packagesAuth->expects($this->never())->method('saveAuthJson');
61  $jsonModel = $this->controller->saveAuthJsonAction();
62  $this->assertInstanceOf(\Zend\View\Model\JsonModel::class, $jsonModel);
63  $variables = $jsonModel->getVariables();
64  $this->assertArrayHasKey('success', $variables);
65  $this->assertArrayHasKey('message', $variables);
66  $this->assertFalse($variables['success']);
67  }
68 
69  public function testCheckAuthAction()
70  {
71  $this->packagesAuth
72  ->expects($this->once())
73  ->method('getAuthJsonData')
74  ->will($this->returnValue(['username' => 'test', 'password' => 'test']));
75  $this->packagesAuth
76  ->expects($this->once())
77  ->method('checkCredentials')
78  ->will($this->returnValue(json_encode(['success' => true])));
79  $jsonModel = $this->controller->checkAuthAction();
80  $this->assertInstanceOf(\Zend\View\Model\ViewModel::class, $jsonModel);
81  $variables = $jsonModel->getVariables();
82  $this->assertArrayHasKey('success', $variables);
83  $this->assertTrue($variables['success']);
84  }
85 
86  public function testCheckAuthActionWithError()
87  {
88  $this->packagesAuth
89  ->expects($this->once())
90  ->method('getAuthJsonData')
91  ->will($this->throwException(new \Exception));
92  $jsonModel = $this->controller->checkAuthAction();
93  $this->assertInstanceOf(\Zend\View\Model\JsonModel::class, $jsonModel);
94  $variables = $jsonModel->getVariables();
95  $this->assertArrayHasKey('success', $variables);
96  $this->assertArrayHasKey('message', $variables);
97  $this->assertFalse($variables['success']);
98  }
99 
100  public function testRemoveCredentialsAction()
101  {
102  $this->packagesAuth
103  ->expects($this->once())
104  ->method('removeCredentials')
105  ->will($this->returnValue(true));
106 
107  $jsonModel = $this->controller->removeCredentialsAction();
108  $this->assertInstanceOf(\Zend\View\Model\ViewModel::class, $jsonModel);
109  $variables = $jsonModel->getVariables();
110  $this->assertArrayHasKey('success', $variables);
111  $this->assertTrue($variables['success']);
112  }
113 
115  {
116  $this->packagesAuth
117  ->expects($this->once())
118  ->method('removeCredentials')
119  ->will($this->throwException(new \Exception));
120  $jsonModel = $this->controller->removeCredentialsAction();
121  $this->assertInstanceOf(\Zend\View\Model\JsonModel::class, $jsonModel);
122  $variables = $jsonModel->getVariables();
123  $this->assertArrayHasKey('success', $variables);
124  $this->assertArrayHasKey('message', $variables);
125  $this->assertFalse($variables['success']);
126  }
127 
128  public function testPopupAuthAction()
129  {
130  $viewModel = $this->controller->popupAuthAction();
131  $this->assertInstanceOf(\Zend\View\Model\ViewModel::class, $viewModel);
132  $this->assertTrue($viewModel->terminate());
133  }
134 
135  public function testIndexAction()
136  {
137  $model = $this->controller->indexAction();
138  $this->assertInstanceOf(\Zend\View\Model\ViewModel::class, $model);
139  }
140 }
$viewModel