Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
RollbackCommandTest.php
Go to the documentation of this file.
1 <?php
7 
10 use Symfony\Component\Console\Tester\CommandTester;
11 
15 class RollbackCommandTest extends \PHPUnit\Framework\TestCase
16 {
20  private $objectManager;
21 
25  private $tester;
26 
30  private $deploymentConfig;
31 
35  private $backupRollback;
36 
40  private $backupRollbackFactory;
41 
45  private $helperSet;
46 
50  private $question;
51 
55  private $command;
56 
57  public function setUp()
58  {
59  $this->deploymentConfig = $this->createMock(\Magento\Framework\App\DeploymentConfig::class);
60  $maintenanceMode = $this->createMock(\Magento\Framework\App\MaintenanceMode::class);
61  $this->objectManager = $this->getMockForAbstractClass(
62  \Magento\Framework\ObjectManagerInterface::class,
63  [],
64  '',
65  false
66  );
67  $objectManagerProvider = $this->createMock(\Magento\Setup\Model\ObjectManagerProvider::class);
68  $objectManagerProvider->expects($this->any())->method('get')->willReturn($this->objectManager);
69  $this->backupRollback = $this->createMock(\Magento\Framework\Setup\BackupRollback::class);
70  $this->backupRollbackFactory = $this->createMock(\Magento\Framework\Setup\BackupRollbackFactory::class);
71  $this->backupRollbackFactory->expects($this->any())
72  ->method('create')
73  ->willReturn($this->backupRollback);
74  $appState = $this->createMock(\Magento\Framework\App\State::class);
75  $configLoader = $this->getMockForAbstractClass(
76  \Magento\Framework\ObjectManager\ConfigLoaderInterface::class,
77  [],
78  '',
79  false
80  );
81  $configLoader->expects($this->any())->method('load')->willReturn([]);
82  $this->objectManager->expects($this->any())
83  ->method('get')
84  ->will($this->returnValueMap([
85  [\Magento\Framework\Setup\BackupRollbackFactory::class, $this->backupRollbackFactory],
86  [\Magento\Framework\App\State::class, $appState],
87  [\Magento\Framework\ObjectManager\ConfigLoaderInterface::class, $configLoader],
88  ]));
89  $this->helperSet = $this->createMock(\Symfony\Component\Console\Helper\HelperSet::class);
90  $this->question = $this->createMock(\Symfony\Component\Console\Helper\QuestionHelper::class);
91  $this->question
92  ->expects($this->any())
93  ->method('ask')
94  ->will($this->returnValue(true));
95  $this->helperSet
96  ->expects($this->any())
97  ->method('get')
98  ->with('question')
99  ->will($this->returnValue($this->question));
100  $this->command = new RollbackCommand(
101  $objectManagerProvider,
102  $maintenanceMode,
103  $this->deploymentConfig,
104  new MaintenanceModeEnabler($maintenanceMode)
105  );
106  $this->command->setHelperSet($this->helperSet);
107  $this->tester = new CommandTester($this->command);
108  }
109 
110  public function testExecuteCodeRollback()
111  {
112  $this->deploymentConfig->expects($this->once())
113  ->method('isAvailable')
114  ->will($this->returnValue(true));
115  $this->backupRollback->expects($this->once())
116  ->method('codeRollback')
117  ->willReturn($this->backupRollback);
118  $this->tester->execute(['--code-file' => 'A.tgz']);
119  }
120 
121  public function testExecuteMediaRollback()
122  {
123  $this->deploymentConfig->expects($this->once())
124  ->method('isAvailable')
125  ->will($this->returnValue(true));
126  $this->backupRollback->expects($this->once())
127  ->method('codeRollback')
128  ->willReturn($this->backupRollback);
129  $this->tester->execute(['--media-file' => 'A.tgz']);
130  }
131 
132  public function testExecuteDBRollback()
133  {
134  $this->deploymentConfig->expects($this->once())
135  ->method('isAvailable')
136  ->will($this->returnValue(true));
137  $this->backupRollback->expects($this->once())
138  ->method('dbRollback')
139  ->willReturn($this->backupRollback);
140  $this->tester->execute(['--db-file' => 'C.gz']);
141  }
142 
143  public function testExecuteNotInstalled()
144  {
145  $this->deploymentConfig->expects($this->once())
146  ->method('isAvailable')
147  ->will($this->returnValue(false));
148  $this->tester->execute(['--db-file' => 'C.gz']);
149  $this->assertStringMatchesFormat(
150  'No information is available: the Magento application is not installed.%w',
151  $this->tester->getDisplay()
152  );
153  }
154 
155  public function testExecuteNoOptions()
156  {
157  $this->deploymentConfig->expects($this->once())
158  ->method('isAvailable')
159  ->will($this->returnValue(true));
160  $this->tester->execute([]);
161  $expected = 'Enabling maintenance mode' . PHP_EOL
162  . 'Not enough information provided to roll back.' . PHP_EOL
163  . 'Disabling maintenance mode' . PHP_EOL;
164  $this->assertSame($expected, $this->tester->getDisplay());
165  }
166 
167  public function testInteraction()
168  {
169  $this->deploymentConfig->expects($this->once())
170  ->method('isAvailable')
171  ->will($this->returnValue(true));
172  $this->question
173  ->expects($this->atLeast(2))
174  ->method('ask')
175  ->will($this->returnValue(false));
176  $this->helperSet
177  ->expects($this->once())
178  ->method('get')
179  ->with('question')
180  ->will($this->returnValue($this->question));
181  $this->command->setHelperSet($this->helperSet);
182  $this->tester = new CommandTester($this->command);
183  $this->tester->execute(['--db-file' => 'C.gz']);
184  }
185 }