Skip to content

Commit c641368

Browse files
committed
cs
1 parent f088a1c commit c641368

File tree

4 files changed

+62
-24
lines changed

4 files changed

+62
-24
lines changed

src/Command/GenerateCommand.php

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Nette\Utils\FileSystem;
88
use Rector\SmokeTestgen\FIleSystem\TestsDirectoryResolver;
9+
use Rector\SmokeTestgen\Templating\TemplateDecorator;
910
use Rector\SmokeTestgen\TestTemplateResolver;
1011
use Rector\SmokeTestgen\Utils\JsonFileLoader;
1112
use Rector\SmokeTestgen\Utils\TestPathResolver;
@@ -19,7 +20,8 @@ final class GenerateCommand extends Command
1920
{
2021
public function __construct(
2122
private readonly TestsDirectoryResolver $testsDirectoryResolver,
22-
private readonly TestTemplateResolver $testTemplateResolver
23+
private readonly TestTemplateResolver $testTemplateResolver,
24+
private readonly TemplateDecorator $templateDecorator
2325
) {
2426
parent::__construct();
2527
}
@@ -46,12 +48,13 @@ protected function execute(InputInterface $input, OutputInterface $output): int
4648
$symfonyStyle->warning(
4749
'No test templates found for the required packages. Make sure you project uses Composer to manage version and has Symfony/Doctrine packages listed in "require" section'
4850
);
51+
4952
return self::FAILURE;
5053
}
5154

5255
$symfonyStyle->newLine();
5356
$symfonyStyle->writeln(sprintf(
54-
'Found <fg=yellow>%d smoke test%s</>, that might come handy',
57+
'Found <fg=yellow>%d smoke test%s</> that might come handy',
5558
count($testByPackageSubscribers),
5659
count($testByPackageSubscribers) > 1 ? 's' : ''
5760
));
@@ -67,7 +70,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
6770
}
6871

6972
$templateContents = FileSystem::read($testByPackageSubscriber->getTemplateFilePath());
70-
$templateContents = $this->addjustTestFileNamespace($templateContents, $smokeTestsDirectory);
73+
$templateContents = $this->templateDecorator->decorate($templateContents, $smokeTestsDirectory);
7174

7275
FileSystem::write($projectTestFilePath, $templateContents);
7376

@@ -85,6 +88,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
8588
$projectTestCaseFilePath = $smokeTestsDirectory . '/AbstractContainerTestCase.php';
8689
if (! file_exists($projectTestCaseFilePath)) {
8790
$templateContents = FileSystem::read(__DIR__ . '/../../templates/Symfony/AbstractContainerTestCase.php');
91+
$templateContents = $this->templateDecorator->decorate($templateContents, $smokeTestsDirectory);
92+
8893
FileSystem::write($projectTestCaseFilePath, $templateContents);
8994
}
9095

@@ -116,17 +121,4 @@ private function resolveProjectRequiredPackageNames(string $projectDirectory): a
116121

117122
return $packageNames;
118123
}
119-
120-
private function addjustTestFileNamespace(string $templateContents, string $smokeTestsDirectory): string
121-
{
122-
if ($smokeTestsDirectory === 'tests/Unit/Smoke') {
123-
// default one, nothing to adjust
124-
return $templateContents;
125-
}
126-
127-
$namespace = str_replace('/', '\\', $smokeTestsDirectory);
128-
$namespace = lcfirst($namespace);
129-
130-
return str_replace('namespace App\Tests\Unit\Smoke', 'namespace App\\' . $namespace, $templateContents);
131-
}
132124
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\SmokeTestgen\Templating;
6+
7+
final class TemplateDecorator
8+
{
9+
public function decorate(string $templateContents, string $smokeTestsDirectory): string
10+
{
11+
$templateContents = $this->adjustKernelClass($templateContents);
12+
13+
return $this->adjustNamespace($smokeTestsDirectory, $templateContents);
14+
}
15+
16+
private function adjustKernelClass(string $templateContents): string
17+
{
18+
$kernelClass = $this->resolveKernelClass();
19+
20+
return str_replace('__KERNEL_CLASS_PLACEHOLDER__', $kernelClass, $templateContents);
21+
}
22+
23+
private function resolveKernelClass(): string
24+
{
25+
// use correct Kernel class
26+
if (class_exists('App\Kernel')) {
27+
return 'App\Kernel';
28+
}
29+
30+
if (class_exists('AppKernel')) {
31+
return 'AppKernel';
32+
}
33+
34+
return 'Kernel';
35+
}
36+
37+
private function adjustNamespace(string $templateContents, string $smokeTestsDirectory): string
38+
{
39+
if ($smokeTestsDirectory === 'tests/Unit/Smoke') {
40+
// default one, nothing to adjust
41+
return $templateContents;
42+
}
43+
44+
// @todo check with composer.json "psr-4" in require-dev
45+
$namespace = str_replace('/', '\\', $smokeTestsDirectory);
46+
$namespace = lcfirst($namespace);
47+
48+
return str_replace('namespace App\Tests\Unit\Smoke', 'namespace App\\' . $namespace, $templateContents);
49+
}
50+
}

src/Utils/TestPathResolver.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,13 @@
55
namespace Rector\SmokeTestgen\Utils;
66

77
use Rector\SmokeTestgen\Contract\TestByPackageSubscriberInterface;
8-
use Webmozart\Assert\Assert;
98

109
final class TestPathResolver
1110
{
1211
public static function resolve(
1312
TestByPackageSubscriberInterface $testByPackageSubscriber,
1413
string $smokeTestsDirectory
1514
): string {
16-
Assert::fileExists($testByPackageSubscriber->getTemplateFilePath());
17-
1815
$absolutePath = $testByPackageSubscriber->getTemplateFilePath();
1916
$testFileBasename = pathinfo($absolutePath, PATHINFO_BASENAME);
2017

templates/Symfony/AbstractContainerTestCase.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace App\Tests\Unit\Smoke;
66

7-
use AppKernel;
87
use PHPUnit\Framework\TestCase;
98
use Symfony\Component\DependencyInjection\Container;
109
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
@@ -21,11 +20,11 @@ abstract class AbstractContainerTestCase extends TestCase
2120
protected function setUp(): void
2221
{
2322
// @todo configure your test environment: most likely "tests", "ci" or "dev"
24-
$appKernel = new AppKernel('tests', true);
25-
$appKernel->boot();
23+
$kernel = new \__KERNEL_CLASS_PLACEHOLDER__('tests', true);
24+
$kernel->boot();
2625

27-
self::$kernel = $appKernel;
28-
self::$container = $appKernel->getContainer()->get('test.service_container');
26+
self::$kernel = $kernel;
27+
self::$container = $kernel->getContainer()->get('test.service_container');
2928
}
3029

3130
/**

0 commit comments

Comments
 (0)