Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
BackupActionItemsTest.php
Go to the documentation of this file.
1 <?php
8 
9 use \Magento\Setup\Controller\BackupActionItems;
10 use \Magento\Setup\Controller\ResponseTypeInterface;
11 
15 class BackupActionItemsTest extends \PHPUnit\Framework\TestCase
16 {
20  private $objectManagerProvider;
21 
25  private $log;
26 
30  private $backupRollback;
31 
35  private $directoryList;
36 
40  private $filesystem;
41 
47  private $controller;
48 
49  public function setUp()
50  {
51  $this->directoryList =
52  $this->createMock(\Magento\Framework\App\Filesystem\DirectoryList::class);
53  $this->objectManagerProvider =
54  $this->createMock(\Magento\Setup\Model\ObjectManagerProvider::class);
55  $this->backupRollback =
56  $this->createPartialMock(\Magento\Framework\Setup\BackupRollback::class, ['getDBDiskSpace', 'dbBackup']);
57  $objectManager = $this->createMock(\Magento\Framework\ObjectManagerInterface::class);
58  $objectManager->expects($this->once())->method('create')->willReturn($this->backupRollback);
59  $this->objectManagerProvider->expects($this->once())->method('get')->willReturn($objectManager);
60  $this->log = $this->createMock(\Magento\Setup\Model\WebLogger::class);
61  $this->filesystem = $this->createMock(\Magento\Framework\Backup\Filesystem::class);
62 
63  $this->controller = new BackupActionItems(
64  $this->objectManagerProvider,
65  $this->log,
66  $this->directoryList,
67  $this->filesystem
68  );
69 
70  $request = $this->createMock(\Zend\Http\PhpEnvironment\Request::class);
71  $response = $this->createMock(\Zend\Http\PhpEnvironment\Response::class);
72  $routeMatch = $this->createMock(\Zend\Mvc\Router\RouteMatch::class);
73 
74  $mvcEvent = $this->createMock(\Zend\Mvc\MvcEvent::class);
75  $mvcEvent->expects($this->any())->method('setRequest')->with($request)->willReturn($mvcEvent);
76  $mvcEvent->expects($this->any())->method('setResponse')->with($response)->willReturn($mvcEvent);
77  $mvcEvent->expects($this->any())->method('setTarget')->with($this->controller)->willReturn($mvcEvent);
78  $mvcEvent->expects($this->any())->method('getRouteMatch')->willReturn($routeMatch);
79  $contentArray = '{"options":{"code":false,"media":false,"db":true}}';
80  $request->expects($this->any())->method('getContent')->willReturn($contentArray);
81 
82  $this->controller->setEvent($mvcEvent);
83  $this->controller->dispatch($request, $response);
84  }
85 
86  public function testCheckAction()
87  {
88  $this->backupRollback->expects($this->once())->method('getDBDiskSpace')->willReturn(500);
89  $this->directoryList->expects($this->once())->method('getPath')->willReturn(__DIR__);
90  $this->filesystem->expects($this->once())->method('validateAvailableDiscSpace');
91  $jsonModel = $this->controller->checkAction();
92  $this->assertInstanceOf(\Zend\View\Model\JsonModel::class, $jsonModel);
93  $variables = $jsonModel->getVariables();
94  $this->assertArrayHasKey('responseType', $variables);
95  $this->assertEquals(ResponseTypeInterface::RESPONSE_TYPE_SUCCESS, $variables['responseType']);
96  $this->assertArrayHasKey('size', $variables);
97  $this->assertEquals(true, $variables['size']);
98  }
99 
100  public function testCheckActionWithError()
101  {
102  $this->directoryList->expects($this->once())->method('getPath')->willReturn(__DIR__);
103  $this->filesystem->expects($this->once())->method('validateAvailableDiscSpace')->will(
104  $this->throwException(new \Exception("Test error message"))
105  );
106  $jsonModel = $this->controller->checkAction();
107  $this->assertInstanceOf(\Zend\View\Model\JsonModel::class, $jsonModel);
108  $variables = $jsonModel->getVariables();
109  $this->assertArrayHasKey('responseType', $variables);
110  $this->assertEquals(ResponseTypeInterface::RESPONSE_TYPE_ERROR, $variables['responseType']);
111  $this->assertArrayHasKey('error', $variables);
112  $this->assertEquals("Test error message", $variables['error']);
113  }
114 
115  public function testCreateAction()
116  {
117  $this->backupRollback->expects($this->once())->method('dbBackup')->willReturn('backup/path/');
118  $jsonModel = $this->controller->createAction();
119  $this->assertInstanceOf(\Zend\View\Model\JsonModel::class, $jsonModel);
120  $variables = $jsonModel->getVariables();
121  $this->assertArrayHasKey('responseType', $variables);
122  $this->assertEquals(ResponseTypeInterface::RESPONSE_TYPE_SUCCESS, $variables['responseType']);
123  $this->assertArrayHasKey('files', $variables);
124  $this->assertEquals(['backup/path/'], $variables['files']);
125  }
126 
127  public function testIndexAction()
128  {
129  $model = $this->controller->indexAction();
130  $this->assertInstanceOf(\Zend\View\Model\ViewModel::class, $model);
131  }
132 }
$response
Definition: 404.php:11
$objectManager
Definition: bootstrap.php:17
defined('TESTS_BP')||define('TESTS_BP' __DIR__
Definition: _bootstrap.php:60