-
-
Notifications
You must be signed in to change notification settings - Fork 196
/
Copy pathCopyFromPackageConfiguratorTest.php
180 lines (154 loc) · 7.15 KB
/
CopyFromPackageConfiguratorTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Flex\Tests\Configurator;
use Composer\Composer;
use Composer\Installer\InstallationManager;
use Composer\IO\IOInterface;
use Composer\Package\PackageInterface;
use PHPUnit\Framework\TestCase;
use Symfony\Flex\Configurator\CopyFromPackageConfigurator;
use Symfony\Flex\Lock;
use Symfony\Flex\Options;
use Symfony\Flex\Recipe;
class CopyFromPackageConfiguratorTest extends TestCase
{
private $sourceFile;
private $sourceDirectory;
private $sourceFileRelativePath;
private $targetFile;
private $targetFileRelativePath;
private $targetDirectory;
private $io;
private $recipe;
public function testNoFilesCopied()
{
if (!file_exists($this->targetDirectory)) {
mkdir($this->targetDirectory);
}
file_put_contents($this->targetFile, '');
$this->io->expects($this->exactly(1))->method('writeError')->with([' Copying files from package']);
$lock = $this->getMockBuilder(Lock::class)->disableOriginalConstructor()->getMock();
$this->createConfigurator()->configure($this->recipe, [$this->sourceFileRelativePath => $this->targetFileRelativePath], $lock);
}
public function testConfigureAndOverwriteFiles()
{
if (!file_exists($this->targetDirectory)) {
mkdir($this->targetDirectory);
}
if (!file_exists($this->sourceDirectory)) {
mkdir($this->sourceDirectory);
}
file_put_contents($this->sourceFile, 'somecontent');
file_put_contents($this->targetFile, '-');
$lock = $this->getMockBuilder(Lock::class)->disableOriginalConstructor()->getMock();
$this->io->expects($this->at(0))->method('writeError')->with([' Copying files from package']);
$this->io->expects($this->at(2))->method('writeError')->with([' Created <fg=green>"./public/file"</>']);
$this->io->method('askConfirmation')->with('File "build/public/file" has uncommitted changes, overwrite? [Y/n] ')->willReturn(true);
$this->assertFileExists($this->targetFile);
$this->createConfigurator()->configure(
$this->recipe,
[$this->sourceFileRelativePath => $this->targetFileRelativePath],
$lock,
['force' => true]
);
$this->assertFileExists($this->targetFile);
$this->assertFileEquals($this->sourceFile, $this->targetFile);
}
public function testSourceFileNotExist()
{
$this->io->expects($this->once())->method('writeError')->with([' Copying files from package']);
$this->expectException(\LogicException::class);
$this->expectExceptionMessage(\sprintf('File "%s" does not exist!', $this->sourceFile));
$lock = $this->getMockBuilder(Lock::class)->disableOriginalConstructor()->getMock();
$this->createConfigurator()->configure($this->recipe, [$this->sourceFileRelativePath => $this->targetFileRelativePath], $lock);
}
public function testConfigure()
{
if (!is_dir($this->sourceDirectory)) {
mkdir($this->sourceDirectory);
}
if (!file_exists($this->sourceFile)) {
file_put_contents($this->sourceFile, '');
}
$this->io->expects($this->at(0))->method('writeError')->with([' Copying files from package']);
$this->io->expects($this->at(1))->method('writeError')->with([' Created <fg=green>"./public/"</>']);
$this->io->expects($this->at(2))->method('writeError')->with([' Created <fg=green>"./public/file"</>']);
$this->assertFileDoesNotExist($this->targetFile);
$lock = $this->getMockBuilder(Lock::class)->disableOriginalConstructor()->getMock();
$this->createConfigurator()->configure($this->recipe, [$this->sourceFileRelativePath => $this->targetFileRelativePath], $lock);
$this->assertFileExists($this->targetFile);
}
public function testUnconfigure()
{
$this->io->expects($this->at(0))->method('writeError')->with([' Removing files from package']);
$this->io->expects($this->at(1))->method('writeError')->with([' Removed <fg=green>"./public/file"</>']);
if (!file_exists($this->targetDirectory)) {
mkdir($this->targetDirectory);
}
file_put_contents($this->targetFile, '');
$this->assertFileExists($this->targetFile);
$lock = $this->getMockBuilder(Lock::class)->disableOriginalConstructor()->getMock();
$this->createConfigurator()->unconfigure(
$this->recipe,
[$this->sourceFileRelativePath => $this->targetFileRelativePath, 'missingdir/' => ''],
$lock
);
$this->assertFileDoesNotExist($this->targetFile);
}
public function testNoFilesRemoved()
{
$this->assertFileDoesNotExist($this->targetFile);
$this->io->expects($this->exactly(1))->method('writeError')->with([' Removing files from package']);
$lock = $this->getMockBuilder(Lock::class)->disableOriginalConstructor()->getMock();
$this->createConfigurator()->unconfigure($this->recipe, [$this->sourceFileRelativePath => $this->targetFileRelativePath], $lock);
}
protected function setUp(): void
{
parent::setUp();
$this->sourceDirectory = FLEX_TEST_DIR.'/package';
$this->sourceFileRelativePath = 'package/file';
$this->sourceFile = $this->sourceDirectory.'/file';
$this->targetDirectory = FLEX_TEST_DIR.'/public';
$this->targetFileRelativePath = 'public/file';
$this->targetFile = $this->targetDirectory.'/file';
$this->io = $this->getMockBuilder(IOInterface::class)->getMock();
$package = $this->getMockBuilder(PackageInterface::class)->getMock();
$this->recipe = $this->getMockBuilder(Recipe::class)->disableOriginalConstructor()->getMock();
$this->recipe->expects($this->exactly(1))->method('getPackage')->willReturn($package);
$installationManager = $this->getMockBuilder(InstallationManager::class)->disableOriginalConstructor()->getMock();
$installationManager->expects($this->exactly(1))
->method('getInstallPath')
->with($package)
->willReturn(FLEX_TEST_DIR)
;
$this->composer = $this->getMockBuilder(Composer::class)->getMock();
$this->composer->expects($this->exactly(1))
->method('getInstallationManager')
->willReturn($installationManager)
;
$this->cleanUpTargetFiles();
}
protected function tearDown(): void
{
parent::tearDown();
@unlink($this->sourceFile);
$this->cleanUpTargetFiles();
}
private function createConfigurator(): CopyFromPackageConfigurator
{
return new CopyFromPackageConfigurator($this->composer, $this->io, new Options(['root-dir' => FLEX_TEST_DIR], $this->io));
}
private function cleanUpTargetFiles()
{
@unlink($this->targetFile);
@rmdir(FLEX_TEST_DIR.'/package');
@rmdir(FLEX_TEST_DIR.'/public');
}
}