Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/Configurator/CopyFromPackageConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ private function removeFiles(array $manifest, string $from, string $to)

private function copyDir(string $source, string $target, array $options)
{
$overwrite = $options['force'] ?? false;

if (!is_dir($target)) {
mkdir($target, 0777, true);
}
Expand All @@ -85,7 +87,7 @@ private function copyDir(string $source, string $target, array $options)
mkdir($targetPath);
$this->write(sprintf(' Created <fg=green>"%s"</>', $this->path->relativize($targetPath)));
}
} elseif (!file_exists($targetPath)) {
} elseif ($overwrite || !file_exists($targetPath)) {
$this->copyFile($item, $targetPath, $options);
}
}
Expand Down
47 changes: 46 additions & 1 deletion tests/Configurator/CopyDirectoryFromPackageConfiguratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,50 @@ public function testConfigureDirectory()
}
}

/**
* @dataProvider providerTestConfigureDirectoryWithExistingFiles
*/
public function testConfigureDirectoryWithExistingFiles(bool $force, string $sourceFileContent, string $existingTargetFileContent, string $expectedFinalTargetFileContent)
{
if (!is_dir($this->sourceDirectory)) {
mkdir($this->sourceDirectory, 0777, true);
}
foreach ($this->sourceFiles as $sourceFile) {
if (!file_exists($sourceFile)) {
file_put_contents($sourceFile, $sourceFileContent);
}
}

if (!is_dir($this->targetDirectory)) {
mkdir($this->targetDirectory, 0777, true);
}

foreach ($this->targetFiles as $targetFile) {
file_put_contents($targetFile, $existingTargetFileContent);
}

$this->createConfigurator()->configure(
$this->recipe,
[$this->sourceFileRelativePath => $this->targetFileRelativePath],
$this->getMockBuilder(Lock::class)->disableOriginalConstructor()->getMock(),
['force' => $force]
);

foreach ($this->targetFiles as $targetFile) {
$this->assertFileExists($targetFile);
$content = file_get_contents($targetFile);
$this->assertEquals($expectedFinalTargetFileContent, $content);
}
}

public function providerTestConfigureDirectoryWithExistingFiles(): array
{
return [
[true, 'NEW_CONTENT', 'OLD_CONTENT', 'NEW_CONTENT'],
[false, 'NEW_CONTENT', 'OLD_CONTENT', 'OLD_CONTENT'],
];
}

protected function setUp(): void
{
parent::setUp();
Expand All @@ -74,6 +118,7 @@ protected function setUp(): void
];

$this->io = $this->getMockBuilder(IOInterface::class)->getMock();
$this->io->method('askConfirmation')->willReturn(true);

$package = $this->getMockBuilder(PackageInterface::class)->getMock();
$this->recipe = $this->getMockBuilder(Recipe::class)->disableOriginalConstructor()->getMock();
Expand Down Expand Up @@ -106,7 +151,7 @@ protected function tearDown(): void

private function createConfigurator(): CopyFromPackageConfigurator
{
return new CopyFromPackageConfigurator($this->composer, $this->io, new Options(['root-dir' => FLEX_TEST_DIR]));
return new CopyFromPackageConfigurator($this->composer, $this->io, new Options(['root-dir' => FLEX_TEST_DIR], $this->io));
}

private function cleanUpTargetFiles()
Expand Down