|
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\PHPBrew\Tests; |
|
15
|
|
|
|
|
16
|
|
|
use Dotfiles\Core\Config\Config; |
|
17
|
|
|
use Dotfiles\Core\Tests\BaseTestCase; |
|
18
|
|
|
use Dotfiles\Core\Util\Downloader; |
|
19
|
|
|
use Dotfiles\Plugins\PHPBrew\Installer; |
|
20
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
|
21
|
|
|
use Psr\Log\LoggerInterface; |
|
22
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Class InstallerTest. |
|
26
|
|
|
* |
|
27
|
|
|
* @covers \Dotfiles\Plugins\PHPBrew\Installer |
|
28
|
|
|
*/ |
|
29
|
|
|
class InstallerTest extends BaseTestCase |
|
30
|
|
|
{ |
|
31
|
|
|
/** |
|
32
|
|
|
* @var MockObject |
|
33
|
|
|
*/ |
|
34
|
|
|
private $config; |
|
35
|
|
|
/** |
|
36
|
|
|
* @var MockObject |
|
37
|
|
|
*/ |
|
38
|
|
|
private $downloader; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var MockObject |
|
42
|
|
|
*/ |
|
43
|
|
|
private $logger; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var MockObject |
|
47
|
|
|
*/ |
|
48
|
|
|
private $output; |
|
49
|
|
|
|
|
50
|
|
|
private $tempDir; |
|
51
|
|
|
|
|
52
|
|
|
public function setUp(): void |
|
53
|
|
|
{ |
|
54
|
|
|
$this->config = $this->createMock(Config::class); |
|
55
|
|
|
$this->downloader = $this->createMock(Downloader::class); |
|
56
|
|
|
$this->logger = $this->createMock(LoggerInterface::class); |
|
57
|
|
|
$this->output = $this->createMock(OutputInterface::class); |
|
58
|
|
|
$this->tempDir = sys_get_temp_dir().'/dotfiles'; |
|
59
|
|
|
static::cleanupTempDir(); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function testRun(): void |
|
63
|
|
|
{ |
|
64
|
|
|
$installer = $this->getSUT(); |
|
65
|
|
|
$tempDir = $this->tempDir; |
|
66
|
|
|
$this->downloader->expects($this->once()) |
|
67
|
|
|
->method('run') |
|
68
|
|
|
->with(Installer::DOWNLOAD_URL, $tempDir.'/temp/phpbrew') |
|
69
|
|
|
->will($this->returnCallback(function () use ($tempDir): void { |
|
70
|
|
|
touch($tempDir.'/temp/phpbrew'); |
|
71
|
|
|
})) |
|
72
|
|
|
; |
|
73
|
|
|
$installer->run(); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function testRunOnAlreadyInstalled(): void |
|
77
|
|
|
{ |
|
78
|
|
|
touch($this->tempDir.'/bin/phpbrew'); |
|
79
|
|
|
$this->output->expects($this->once()) |
|
80
|
|
|
->method('writeln') |
|
81
|
|
|
->with($this->stringContains('already installed')) |
|
82
|
|
|
; |
|
83
|
|
|
$installer = $this->getSUT(); |
|
84
|
|
|
$installer->run(); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public function testRunWhenFileDownloaded(): void |
|
88
|
|
|
{ |
|
89
|
|
|
touch($file = $this->tempDir.'/temp/phpbrew'); |
|
90
|
|
|
$this->logger->expects($this->once()) |
|
91
|
|
|
->method('debug') |
|
92
|
|
|
->with($this->stringContains('file already downloaded')) |
|
93
|
|
|
; |
|
94
|
|
|
$installer = $this->getSUT(); |
|
95
|
|
|
$installer->run(); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @param array $config |
|
100
|
|
|
* |
|
101
|
|
|
* @return Installer |
|
102
|
|
|
* |
|
103
|
|
|
* @throws \ReflectionException |
|
104
|
|
|
*/ |
|
105
|
|
|
private function getSUT( |
|
106
|
|
|
$config = array() |
|
107
|
|
|
) { |
|
108
|
|
|
$tempDir = $this->tempDir; |
|
109
|
|
|
$this->config->expects($this->any()) |
|
110
|
|
|
->method('get') |
|
111
|
|
|
->willReturnMap(array( |
|
112
|
|
|
array('dotfiles.dry_run', false), |
|
113
|
|
|
array('dotfiles.install_dir', $tempDir.'/install'), |
|
114
|
|
|
array('dotfiles.temp_dir', $tempDir.'/temp'), |
|
115
|
|
|
array('dotfiles.bin_dir', $tempDir.'/bin'), |
|
116
|
|
|
)) |
|
117
|
|
|
; |
|
118
|
|
|
|
|
119
|
|
|
return new Installer( |
|
120
|
|
|
$this->config, |
|
121
|
|
|
$this->downloader, |
|
122
|
|
|
$this->logger, |
|
123
|
|
|
$this->output |
|
124
|
|
|
); |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|