Skip to content

Commit 6cc8587

Browse files
committed
use dynamic namespace resolution
1 parent c3b4bf9 commit 6cc8587

File tree

4 files changed

+20
-18
lines changed

4 files changed

+20
-18
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"illuminate/container": "^12.19",
1111
"nette/utils": "^4.0",
1212
"symfony/console": "^6.4",
13+
"symfony/dependency-injection": "^6.4",
1314
"symfony/finder": "^7.2",
1415
"webmozart/assert": "^1.11"
1516
},

src/Templating/TemplateDecorator.php

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,27 +39,28 @@ private function resolveKernelClass(): string
3939

4040
private function adjustNamespace(string $templateContents, string $smokeTestsDirectory): string
4141
{
42-
$composerJsonFilePath = getcwd() . '/composer.json';
43-
if (file_exists($composerJsonFilePath)) {
44-
$projectComposerJson = Json::decode(FileSystem::read($composerJsonFilePath));
42+
$projectTestsNamespace = $this->resolveProjectTestsNamespace();
43+
$smokeTestNamespace = $projectTestsNamespace . '\\Unit\\Smoke';
4544

46-
$requireDevPsr4 = $projectComposerJson['require-dev']['psr-4'] ?? null;
45+
return str_replace('__SMOKE_TEST_NAMESPACE__', $smokeTestNamespace, $templateContents);
46+
}
4747

48-
dump($requireDevPsr4);
49-
die;
50-
}
48+
private function resolveProjectTestsNamespace(): string
49+
{
50+
$composerJsonFilePath = getcwd() . '/composer.json';
51+
if (file_exists($composerJsonFilePath)) {
52+
$projectComposerJson = Json::decode(FileSystem::read($composerJsonFilePath), true);
5153

52-
$projectComposerJson = json_decode(file_get_contents($composerJsonFilePath), true);
54+
$autoloadDevPsr4 = $projectComposerJson['autoload-dev']['psr-4'] ?? [];
55+
foreach ($autoloadDevPsr4 as $namespace => $directory) {
56+
if ($directory === 'tests') {
57+
return rtrim($namespace, '\\');
58+
}
59+
}
5360

54-
if ($smokeTestsDirectory === 'tests/Unit/Smoke') {
55-
// default one, nothing to adjust
56-
return $templateContents;
5761
}
5862

59-
// @todo check with composer.json "psr-4" in require-dev
60-
$namespace = str_replace('/', '\\', $smokeTestsDirectory);
61-
$namespace = lcfirst($namespace);
62-
63-
return str_replace('namespace App\Tests\Unit\Smoke', 'namespace App\\' . $namespace, $templateContents);
63+
// fallback to default
64+
return 'App\Tests';
6465
}
6566
}

templates/Symfony/AbstractContainerTestCase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace App\Tests\Unit\Smoke;
5+
namespace __SMOKE_TEST_NAMESPACE__;
66

77
use PHPUnit\Framework\TestCase;
88
use Symfony\Component\DependencyInjection\Container;

templates/Symfony/ServiceContainerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace App\Tests\Unit\Smoke;
5+
namespace __SMOKE_TEST_NAMESPACE__;
66

77
use Throwable;
88

0 commit comments

Comments
 (0)