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

InstallerTest::getSUT()   B

Complexity

Conditions 2
Paths 1

Size

Total Lines 38
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 38
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 26
nc 1
nop 2
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\Plugins\Composer\Tests;
15
16
use Dotfiles\Core\Config\Config;
17
use Dotfiles\Core\Tests\BaseTestCase;
18
use Dotfiles\Core\Util\CommandProcessor;
19
use Dotfiles\Core\Util\Downloader;
20
use Dotfiles\Core\Util\Filesystem;
21
use Dotfiles\Core\Util\Toolkit;
22
use Dotfiles\Plugins\Composer\Installer;
23
use PHPUnit\Framework\MockObject\MockObject;
24
use Psr\Log\LoggerInterface;
25
use Symfony\Component\Console\Output\OutputInterface;
26
use Symfony\Component\Process\Process;
27
28
/**
29
 * Class InstallerTest.
30
 *
31
 * @covers \Dotfiles\Plugins\Composer\Installer
32
 */
33
class InstallerTest extends BaseTestCase
34
{
35
    /**
36
     * @var MockObject
37
     */
38
    private $config;
39
40
    /**
41
     * @var MockObject
42
     */
43
    private $downloader;
44
45
    /**
46
     * @var MockObject
47
     */
48
    private $logger;
49
    /**
50
     * @var MockObject
51
     */
52
    private $output;
53
54
    /**
55
     * @var MockObject
56
     */
57
    private $processor;
58
59
    private $tempDir;
60
61
    public function setUp(): void/* The :void return type declaration that should be here would cause a BC issue */
62
    {
63
        $this->output = $this->createMock(OutputInterface::class);
64
        $this->config = $this->createMock(Config::class);
65
        $this->logger = $this->createMock(LoggerInterface::class);
66
        $this->downloader = $this->createMock(Downloader::class);
67
        $this->processor = $this->createMock(CommandProcessor::class);
68
        $this->tempDir = sys_get_temp_dir().'/dotfiles/tests/composer';
69
        static::cleanupTempDir();
70
    }
71
72
    public function testRunOnInstalled(): void
73
    {
74
        $this->output->expects($this->once())
75
            ->method('writeln')
76
            ->with($this->stringContains('Composer already installed'))
77
        ;
78
        $file = $this->tempDir.'/bin/composer.phar';
79
        Toolkit::ensureFileDir($file);
80
        touch($file);
81
        $installer = $this->getSUT();
82
        $installer->run();
83
    }
84
85
    public function testRunSuccessfully(): void
86
    {
87
        $process = $this->createMock(Process::class);
88
        $this->processor->expects($this->once())
89
            ->method('create')
90
            ->with($this->stringContains('composer.phar'))
91
            ->willReturn($process)
92
        ;
93
94
        $process->expects($this->once())
95
            ->method('run')
96
            ->will($this->returnCallback(function () {
97
                touch($this->tempDir.'/bin/composer.phar');
98
99
                return 0;
100
            }))
101
        ;
102
        $installer = $this->getSUT();
103
        $installer->run();
104
    }
105
106
    public function testRunWithFailedSignature(): void
107
    {
108
        $this->output->expects($this->once())
109
            ->method('writeln')
110
            ->with($this->stringContains('Signature Invalid'))
111
        ;
112
        $installer = $this->getSUT(array(
113
            'installer.php' => __DIR__.'/fixtures/empty.php',
114
        ));
115
        $installer->run();
116
    }
117
118
    private function getSUT($config = array(), $useDownloader = true)
119
    {
120
        $defaults = array(
121
            'installer.sig' => __DIR__.'/fixtures/installer.sig',
122
            'installer.php' => __DIR__.'/fixtures/installer.php',
123
        );
124
        $config = array_merge($defaults, $config);
125
126
        $tempDir = $this->tempDir;
127
128
        $this->config->expects($this->any())
129
            ->method('get')
130
            ->willReturnMap(array(
131
                array('dotfiles.temp_dir', $tempDir.'/temp'),
132
                array('dotfiles.bin_dir', $tempDir.'/bin'),
133
                array('composer.file_name', 'composer.phar'),
134
            ))
135
        ;
136
137
        $this->downloader->expects($this->any())
138
            ->method('run')
139
            ->will($this->returnCallback(function ($url, $target) use ($config): void {
140
                Toolkit::ensureFileDir($target);
141
                $origin = $config['installer.php'];
142
                if (false !== strpos($target, 'composer.sig')) {
143
                    $origin = $config['installer.sig'];
144
                }
145
                $fs = new Filesystem();
146
                $fs->copy($origin, $target);
147
            }))
148
        ;
149
150
        return new Installer(
151
            $this->output,
152
            $this->logger,
153
            $this->config,
154
            $this->downloader,
155
            $this->processor
156
        );
157
    }
158
}
159