-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34 from gacela-project/feature/add-tests
Add tests for CodeGenerator Domain layer
- Loading branch information
Showing
7 changed files
with
212 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Gacela\CodeGenerator\Infrastructure; | ||
|
||
use RuntimeException; | ||
|
||
/** | ||
* @codeCoverageIgnore | ||
*/ | ||
final class FileContentIo implements FileContentIoInterface | ||
{ | ||
public function mkdir(string $directory): void | ||
{ | ||
if (is_dir($directory)) { | ||
return; | ||
} | ||
if (!mkdir($directory) && !is_dir($directory)) { | ||
throw new RuntimeException(sprintf('Directory "%s" was not created', $directory)); | ||
} | ||
} | ||
|
||
public function filePutContents(string $path, string $fileContent): void | ||
{ | ||
file_put_contents($path, $fileContent); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/CodeGenerator/Infrastructure/FileContentIoInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Gacela\CodeGenerator\Infrastructure; | ||
|
||
interface FileContentIoInterface | ||
{ | ||
public function mkdir(string $directory): void; | ||
|
||
public function filePutContents(string $path, string $fileContent): void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
132 changes: 132 additions & 0 deletions
132
tests/Unit/CodeGenerator/Domain/FileContentGeneratorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace GacelaTest\Unit\CodeGenerator\Domain; | ||
|
||
use Gacela\CodeGenerator\Domain\CommandArguments; | ||
use Gacela\CodeGenerator\Domain\FileContentGenerator; | ||
use Gacela\CodeGenerator\Domain\FilenameSanitizer; | ||
use Gacela\CodeGenerator\Infrastructure\FileContentIoInterface; | ||
use Gacela\CodeGenerator\Infrastructure\Template\CodeTemplateInterface; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class FileContentGeneratorTest extends TestCase | ||
{ | ||
public function test_error_when_unknown_template(): void | ||
{ | ||
$codeTemplate = $this->createStub(CodeTemplateInterface::class); | ||
$fileContentIo = $this->createStub(FileContentIoInterface::class); | ||
$generator = new FileContentGenerator($codeTemplate, $fileContentIo); | ||
|
||
$this->expectExceptionMessage('Unknown template for "unknown_template"?'); | ||
$generator->generate( | ||
new CommandArguments('Namespace', 'Dir'), | ||
'unknown_template' | ||
); | ||
} | ||
|
||
public function test_facade_maker_template(): void | ||
{ | ||
$codeTemplate = $this->createMock(CodeTemplateInterface::class); | ||
$codeTemplate->expects(self::once()) | ||
->method('getFacadeMakerTemplate') | ||
->willReturn('template-result'); | ||
|
||
$fileContentIo = $this->createMock(FileContentIoInterface::class); | ||
$fileContentIo->expects(self::once()) | ||
->method('mkdir') | ||
->with('Dir'); | ||
|
||
$fileContentIo->expects(self::once()) | ||
->method('filePutContents') | ||
->with('Dir/DirFacade.php', 'template-result'); | ||
|
||
$generator = new FileContentGenerator($codeTemplate, $fileContentIo); | ||
|
||
$actualPath = $generator->generate( | ||
new CommandArguments('Namespace', 'Dir'), | ||
FilenameSanitizer::FACADE | ||
); | ||
|
||
self::assertSame('Dir/DirFacade.php', $actualPath); | ||
} | ||
|
||
public function test_factory_maker_template(): void | ||
{ | ||
$codeTemplate = $this->createMock(CodeTemplateInterface::class); | ||
$codeTemplate->expects(self::once()) | ||
->method('getFactoryMakerTemplate') | ||
->willReturn('template-result'); | ||
|
||
$fileContentIo = $this->createMock(FileContentIoInterface::class); | ||
$fileContentIo->expects(self::once()) | ||
->method('mkdir') | ||
->with('Dir'); | ||
|
||
$fileContentIo->expects(self::once()) | ||
->method('filePutContents') | ||
->with('Dir/DirFactory.php', 'template-result'); | ||
|
||
$generator = new FileContentGenerator($codeTemplate, $fileContentIo); | ||
|
||
$actualPath = $generator->generate( | ||
new CommandArguments('Namespace', 'Dir'), | ||
FilenameSanitizer::FACTORY | ||
); | ||
|
||
self::assertSame('Dir/DirFactory.php', $actualPath); | ||
} | ||
|
||
public function test_config_maker_template(): void | ||
{ | ||
$codeTemplate = $this->createMock(CodeTemplateInterface::class); | ||
$codeTemplate->expects(self::once()) | ||
->method('getConfigMakerTemplate') | ||
->willReturn('template-result'); | ||
|
||
$fileContentIo = $this->createMock(FileContentIoInterface::class); | ||
$fileContentIo->expects(self::once()) | ||
->method('mkdir') | ||
->with('Dir'); | ||
|
||
$fileContentIo->expects(self::once()) | ||
->method('filePutContents') | ||
->with('Dir/DirConfig.php', 'template-result'); | ||
|
||
$generator = new FileContentGenerator($codeTemplate, $fileContentIo); | ||
|
||
$actualPath = $generator->generate( | ||
new CommandArguments('Namespace', 'Dir'), | ||
FilenameSanitizer::CONFIG | ||
); | ||
|
||
self::assertSame('Dir/DirConfig.php', $actualPath); | ||
} | ||
|
||
public function test_dependency_provider_maker_template(): void | ||
{ | ||
$codeTemplate = $this->createMock(CodeTemplateInterface::class); | ||
$codeTemplate->expects(self::once()) | ||
->method('getDependencyProviderMakerTemplate') | ||
->willReturn('template-result'); | ||
|
||
$fileContentIo = $this->createMock(FileContentIoInterface::class); | ||
$fileContentIo->expects(self::once()) | ||
->method('mkdir') | ||
->with('Dir'); | ||
|
||
$fileContentIo->expects(self::once()) | ||
->method('filePutContents') | ||
->with('Dir/DirDependencyProvider.php', 'template-result'); | ||
|
||
$generator = new FileContentGenerator($codeTemplate, $fileContentIo); | ||
|
||
$actualPath = $generator->generate( | ||
new CommandArguments('Namespace', 'Dir'), | ||
FilenameSanitizer::DEPENDENCY_PROVIDER | ||
); | ||
|
||
self::assertSame('Dir/DirDependencyProvider.php', $actualPath); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters