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

InstallCommandTest::getBackupCommand()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 17
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\Command\InstallCommand;
18
use Dotfiles\Core\Config\Config;
19
use Dotfiles\Core\Event\Dispatcher;
20
use Dotfiles\Core\Tests\CommandTestCase;
21
use Dotfiles\Core\Tests\CommandTester;
22
use PHPUnit\Framework\MockObject\MockObject;
23
use Psr\Log\LoggerInterface;
24
25
class InstallCommandTest extends CommandTestCase
26
{
27
    /**
28
     * @var MockObject
29
     */
30
    private $config;
31
    /**
32
     * @var MockObject
33
     */
34
    private $dispatcher;
35
36
    /**
37
     * @var MockObject
38
     */
39
    private $logger;
40
41
    public function getTestProcessMachine()
42
    {
43
        return array(
44
            array('zeus'),
45
            array('athena'),
46
        );
47
    }
48
49
    public function testExecute(): void
50
    {
51
        $command = $this->getClassToTest();
52
53
        $backup = $this->getBackupCommand();
54
        $backup->expects($this->once())
55
            ->method('execute')
56
        ;
57
58
        $app = $this->getApplication();
59
        $app->add($command);
60
        $app->add($backup);
61
        $command = $app->find('install');
62
        $tester = new CommandTester($command);
63
        $tester->execute(array());
64
        $output = $tester->getDisplay(true);
65
66
        $this->assertContains('Begin installing dotfiles', $output);
67
    }
68
69
    /**
70
     * @dataProvider getTestProcessMachine
71
     */
72
    public function testProcessMachine($machine): void
73
    {
74
        $fixturesDir = __DIR__.'/fixtures/default';
75
        $command = $this->getClassToTest($fixturesDir, $machine);
76
77
        $backup = $this->getBackupCommand();
78
        $app = $this->getApplication();
79
        $app->add($command);
80
        $app->add($backup);
81
        $command = $app->find('install');
82
        $tester = new CommandTester($command);
83
        $tester->execute(array());
84
85
        $output = $tester->getDisplay(true);
86
        $home = getenv('HOME');
87
        $binDir = sys_get_temp_dir().'/dotfiles/tests/bin';
88
        $this->assertFileExists($home.'/.config/i3/config');
89
        $this->assertFileExists($home.'/.'.$machine);
90
        $this->assertFileExists($binDir.'/default-bin');
91
        $this->assertFileExists($binDir."/{$machine}-bin");
92
        $this->assertContains('default install hook', $output);
93
        $this->assertContains("$machine install hook", $output);
94
95
        // testing patch
96
        $file = $home.'/.patch';
97
        $contents = file_get_contents($file);
98
        $this->assertContains('base contents', $contents);
99
        $this->assertContains('patch default', $contents);
100
        $this->assertContains('patch '.$machine, $contents);
101
    }
102
103
    /**
104
     * @return MockObject
105
     *
106
     * @throws \ReflectionException
107
     */
108
    private function getBackupCommand()
109
    {
110
        $backup = $this->getMockBuilder(BackupCommand::class)
111
            ->setMethods(array('execute', 'isEnabled', 'getName', 'getDefinition', 'getAliases'))
112
            ->disableOriginalConstructor()
113
            ->getMock()
114
        ;
115
        $backup->expects($this->any())
116
            ->method('isEnabled')
117
            ->willReturn(true)
118
        ;
119
        $backup->expects($this->any())
120
            ->method('getName')
121
            ->willReturn('backup')
122
        ;
123
        $backup->expects($this->any())
124
            ->method('getDefinition')
125
            ->willReturn('Backup command')
126
        ;
127
        $backup->expects($this->any())
128
            ->method('getAliases')
129
            ->willReturn(array())
130
        ;
131
132
        return $backup;
133
    }
134
135
    private function getClassToTest($baseDir = null, $machineName = null)
136
    {
137
        $this->dispatcher = $this->createMock(Dispatcher::class);
138
        $this->config = $this->createMock(Config::class);
139
        $this->logger = $this->createMock(LoggerInterface::class);
140
        $binDir = sys_get_temp_dir().'/dotfiles/tests/bin';
141
        $vendorDir = sys_get_temp_dir().'/dotfiles/tests/vendor';
142
        $backupDir = sys_get_temp_dir().'/dotfiles/backup';
143
        $this->config->expects($this->any())
144
            ->method('get')
145
            ->willReturnMap(array(
146
                array('dotfiles.bin_dir', $binDir),
147
                array('dotfiles.vendor_dir', $vendorDir),
148
                array('dotfiles.base_dir', $baseDir),
149
                array('dotfiles.machine_name', $machineName),
150
                array('dotfiles.backup_dir', $backupDir),
151
                array('dotfiles.home_dir', getenv('HOME')),
152
            ))
153
        ;
154
        $command = new InstallCommand(
155
            null,
156
            $this->dispatcher,
157
            $this->config,
158
            $this->logger
159
        );
160
161
        return $command;
162
    }
163
}
164