Skip to content

Commit

Permalink
Merge pull request #205 from gacela-project/feature/read-autoload-dev…
Browse files Browse the repository at this point in the history
…-when-parsing-composer

Read autoload dev when parsing composer
  • Loading branch information
Chemaclass authored Oct 9, 2022
2 parents 5517cfc + 372e894 commit d823ba7
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 18 deletions.
6 changes: 3 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
coverage/
.idea/
.gacela/
.vscode/
vendor/
data/
.phpbench/
coverage/
data/
vendor/

*.cache
composer.lock
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

### Unreleased

- Read autoload-dev psr-4 namespaces for gacela make commands.

### 0.26.0
#### 2022-10-01

Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@
},
"autoload-dev": {
"psr-4": {
"GacelaData\\": "data",
"GacelaTest\\": "tests"
"GacelaData\\": "data/",
"GacelaTest\\": "tests/"
}
},
"bin": [
Expand Down
Empty file added data/.gitkeep
Empty file.
17 changes: 15 additions & 2 deletions src/Console/Domain/CommandArguments/CommandArgumentsParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,19 @@

final class CommandArgumentsParser implements CommandArgumentsParserInterface
{
/** @var array{autoload: array{psr-4?:array<string,string>}} */
/**
* @var array{
* autoload: array{psr-4?:array<string,string>},
* autoload-dev?: array{psr-4?:array<string,string>},
* }
*/
private array $composerJson;

/**
* @param array{autoload: array{psr-4?:array<string,string>}} $composerJson
* @param array{
* autoload: array{psr-4?:array<string,string>},
* autoload-dev?: array{psr-4?:array<string,string>},
* } $composerJson
*/
public function __construct(array $composerJson)
{
Expand All @@ -37,6 +45,8 @@ public function parse(string $desiredNamespace): CommandArguments
}

$psr4 = $this->composerJson['autoload']['psr-4'];
$psr4Dev = $this->composerJson['autoload-dev']['psr-4'] ?? [];

$allPsr4Combinations = $this->allPossiblePsr4Combinations($desiredNamespace);

foreach ($allPsr4Combinations as $psr4Combination) {
Expand All @@ -45,6 +55,9 @@ public function parse(string $desiredNamespace): CommandArguments
if (isset($psr4[$psr4Key])) {
return $this->foundPsr4($psr4Key, $psr4[$psr4Key], $desiredNamespace);
}
if (isset($psr4Dev[$psr4Key])) {
return $this->foundPsr4($psr4Key, $psr4Dev[$psr4Key], $desiredNamespace);
}
}

throw CommandArgumentsException::noAutoloadPsr4MatchFound($desiredNamespace, array_keys($psr4));
Expand Down
22 changes: 11 additions & 11 deletions tests/Feature/CodeGenerator/MakeModuleCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,40 +15,40 @@ final class MakeModuleCommandTest extends TestCase
{
public static function tearDownAfterClass(): void
{
DirectoryUtil::removeDir('./src/TestModule');
DirectoryUtil::removeDir('./data/TestModule');
}

public function setUp(): void
{
Gacela::bootstrap(__DIR__);
DirectoryUtil::removeDir('./src/TestModule');
DirectoryUtil::removeDir('./data/TestModule');
}

/**
* @dataProvider createModulesProvider
*/
public function test_make_module(string $fileName, string $shortName): void
{
$input = new StringInput("make:module Psr4CodeGenerator/TestModule {$shortName}");
$input = new StringInput("make:module Psr4CodeGeneratorData/TestModule {$shortName}");
$output = new BufferedOutput();

$bootstrap = new ConsoleBootstrap();
$bootstrap->setAutoExit(false);
$bootstrap->run($input, $output);

$expectedOutput = <<<OUT
> Path 'src/TestModule/{$fileName}Facade.php' created successfully
> Path 'src/TestModule/{$fileName}Factory.php' created successfully
> Path 'src/TestModule/{$fileName}Config.php' created successfully
> Path 'src/TestModule/{$fileName}DependencyProvider.php' created successfully
> Path 'data/TestModule/{$fileName}Facade.php' created successfully
> Path 'data/TestModule/{$fileName}Factory.php' created successfully
> Path 'data/TestModule/{$fileName}Config.php' created successfully
> Path 'data/TestModule/{$fileName}DependencyProvider.php' created successfully
Module 'TestModule' created successfully
OUT;
self::assertSame($expectedOutput, trim($output->fetch()));

self::assertFileExists("./src/TestModule/{$fileName}Facade.php");
self::assertFileExists("./src/TestModule/{$fileName}Factory.php");
self::assertFileExists("./src/TestModule/{$fileName}Config.php");
self::assertFileExists("./src/TestModule/{$fileName}DependencyProvider.php");
self::assertFileExists("./data/TestModule/{$fileName}Facade.php");
self::assertFileExists("./data/TestModule/{$fileName}Factory.php");
self::assertFileExists("./data/TestModule/{$fileName}Config.php");
self::assertFileExists("./data/TestModule/{$fileName}DependencyProvider.php");
}

public function createModulesProvider(): iterable
Expand Down
5 changes: 5 additions & 0 deletions tests/Feature/CodeGenerator/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,10 @@
"psr-4": {
"Psr4CodeGenerator\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Psr4CodeGeneratorData\\": "data/"
}
}
}

0 comments on commit d823ba7

Please sign in to comment.