diff --git a/src/Generators/FileGenerator.php b/src/Generators/FileGenerator.php index 8149dd8..4a20abd 100644 --- a/src/Generators/FileGenerator.php +++ b/src/Generators/FileGenerator.php @@ -30,7 +30,7 @@ class FileGenerator extends Generator /** * @var bool */ - private bool $overwriteFile; + private bool $overwriteFile = false; /** * The constructor. diff --git a/tests/Unit/FileGeneratorTest.php b/tests/Unit/FileGeneratorTest.php new file mode 100644 index 0000000..faa51ba --- /dev/null +++ b/tests/Unit/FileGeneratorTest.php @@ -0,0 +1,76 @@ +shouldReceive('exists')->once()->with($path)->andReturn(false); + $filesystem->shouldReceive('put')->once()->with($path, $contents)->andReturn(13); + + $generator = new FileGenerator($path, $contents, $filesystem); + + $this->assertEquals(13, $generator->generate()); + } + + public function test_it_generates_file_when_exists_and_overwrite_is_true() + { + $path = '/fake/path/file.txt'; + $contents = 'fake contents'; + + $filesystem = Mockery::mock(Filesystem::class); + $filesystem->shouldReceive('exists')->once()->with($path)->andReturn(true); + $filesystem->shouldReceive('put')->once()->with($path, $contents)->andReturn(13); + + $generator = new FileGenerator($path, $contents, $filesystem); + $generator->withFileOverwrite(true); + + $this->assertEquals(13, $generator->generate()); + } + + public function test_it_throws_exception_when_file_exists_and_overwrite_is_false() + { + $path = '/fake/path/file.txt'; + $contents = 'fake contents'; + + $filesystem = Mockery::mock(Filesystem::class); + $filesystem->shouldReceive('exists')->once()->with($path)->andReturn(true); + $filesystem->shouldReceive('put')->never(); + + $generator = new FileGenerator($path, $contents, $filesystem); + $generator->withFileOverwrite(false); + + $this->expectException(FileAlreadyExistException::class); + $this->expectExceptionMessage('File already exists!'); + + $generator->generate(); + } + + public function test_it_throws_exception_when_file_exists_and_overwrite_is_not_set() + { + $path = '/fake/path/file.txt'; + $contents = 'fake contents'; + + $filesystem = Mockery::mock(Filesystem::class); + $filesystem->shouldReceive('exists')->once()->with($path)->andReturn(true); + $filesystem->shouldReceive('put')->never(); + + $generator = new FileGenerator($path, $contents, $filesystem); + + $this->expectException(FileAlreadyExistException::class); + $this->expectExceptionMessage('File already exists!'); + + $generator->generate(); + } +}