Skip to content

Commit f7560c6

Browse files
committed
[tests] bring test suite up to PHP8 standards
1 parent 09f9cbf commit f7560c6

File tree

6 files changed

+63
-71
lines changed

6 files changed

+63
-71
lines changed

src/Test/MakerTestCase.php

Lines changed: 11 additions & 26 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));
@@ -143,24 +148,4 @@ private function hasRequiredDependencyVersions(MakerTestDetails $testDetails, Ma
143148

144149
return true;
145150
}
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-
}
166151
}

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 & 19 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,7 +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);
401+
$data = json_decode($process->getOutput(), true, 512, \JSON_THROW_ON_ERROR);
409402
if (null === $data) {
410403
throw new \Exception('Could not determine dependencies');
411404
}

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: 20 additions & 7 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;
@@ -64,6 +66,8 @@ public function copy(string $source, string $destination)
6466

6567
/**
6668
* When using an authenticator "fixtures" file, this adjusts it to support Symfony 5.2/5.3.
69+
*
70+
* @deprecated legacy PassportInterface support will be dropped in a future version
6771
*/
6872
public function adjustAuthenticatorForLegacyPassportInterface(string $filename): void
6973
{
@@ -116,6 +120,9 @@ public function getExecutedMakerProcess(): MakerTestProcess
116120
return $this->executedMakerProcess;
117121
}
118122

123+
/**
124+
* @return void
125+
*/
119126
public function modifyYamlFile(string $filename, \Closure $callback)
120127
{
121128
$path = $this->getPath($filename);
@@ -130,6 +137,9 @@ public function modifyYamlFile(string $filename, \Closure $callback)
130137
file_put_contents($path, $manipulator->getContents());
131138
}
132139

140+
/**
141+
* @return void
142+
*/
133143
public function runConsole(string $command, array $inputs, string $arguments = '')
134144
{
135145
$process = $this->environment->createInteractiveCommandProcess(
@@ -197,7 +207,7 @@ public function configureDatabase(bool $createSchema = true): void
197207
// this looks silly, but it's the only way to drop the database *for sure*,
198208
// as doctrine:database:drop will error if there is no database
199209
// also, skip for SQLITE, as it does not support --if-not-exists
200-
if (0 !== strpos(getenv('TEST_DATABASE_DSN'), 'sqlite://')) {
210+
if (!str_starts_with(getenv('TEST_DATABASE_DSN'), 'sqlite://')) {
201211
$this->runConsole('doctrine:database:create', [], '--env=test --if-not-exists');
202212
}
203213
$this->runConsole('doctrine:database:drop', [], '--env=test --force');
@@ -234,6 +244,9 @@ public function writeFile(string $filename, string $contents): void
234244
file_put_contents($this->getPath($filename), $contents);
235245
}
236246

247+
/**
248+
* @return void
249+
*/
237250
public function addToAutoloader(string $namespace, string $path)
238251
{
239252
$this->replaceInFile(

0 commit comments

Comments
 (0)