Skip to content

Commit d2ce2ec

Browse files
committed
feature #1129 [tests] bring test suite up to PHP8 standards (jrushlow)
This PR was squashed before being merged into the 1.0-dev branch. Discussion ---------- [tests] bring test suite up to PHP8 standards Commits ------- 669c325 [tests] bring test suite up to PHP8 standards
2 parents 09f9cbf + 669c325 commit d2ce2ec

File tree

6 files changed

+63
-98
lines changed

6 files changed

+63
-98
lines changed

src/Test/MakerTestCase.php

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,12 @@
2020

2121
abstract class MakerTestCase extends TestCase
2222
{
23-
/**
24-
* @var KernelInterface
25-
*/
26-
private $kernel;
23+
private ?KernelInterface $kernel = null;
2724

2825
/**
2926
* @dataProvider getTestDetails
27+
*
28+
* @return void
3029
*/
3130
public function testExecute(MakerTestDetails $makerTestDetails)
3231
{
@@ -42,6 +41,9 @@ protected function createMakerTest(): MakerTestDetails
4241
return new MakerTestDetails($this->getMakerInstance($this->getMakerClass()));
4342
}
4443

44+
/**
45+
* @return void
46+
*/
4547
protected function executeMakerCommand(MakerTestDetails $testDetails)
4648
{
4749
if (!class_exists(Process::class)) {
@@ -75,7 +77,7 @@ protected function executeMakerCommand(MakerTestDetails $testDetails)
7577
foreach ($files as $file) {
7678
$this->assertTrue($testEnv->fileExists($file), sprintf('The file "%s" does not exist after generation', $file));
7779

78-
if ('.php' === substr($file, -4)) {
80+
if (str_ends_with($file, '.php')) {
7981
$csProcess = $testEnv->runPhpCSFixer($file);
8082

8183
$this->assertTrue($csProcess->isSuccessful(), sprintf(
@@ -85,14 +87,17 @@ protected function executeMakerCommand(MakerTestDetails $testDetails)
8587
));
8688
}
8789

88-
if ('.twig' === substr($file, -5)) {
90+
if (str_ends_with($file, '.twig')) {
8991
$csProcess = $testEnv->runTwigCSLint($file);
9092

9193
$this->assertTrue($csProcess->isSuccessful(), sprintf('File "%s" has a twig-cs problem: %s', $file, $csProcess->getErrorOutput()."\n".$csProcess->getOutput()));
9294
}
9395
}
9496
}
9597

98+
/**
99+
* @return void
100+
*/
96101
protected function assertContainsCount(string $needle, string $haystack, int $count)
97102
{
98103
$this->assertEquals(1, substr_count($haystack, $needle), sprintf('Found more than %d occurrences of "%s" in "%s"', $count, $needle, $haystack));
@@ -122,8 +127,9 @@ private function hasRequiredDependencyVersions(MakerTestDetails $testDetails, Ma
122127
return true;
123128
}
124129

125-
$installedPackages = json_decode($testEnv->readFile('vendor/composer/installed.json'), true);
130+
$installedPackages = json_decode($testEnv->readFile('vendor/composer/installed.json'), true, 512, \JSON_THROW_ON_ERROR);
126131
$packageVersions = [];
132+
127133
foreach ($installedPackages['packages'] ?? $installedPackages as $installedPackage) {
128134
$packageVersions[$installedPackage['name']] = $installedPackage['version_normalized'];
129135
}
@@ -143,24 +149,4 @@ private function hasRequiredDependencyVersions(MakerTestDetails $testDetails, Ma
143149

144150
return true;
145151
}
146-
147-
public static function assertStringContainsString(string $needle, string $haystack, string $message = ''): void
148-
{
149-
if (method_exists(TestCase::class, 'assertStringContainsString')) {
150-
parent::assertStringContainsString($needle, $haystack, $message);
151-
} else {
152-
// legacy for older phpunit versions (e.g. older php version on CI)
153-
self::assertContains($needle, $haystack, $message);
154-
}
155-
}
156-
157-
public static function assertStringNotContainsString(string $needle, string $haystack, string $message = ''): void
158-
{
159-
if (method_exists(TestCase::class, 'assertStringNotContainsString')) {
160-
parent::assertStringNotContainsString($needle, $haystack, $message);
161-
} else {
162-
// legacy for older phpunit versions (e.g. older php version on CI)
163-
self::assertNotContains($needle, $haystack, $message);
164-
}
165-
}
166152
}

src/Test/MakerTestDetails.php

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,16 @@
1616

1717
final class MakerTestDetails
1818
{
19-
private $maker;
19+
private ?\Closure $runCallback = null;
20+
private array $preRunCallbacks = [];
21+
private array $extraDependencies = [];
22+
private string $rootNamespace = 'App';
23+
private int $requiredPhpVersion = 80000;
24+
private array $requiredPackageVersions = [];
2025

21-
private $runCallback;
22-
23-
private $preRunCallbacks = [];
24-
25-
private $extraDependencies = [];
26-
27-
private $rootNamespace = 'App';
28-
29-
private $requiredPhpVersion;
30-
31-
private $requiredPackageVersions = [];
32-
33-
public function __construct(MakerInterface $maker)
34-
{
35-
$this->maker = $maker;
26+
public function __construct(
27+
private MakerInterface $maker,
28+
) {
3629
}
3730

3831
public function run(\Closure $callback): self
@@ -49,6 +42,9 @@ public function preRun(\Closure $callback): self
4942
return $this;
5043
}
5144

45+
/**
46+
* @return string
47+
*/
5248
public function getRootNamespace()
5349
{
5450
return $this->rootNamespace;
@@ -70,6 +66,8 @@ public function addExtraDependencies(string ...$packages): self
7066

7167
public function setRequiredPhpVersion(int $version): self
7268
{
69+
@trigger_deprecation('symfony/maker-bundle', 'v1.44.0', 'setRequiredPhpVersion() is no longer used and will be removed in a future version.');
70+
7371
$this->requiredPhpVersion = $version;
7472

7573
return $this;
@@ -120,7 +118,7 @@ public function getDependencyBuilder(): DependencyBuilder
120118

121119
public function isSupportedByCurrentPhpVersion(): bool
122120
{
123-
return null === $this->requiredPhpVersion || \PHP_VERSION_ID >= $this->requiredPhpVersion;
121+
return \PHP_VERSION_ID >= $this->requiredPhpVersion;
124122
}
125123

126124
public function getRequiredPackageVersions(): array

src/Test/MakerTestEnvironment.php

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,25 +22,18 @@
2222
*/
2323
final class MakerTestEnvironment
2424
{
25-
private $testDetails;
26-
private $fs;
27-
private $rootPath;
28-
private $cachePath;
29-
private $flexPath;
30-
private $path;
31-
32-
/**
33-
* @var MakerTestProcess
34-
*/
35-
private $runnedMakerProcess;
36-
37-
private function __construct(MakerTestDetails $testDetails)
38-
{
39-
$this->testDetails = $testDetails;
25+
private Filesystem $fs;
26+
private bool|string $rootPath;
27+
private string $cachePath;
28+
private string $flexPath;
29+
private string $path;
30+
private MakerTestProcess $runnedMakerProcess;
31+
32+
private function __construct(
33+
private MakerTestDetails $testDetails,
34+
) {
4035
$this->fs = new Filesystem();
41-
4236
$this->rootPath = realpath(__DIR__.'/../../');
43-
4437
$cachePath = $this->rootPath.'/tests/tmp/cache';
4538

4639
if (!$this->fs->exists($cachePath)) {
@@ -325,7 +318,7 @@ public function processReplacement(string $rootDir, string $filename, string $fi
325318
}
326319

327320
$contents = file_get_contents($path);
328-
if (false === strpos($contents, $find)) {
321+
if (!str_contains($contents, $find)) {
329322
if ($allowNotFound) {
330323
return;
331324
}
@@ -405,10 +398,7 @@ private function determineMissingDependencies(): array
405398
');
406399

407400
$process = MakerTestProcess::create('php dep_runner.php', $this->path)->run();
408-
$data = json_decode($process->getOutput(), true);
409-
if (null === $data) {
410-
throw new \Exception('Could not determine dependencies');
411-
}
401+
$data = json_decode($process->getOutput(), true, 512, \JSON_THROW_ON_ERROR);
412402

413403
unlink($this->path.'/dep_builder');
414404
unlink($this->path.'/dep_runner.php');

src/Test/MakerTestKernel.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class MakerTestKernel extends Kernel implements CompilerPassInterface
2525
{
2626
use MicroKernelTrait;
2727

28-
private $testRootDir;
28+
private string $testRootDir;
2929

3030
public function __construct(string $environment, bool $debug)
3131
{
@@ -70,6 +70,9 @@ public function getRootDir(): string
7070
return $this->testRootDir;
7171
}
7272

73+
/**
74+
* @return void
75+
*/
7376
public function process(ContainerBuilder $container)
7477
{
7578
// makes all makers public to help the tests

src/Test/MakerTestProcess.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
*/
2121
final class MakerTestProcess
2222
{
23-
private $process;
23+
private Process $process;
2424

2525
private function __construct($commandLine, $cwd, array $envVars, $timeout)
2626
{

src/Test/MakerTestRunner.php

Lines changed: 18 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,12 @@
2222

2323
class MakerTestRunner
2424
{
25-
private $environment;
26-
private $filesystem;
27-
private $executedMakerProcess;
25+
private Filesystem $filesystem;
26+
private ?MakerTestProcess $executedMakerProcess = null;
2827

29-
public function __construct(MakerTestEnvironment $environment)
30-
{
31-
$this->environment = $environment;
28+
public function __construct(
29+
private MakerTestEnvironment $environment,
30+
) {
3231
$this->filesystem = new Filesystem();
3332
}
3433

@@ -39,6 +38,9 @@ public function runMaker(array $inputs, string $argumentsString = '', bool $allo
3938
return $this->executedMakerProcess->getOutput();
4039
}
4140

41+
/**
42+
* @return void
43+
*/
4244
public function copy(string $source, string $destination)
4345
{
4446
$path = __DIR__.'/../../tests/fixtures/'.$source;
@@ -62,29 +64,6 @@ public function copy(string $source, string $destination)
6264
}
6365
}
6466

65-
/**
66-
* When using an authenticator "fixtures" file, this adjusts it to support Symfony 5.2/5.3.
67-
*/
68-
public function adjustAuthenticatorForLegacyPassportInterface(string $filename): void
69-
{
70-
// no adjustment needed on 5.4 and higher
71-
if ($this->getSymfonyVersion() >= 50400) {
72-
return;
73-
}
74-
75-
$this->replaceInFile(
76-
$filename,
77-
'\\Passport;',
78-
"\\Passport;\nuse Symfony\Component\Security\Http\Authenticator\Passport\PassportInterface;"
79-
);
80-
81-
$this->replaceInFile(
82-
$filename,
83-
': Passport',
84-
': PassportInterface'
85-
);
86-
}
87-
8867
public function renderTemplateFile(string $source, string $destination, array $variables): void
8968
{
9069
$twig = new Environment(
@@ -116,6 +95,9 @@ public function getExecutedMakerProcess(): MakerTestProcess
11695
return $this->executedMakerProcess;
11796
}
11897

98+
/**
99+
* @return void
100+
*/
119101
public function modifyYamlFile(string $filename, \Closure $callback)
120102
{
121103
$path = $this->getPath($filename);
@@ -130,6 +112,9 @@ public function modifyYamlFile(string $filename, \Closure $callback)
130112
file_put_contents($path, $manipulator->getContents());
131113
}
132114

115+
/**
116+
* @return void
117+
*/
133118
public function runConsole(string $command, array $inputs, string $arguments = '')
134119
{
135120
$process = $this->environment->createInteractiveCommandProcess(
@@ -197,7 +182,7 @@ public function configureDatabase(bool $createSchema = true): void
197182
// this looks silly, but it's the only way to drop the database *for sure*,
198183
// as doctrine:database:drop will error if there is no database
199184
// also, skip for SQLITE, as it does not support --if-not-exists
200-
if (0 !== strpos(getenv('TEST_DATABASE_DSN'), 'sqlite://')) {
185+
if (!str_starts_with(getenv('TEST_DATABASE_DSN'), 'sqlite://')) {
201186
$this->runConsole('doctrine:database:create', [], '--env=test --if-not-exists');
202187
}
203188
$this->runConsole('doctrine:database:drop', [], '--env=test --force');
@@ -234,6 +219,9 @@ public function writeFile(string $filename, string $contents): void
234219
file_put_contents($this->getPath($filename), $contents);
235220
}
236221

222+
/**
223+
* @return void
224+
*/
237225
public function addToAutoloader(string $namespace, string $path)
238226
{
239227
$this->replaceInFile(

0 commit comments

Comments
 (0)