Skip to content

Commit 5458486

Browse files
Added viriable replacement to DeleteFilesAndFolders so creating and deleting files and folders works the same way
1 parent 3c172a1 commit 5458486

5 files changed

Lines changed: 29 additions & 19 deletions

File tree

README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,7 @@ return call_user_func(static function() {
6969
$variables
7070
),
7171
new CreateFolders($variableReplacer, $filesystem, [__DIR__ . '/../src', __DIR__ . '/../test'], $variables),
72-
new DeleteFilesAndFolders($filesystem, [
73-
__DIR__
74-
]),
72+
new DeleteFilesAndFolders($variableReplacer, $filesystem, [__DIR__], $variables),
7573
)
7674
))->run();
7775
});

src/steps/CreateFolders.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@ public function __construct(public iVariableReplacer $variableReplacer, public F
4343
* @throws IOException
4444
*/
4545
public function run() : void {
46-
foreach($this->folders as $folder) {
47-
$this->filesystem->mkdir($this->variableReplacer->replace($folder, $this->variables));
48-
}
46+
$this->filesystem->mkdir(array_map(fn(string $folder) => $this->variableReplacer->replace($folder, $this->variables), $this->folders));
4947
}
5048
}

src/steps/DeleteFilesAndFolders.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
namespace de\codenamephp\installer\steps;
2020

21+
use de\codenamephp\installer\templateCopy\variableReplacer\iVariableReplacer;
2122
use Symfony\Component\Filesystem\Exception\IOException;
2223
use Symfony\Component\Filesystem\Filesystem;
2324

@@ -27,17 +28,19 @@
2728
final class DeleteFilesAndFolders implements iStep {
2829

2930
/**
30-
* @param Filesystem $filesystem
31-
* @param string[] $filesAndFoldersToDelete
31+
* @param iVariableReplacer $variableReplacer Used to replace variables in the paths of the files and folders
32+
* @param Filesystem $filesystem Used to delete the files and folders
33+
* @param array<string> $filesAndFoldersToDelete The absolute paths of the files and folders to be deleted
34+
* @param array<string, string> $variables Used in variable replacement in the folder paths of the files and folders
3235
*/
33-
public function __construct(public Filesystem $filesystem, public array $filesAndFoldersToDelete) { }
36+
public function __construct(public iVariableReplacer $variableReplacer, public Filesystem $filesystem, public array $filesAndFoldersToDelete, public array $variables) { }
3437

3538
/**
3639
*
3740
* @inheritDoc
3841
* @throws IOException
3942
*/
4043
public function run() : void {
41-
$this->filesystem->remove($this->filesAndFoldersToDelete);
44+
$this->filesystem->remove(array_map(fn(string $path) => $this->variableReplacer->replace($path, $this->variables), $this->filesAndFoldersToDelete));
4245
}
4346
}

test/steps/CreateFoldersTest.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,7 @@ public function testRun() : void {
5050
->willReturnOnConsecutiveCalls('replaced1', 'replaced2', 'replaced3');
5151

5252
$this->sut->filesystem = $this->createMock(Filesystem::class);
53-
$this->sut->filesystem
54-
->expects(self::exactly(3))
55-
->method('mkdir')
56-
->withConsecutive(['replaced1'], ['replaced2'], ['replaced3']);
53+
$this->sut->filesystem->expects(self::once())->method('mkdir')->with(['replaced1', 'replaced2', 'replaced3']);
5754

5855
$this->sut->run();
5956
}

test/steps/DeleteFilesAndFoldersTest.php

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
namespace de\codenamephp\installer\test\steps;
2020

2121
use de\codenamephp\installer\steps\DeleteFilesAndFolders;
22+
use de\codenamephp\installer\templateCopy\variableReplacer\iVariableReplacer;
2223
use PHPUnit\Framework\TestCase;
2324
use Symfony\Component\Filesystem\Filesystem;
2425

@@ -27,17 +28,30 @@ class DeleteFilesAndFoldersTest extends TestCase {
2728
private DeleteFilesAndFolders $sut;
2829

2930
protected function setUp() : void {
31+
$variableReplacer = $this->createMock(iVariableReplacer::class);
3032
$filesystem = $this->createMock(Filesystem::class);
3133

32-
$this->sut = new DeleteFilesAndFolders($filesystem, []);
34+
$this->sut = new DeleteFilesAndFolders($variableReplacer, $filesystem, [], []);
3335
}
3436

3537
public function testRun() : void {
3638
$this->sut->filesAndFoldersToDelete = ['some', 'files'];
37-
38-
$filesystem = $this->createMock(Filesystem::class);
39-
$filesystem->expects(self::once())->method('remove')->with($this->sut->filesAndFoldersToDelete);
40-
$this->sut->filesystem = $filesystem;
39+
$this->sut->variables = ['some', 'vars'];
40+
$this->sut->filesAndFoldersToDelete = ['folder1', 'file1', 'file2'];
41+
42+
$this->sut->variableReplacer = $this->createMock(iVariableReplacer::class);
43+
$this->sut->variableReplacer
44+
->expects(self::exactly(3))
45+
->method('replace')
46+
->withConsecutive(
47+
[$this->sut->filesAndFoldersToDelete[0], $this->sut->variables],
48+
[$this->sut->filesAndFoldersToDelete[1], $this->sut->variables],
49+
[$this->sut->filesAndFoldersToDelete[2], $this->sut->variables],
50+
)
51+
->willReturnOnConsecutiveCalls('replaced1', 'replaced2', 'replaced3');
52+
53+
$this->sut->filesystem = $this->createMock(Filesystem::class);
54+
$this->sut->filesystem->expects(self::once())->method('remove')->with(['replaced1', 'replaced2', 'replaced3']);
4155

4256
$this->sut->run();
4357
}

0 commit comments

Comments
 (0)