Skip to content
This repository has been archived by the owner on Mar 7, 2023. It is now read-only.

Commit

Permalink
Adds VoidReturnType migration - for PHPUnit 8
Browse files Browse the repository at this point in the history
  • Loading branch information
michalbundyra committed Dec 6, 2019
1 parent 512cc14 commit 69468af
Show file tree
Hide file tree
Showing 8 changed files with 179 additions and 10 deletions.
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,15 @@ $ ../path/to/phpunit-migration/bin/phpunit-migration migrate
7. `getMock` to `getMockBuilder` with other required function calls (PHPUnit 5.4+),
8. `getMockBuilder(...)->...->getMock()` to `createMock(...)` if possible (PHPUnit 5.4+),
9. `assertEquals()` and `assertNotEquals()` with `$delta`, `$maxDepth`, `$canonicalize` and `$ignoreCase`
parameters to more specific assertion method (PHPUnit 7.5),
10. TODO: `getMockBuilder(...)->...->setMethods(...)->getMock()` to `createPartialMock(...)` if possible
parameters to more specific assertion method (PHPUnit 7.5+),
10. add void return type to the following methods:
`setUp()`, `tearDown()`, `setUpBeforeClass()`, `tearDownAfterClass()`,
`assertPreConditions()`, `assertPostConditions()`, `onNotSuccessfulTest(\Throwable $th)` (PHPUnit 8.0+),
11. TODO: `getMockBuilder(...)->...->setMethods(...)->getMock()` to `createPartialMock(...)` if possible
(PHPUnit 5.5.3+),
11. TODO: `assertContains()` and `assertNotContains()` on `string` haystack to more specific assertion method
12. TODO: `assertContains()` and `assertNotContains()` on `string` haystack to more specific assertion method
(PHPUnit 7.5+),
12. TODO: `$this->assert` to `self::assert`.
13. TODO: `$this->assert` to `self::assert`.

## What the tool is NOT doing?

Expand Down
18 changes: 12 additions & 6 deletions src/Migrate.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use Webimpress\PHPUnitMigration\Migration\SetUpMigration;
use Webimpress\PHPUnitMigration\Migration\TearDownMigration;
use Webimpress\PHPUnitMigration\Migration\TestCaseMigration;
use Webimpress\PHPUnitMigration\Migration\VoidReturnTypeMigration;

use function array_reverse;
use function exec;
Expand All @@ -50,7 +51,6 @@
use function strstr;
use function strtolower;
use function usort;
use function version_compare;

use const JSON_PRETTY_PRINT;
use const JSON_UNESCAPED_SLASHES;
Expand All @@ -68,7 +68,7 @@ class Migrate extends Command
private $versionsJson;

/** @var float */
private $php7max = 7.3;
private $php7max = 7.4;

protected function configure()
{
Expand Down Expand Up @@ -132,6 +132,7 @@ protected function execute(InputInterface $input, OutputInterface $output)

$iterations = (int) $input->getArgument('iterations');

$phpRequired = $this->getPHP5Version($php) ?? $this->getPHP7Version($php);
$minPHPUnitVersion = $this->findMinimumPHPUnitVersion($phpunit);
$newPHPUnitVersions = $this->findPHPUnitVersion($php);

Expand All @@ -140,7 +141,7 @@ protected function execute(InputInterface $input, OutputInterface $output)

if ($to >= 5) {
foreach ($this->fileIterator() as $file) {
$content = $this->replaceTestCase($file, $newPHPUnitVersions[0], $iterations);
$content = $this->replaceTestCase($file, $newPHPUnitVersions[0], $phpRequired, $iterations);
file_put_contents($file, $content);
}
}
Expand Down Expand Up @@ -220,8 +221,12 @@ private function files(string $path) : Generator
}
}

private function replaceTestCase(string $fileName, string $phpUnitVersion, int $iterations) : string
{
private function replaceTestCase(
string $fileName,
string $phpUnitVersion,
?string $phpVersion,
int $iterations
) : string {
$content = file_get_contents($fileName);

$migrations = [
Expand All @@ -233,11 +238,12 @@ private function replaceTestCase(string $fileName, string $phpUnitVersion, int $
new SetUpMigration(),
new TearDownMigration(),
new TestCaseMigration(),
new VoidReturnTypeMigration(),
];

for ($i = 1; $i <= $iterations; ++$i) {
foreach ($migrations as $migration) {
if (version_compare($phpUnitVersion, $migration::PHPUNIT_VERSION_REQUIRED) >= 0) {
if ($migration->canBeExecuted($phpUnitVersion, $phpVersion)) {
echo sprintf('[%d] Run migration %s on file %s', $i, get_class($migration), $fileName), PHP_EOL;
$content = $migration->migrate($content);
}
Expand Down
7 changes: 7 additions & 0 deletions src/Migration/AbstractMigration.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,16 @@

namespace Webimpress\PHPUnitMigration\Migration;

use function version_compare;

abstract class AbstractMigration
{
public const PHPUNIT_VERSION_REQUIRED = '5.0';

abstract public function migrate(string $content) : string;

public function canBeExecuted(string $phpUnitVersion, ?string $phpVersion) : bool
{
return version_compare($phpUnitVersion, static::PHPUNIT_VERSION_REQUIRED) >= 0;
}
}
41 changes: 41 additions & 0 deletions src/Migration/VoidReturnTypeMigration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace Webimpress\PHPUnitMigration\Migration;

use function implode;
use function preg_replace;
use function version_compare;

class VoidReturnTypeMigration extends AbstractMigration
{
public const PHPUNIT_VERSION_REQUIRED = '8.0';

private const FUNCTION_NAME = [
'setUp',
'tearDown',
'setUpBeforeClass',
'tearDownAfterClass',
'assertPreConditions',
'assertPostConditions',
'onNotSuccessfulTest',
];

public function migrate(string $content) : string
{
$content = preg_replace(
'/(function\s+(' . implode('|', self::FUNCTION_NAME) . ')\s*\([^)]*\))([^:{]*{)/i',
'\\1 : void\\3',
$content
);

return $content;
}

public function canBeExecuted(string $phpUnitVersion, ?string $phpVersion) : bool
{
return version_compare($phpVersion, '7.0') >= 0
|| parent::canBeExecuted($phpUnitVersion, $phpVersion);
}
}
30 changes: 30 additions & 0 deletions test/Migration/TestAsset/VoidReturnTypeMigration.1.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace WebimpressTest\PHPUnitMigration\Migration\TestAsset;

use PHPUnit\Framework\TestCase;

class VoidReturnTypeMigration extends TestCase
{
public function setUp()
{
}

protected function tearDown()
{
}

public static function setUpBeforeClass()
{
}

public static function tearDownAfterClass()
{
}

protected function assertPreConditions() {}

protected function assertPostConditions() {}

protected function onNotSuccessfulTest(\Throwable $t) {}
}
30 changes: 30 additions & 0 deletions test/Migration/TestAsset/VoidReturnTypeMigration.1.inc.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace WebimpressTest\PHPUnitMigration\Migration\TestAsset;

use PHPUnit\Framework\TestCase;

class VoidReturnTypeMigration extends TestCase
{
public function setUp() : void
{
}

protected function tearDown() : void
{
}

public static function setUpBeforeClass() : void
{
}

public static function tearDownAfterClass() : void
{
}

protected function assertPreConditions() : void {}

protected function assertPostConditions() : void {}

protected function onNotSuccessfulTest(\Throwable $t) : void {}
}
26 changes: 26 additions & 0 deletions test/Migration/TestAsset/VoidReturnTypeMigration.2.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace WebimpressTest\PHPUnitMigration\Migration\TestAsset;

use PHPUnit\Framework\TestCase;

class VoidReturnTypeMigration extends TestCase
{
public function setUp(): void
{
}

protected function tearDown() // comment
{
}

public static function setUpBeforeClass()
: void
{
}

public static function tearDownAfterClass()
// comment
{
}
}
26 changes: 26 additions & 0 deletions test/Migration/TestAsset/VoidReturnTypeMigration.2.inc.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace WebimpressTest\PHPUnitMigration\Migration\TestAsset;

use PHPUnit\Framework\TestCase;

class VoidReturnTypeMigration extends TestCase
{
public function setUp(): void
{
}

protected function tearDown() : void // comment
{
}

public static function setUpBeforeClass()
: void
{
}

public static function tearDownAfterClass() : void
// comment
{
}
}

0 comments on commit 69468af

Please sign in to comment.