Completed
Push — master ( ac98ee...01982b )
by ANTHONIUS
02:45
created

BackupCommandTest::testExecute()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 8.9713
c 0
b 0
f 0
cc 1
eloc 19
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the dotfiles project.
7
 *
8
 *     (c) Anthonius Munthi <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Dotfiles\Core\Tests\Command;
15
16
use Dotfiles\Core\Command\BackupCommand;
17
use Dotfiles\Core\Config\Config;
18
use Dotfiles\Core\Tests\CommandTestCase;
19
use Dotfiles\Core\Tests\CommandTester;
20
use Dotfiles\Core\Util\Toolkit;
21
use Psr\Log\LoggerInterface;
22
23
class BackupCommandTest extends CommandTestCase
24
{
25
    private $dirs = array();
26
27
    public function getObjectToTest($config = array())
28
    {
29
        $defConfig = array(
30
            'base_dir' => __DIR__.'/fixtures/default',
31
            'backup_dir' => sys_get_temp_dir().'/dotfiles/backup',
32
            'home_dir' => __DIR__.'/fixtures/home',
33
            'machine_name' => 'zeus',
34
        );
35
36
        $this->dirs = $dirs = array_merge($defConfig, $config);
37
38
        $config = $this->createMock(Config::class);
39
        $logger = $this->createMock(LoggerInterface::class);
40
41
        $config->expects($this->any())
42
            ->method('get')
43
            ->willReturnMap(array(
44
                array('dotfiles.base_dir', $dirs['base_dir']),
45
                array('dotfiles.backup_dir', $dirs['backup_dir']),
46
                array('dotfiles.home_dir', $dirs['home_dir']),
47
                array('dotfiles.machine_name', $dirs['machine_name']),
48
            ))
49
        ;
50
51
        return new BackupCommand($config, $logger);
52
    }
53
54
    public function testExecute(): void
55
    {
56
        $command = $this->getObjectToTest();
57
        $app = $this->getApplication();
58
        $app->add($command);
59
60
        $backupDir = $this->dirs['backup_dir'];
61
        Toolkit::ensureDir($backupDir);
62
        touch($backupDir.'/manifest.php');
63
        $tester = new CommandTester($command);
64
        $tester->execute(array());
65
66
        $output = $tester->getDisplay(true);
67
        $this->assertContains('Backup files already exists', $output);
68
        unlink($backupDir.'/manifest.php');
69
        $tester->execute(array());
70
        $output = $tester->getDisplay(true);
71
        $this->assertNotContains('Backup file already exists', $output);
72
        $this->assertFileExists($backupDir.'/.bashrc');
73
        $this->assertFileExists($backupDir.'/.zeus');
74
        $this->assertFileExists($backupDir.'/manifest.php');
75
76
        $manifest = include $backupDir.'/manifest.php';
77
        $this->assertArrayHasKey('.bashrc', $manifest);
78
    }
79
}
80