Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
BackupCommandTest.php
Go to the documentation of this file.
1 <?php
7 
10 use Symfony\Component\Console\Tester\CommandTester;
11 
12 class BackupCommandTest extends \PHPUnit\Framework\TestCase
13 {
17  private $objectManager;
18 
22  private $backupRollback;
23 
27  private $tester;
28 
32  private $backupRollbackFactory;
33 
37  private $deploymentConfig;
38 
39  public function setUp()
40  {
41  $maintenanceMode = $this->createMock(\Magento\Framework\App\MaintenanceMode::class);
42  $objectManagerProvider = $this->createMock(\Magento\Setup\Model\ObjectManagerProvider::class);
43  $this->objectManager = $this->getMockForAbstractClass(
44  \Magento\Framework\ObjectManagerInterface::class,
45  [],
46  '',
47  false
48  );
49  $objectManagerProvider->expects($this->any())->method('get')->willReturn($this->objectManager);
50  $this->backupRollback = $this->createMock(\Magento\Framework\Setup\BackupRollback::class);
51  $this->backupRollbackFactory = $this->createMock(\Magento\Framework\Setup\BackupRollbackFactory::class);
52  $this->backupRollbackFactory->expects($this->any())
53  ->method('create')
54  ->willReturn($this->backupRollback);
55  $this->deploymentConfig = $this->createMock(\Magento\Framework\App\DeploymentConfig::class);
56  $appState = $this->createMock(\Magento\Framework\App\State::class);
57  $configLoader = $this->getMockForAbstractClass(
58  \Magento\Framework\ObjectManager\ConfigLoaderInterface::class,
59  [],
60  '',
61  false
62  );
63  $configLoader->expects($this->any())->method('load')->willReturn([]);
64 
65  $this->objectManager->expects($this->any())
66  ->method('get')
67  ->will(
68  $this->returnValueMap([
69  [\Magento\Framework\Setup\BackupRollbackFactory::class, $this->backupRollbackFactory],
70  [\Magento\Framework\App\State::class, $appState],
71  [\Magento\Framework\ObjectManager\ConfigLoaderInterface::class, $configLoader],
72  ])
73  );
74  $command = new BackupCommand(
75  $objectManagerProvider,
76  $maintenanceMode,
77  $this->deploymentConfig,
78  new MaintenanceModeEnabler($maintenanceMode)
79  );
80  $this->tester = new CommandTester($command);
81  }
82 
83  public function testExecuteCodeBackup()
84  {
85  $this->deploymentConfig->expects($this->once())
86  ->method('isAvailable')
87  ->will($this->returnValue(true));
88  $this->backupRollback->expects($this->once())
89  ->method('codeBackup')
90  ->willReturn($this->backupRollback);
91  $this->tester->execute(['--code' => true]);
92  }
93 
94  public function testExecuteMediaBackup()
95  {
96  $this->deploymentConfig->expects($this->once())
97  ->method('isAvailable')
98  ->will($this->returnValue(true));
99  $this->backupRollback->expects($this->once())
100  ->method('codeBackup')
101  ->willReturn($this->backupRollback);
102  $this->tester->execute(['--media' => true]);
103  }
104 
105  public function testExecuteDBBackup()
106  {
107  $this->deploymentConfig->expects($this->once())
108  ->method('isAvailable')
109  ->will($this->returnValue(true));
110  $this->backupRollback->expects($this->once())
111  ->method('dbBackup')
112  ->willReturn($this->backupRollback);
113  $this->tester->execute(['--db' => true]);
114  }
115 
116  public function testExecuteNotInstalled()
117  {
118  $this->deploymentConfig->expects($this->once())
119  ->method('isAvailable')
120  ->will($this->returnValue(false));
121  $this->tester->execute(['--db' => true]);
122  $this->assertStringMatchesFormat(
123  'No information is available: the Magento application is not installed.%w',
124  $this->tester->getDisplay()
125  );
126  }
127 
128  public function testExecuteNoOptions()
129  {
130  $this->deploymentConfig->expects($this->once())
131  ->method('isAvailable')
132  ->will($this->returnValue(false));
133  $this->tester->execute([]);
134  $expected = 'Enabling maintenance mode' . PHP_EOL
135  . 'Not enough information provided to take backup.' . PHP_EOL
136  . 'Disabling maintenance mode' . PHP_EOL;
137  $this->assertSame($expected, $this->tester->getDisplay());
138  }
139 }