Skip to content

Commit

Permalink
Merge pull request #34 from gacela-project/feature/add-tests
Browse files Browse the repository at this point in the history
Add tests for CodeGenerator Domain layer
  • Loading branch information
JesusValeraDev authored Jul 4, 2021
2 parents ae92905 + 1f9940b commit cac7840
Show file tree
Hide file tree
Showing 7 changed files with 212 additions and 18 deletions.
12 changes: 11 additions & 1 deletion src/CodeGenerator/CodeGeneratorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use Gacela\CodeGenerator\Domain\FilenameSanitizer;
use Gacela\CodeGenerator\Infrastructure\Command\MakeFileCommand;
use Gacela\CodeGenerator\Infrastructure\Command\MakeModuleCommand;
use Gacela\CodeGenerator\Infrastructure\FileContentIo;
use Gacela\CodeGenerator\Infrastructure\FileContentIoInterface;
use Gacela\Framework\AbstractFactory;

/**
Expand Down Expand Up @@ -47,6 +49,14 @@ private function createFilenameSanitizer(): FilenameSanitizer

private function createFileContentGenerator(): FileContentGenerator
{
return new FileContentGenerator($this->getConfig());
return new FileContentGenerator(
$this->getConfig(),
$this->createFileContentIo()
);
}

private function createFileContentIo(): FileContentIoInterface
{
return new FileContentIo();
}
}
25 changes: 10 additions & 15 deletions src/CodeGenerator/Domain/FileContentGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,29 @@

namespace Gacela\CodeGenerator\Domain;

use Gacela\CodeGenerator\Infrastructure\FileContentIoInterface;
use Gacela\CodeGenerator\Infrastructure\Template\CodeTemplateInterface;
use RuntimeException;

final class FileContentGenerator
{
private CodeTemplateInterface $codeTemplate;
private FileContentIoInterface $fileContentIo;

public function __construct(CodeTemplateInterface $codeTemplate)
{
public function __construct(
CodeTemplateInterface $codeTemplate,
FileContentIoInterface $fileContentIo
) {
$this->codeTemplate = $codeTemplate;
$this->fileContentIo = $fileContentIo;
}

/**
* @return string path result where the file was generated
*/
public function generate(CommandArguments $commandArguments, string $filename, bool $withShortName): string
public function generate(CommandArguments $commandArguments, string $filename, bool $withShortName = false): string
{
$this->mkdir($commandArguments->directory());
$this->fileContentIo->mkdir($commandArguments->directory());

$moduleName = $withShortName ? '' : $commandArguments->basename();
$className = $moduleName . $filename;
Expand All @@ -33,21 +38,11 @@ public function generate(CommandArguments $commandArguments, string $filename, b
$template = $this->findTemplate($filename);
$fileContent = str_replace($search, $replace, $template);

file_put_contents($path, $fileContent);
$this->fileContentIo->filePutContents($path, $fileContent);

return $path;
}

private 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));
}
}

private function findTemplate(string $filename): string
{
switch ($filename) {
Expand Down
28 changes: 28 additions & 0 deletions src/CodeGenerator/Infrastructure/FileContentIo.php
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 src/CodeGenerator/Infrastructure/FileContentIoInterface.php
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;
}
13 changes: 11 additions & 2 deletions tests/Unit/CodeGenerator/Domain/CommandArgumentsParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ public function test_parse_multi_level_from_target_directory(): void
self::assertSame('src/TestModule/TestSubModule', $args->directory());
}


private function exampleOneLevelComposerJson(): array
{
$composerJson = <<<'JSON'
Expand Down Expand Up @@ -102,6 +101,16 @@ private function exampleMultiLevelComposerJson(): array
}
}
JSON;
return (array)json_decode($composerJson, true);
return json_decode($composerJson, true);
}

public function test_no_autoload_psr4_match_found(): void
{
$this->expectExceptionObject(
CommandArgumentsException::noAutoloadPsr4MatchFound('Unknown/Module')
);

$parser = new CommandArgumentsParser($this->exampleOneLevelComposerJson());
$parser->parse('Unknown/Module');
}
}
132 changes: 132 additions & 0 deletions tests/Unit/CodeGenerator/Domain/FileContentGeneratorTest.php
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);
}
}
8 changes: 8 additions & 0 deletions tests/Unit/CodeGenerator/Domain/FilenameSanitizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ protected function setUp(): void
$this->filenameSanitizer = new FilenameSanitizer();
}

public function test_expected_filenames(): void
{
self::assertSame(
'Facade, Factory, Config, DependencyProvider',
FilenameSanitizer::expectedFilenames()
);
}

public function test_facade_or_factory_problem(): void
{
$this->expectExceptionMessage('When using "fac", which filename do you mean [Facade or Factory]?');
Expand Down

0 comments on commit cac7840

Please sign in to comment.