Skip to content

Commit

Permalink
Fix stub PHPUnit\Framework\TestCase (#1954)
Browse files Browse the repository at this point in the history
* Fix stub PHPUnit\Framework\TestCase

* final touch: clean up

* final touch: clean up

* load after bootstrap loaded

* move to target-repository

* final touch: remove stubs-rector on remote-repository first when exists
  • Loading branch information
samsonasik committed Mar 22, 2022
1 parent e7c0283 commit 8f86f52
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 64 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build_scoped_rector.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ jobs:
token: ${{ secrets.ACCESS_TOKEN }}

# remove remote files, to avoid piling up dead code in remote repository
- run: rm -rf remote-repository/.github remote-repository/e2e remote-repository/config remote-repository/src remote-repository/rules remote-repository/packages remote-repository/vendor
- run: rm -rf remote-repository/.github remote-repository/e2e remote-repository/config remote-repository/src remote-repository/rules remote-repository/packages remote-repository/vendor remote-repository/stubs-rector

- run: cp -a rector-prefixed-downgraded/. remote-repository

Expand Down
1 change: 0 additions & 1 deletion build/build-preload.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ private function findPhpParserFiles(string $vendorDir): array
->notPath('#\/tests\/#')
->notPath('#\/config\/#')
->notPath('#\/set\/#')
->in($vendorDir . '/symplify/symfony-php-config')
->sortByName();

return iterator_to_array($finder->getIterator());
Expand Down
2 changes: 1 addition & 1 deletion build/config/config-downgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
$phpStanStubLoader = new PHPStanStubLoader();
$phpStanStubLoader->loadStubs();

require_once __DIR__ . '/../../stubs-rector/PHPUnit/Framework/TestCase.php';
require_once __DIR__ . '/../target-repository/stubs-rector/PHPUnit/Framework/TestCase.php';
require_once __DIR__ . '/../../stubs/Composer/EventDispatcher/EventSubscriberInterface.php';
require_once __DIR__ . '/../../stubs/Composer/Plugin/PluginInterface.php';
require_once __DIR__ . '/../../stubs/Nette/DI/CompilerExtension.php';
Expand Down
109 changes: 48 additions & 61 deletions build/target-repository/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,81 +2,68 @@

declare(strict_types = 1);

namespace
{
// inspired by https://github.com/phpstan/phpstan/blob/master/bootstrap.php
spl_autoload_register(function (string $class): void {
static $composerAutoloader;
// inspired by https://github.com/phpstan/phpstan/blob/master/bootstrap.php
spl_autoload_register(function (string $class): void {
static $composerAutoloader;

// already loaded in bin/rector.php
if (defined('__RECTOR_RUNNING__')) {
return;
}
// already loaded in bin/rector.php
if (defined('__RECTOR_RUNNING__')) {
return;
}

// load prefixed or native class, e.g. for running tests
if (strpos($class, 'RectorPrefix') === 0 || strpos($class, 'Rector\\') === 0) {
if ($composerAutoloader === null) {
// prefixed version autoload
$composerAutoloader = require __DIR__ . '/vendor/autoload.php';
}
$composerAutoloader->loadClass($class);
// load prefixed or native class, e.g. for running tests
if (strpos($class, 'RectorPrefix') === 0 || strpos($class, 'Rector\\') === 0) {
if ($composerAutoloader === null) {
// prefixed version autoload
$composerAutoloader = require __DIR__ . '/vendor/autoload.php';
}
$composerAutoloader->loadClass($class);
}

// aliased by php-scoper, that's why its missing
if ($class === 'Symplify\SmartFileSystem\SmartFileInfo') {
$filePath = __DIR__ . '/vendor/symplify/smart-file-system/src/SmartFileInfo.php';
if (file_exists($filePath)) {
require $filePath;
}
// aliased by php-scoper, that's why its missing
if ($class === 'Symplify\SmartFileSystem\SmartFileInfo') {
$filePath = __DIR__ . '/vendor/symplify/smart-file-system/src/SmartFileInfo.php';
if (file_exists($filePath)) {
require $filePath;
}
}

if ($class === 'Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator') {
// avoid duplicated autoload bug in Rector demo runner
if (class_exists('Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator', false)) {
return;
}
if ($class === 'Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator') {
// avoid duplicated autoload bug in Rector demo runner
if (class_exists('Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator', false)) {
return;
}
});
}
});

if (! interface_exists('UnitEnum')) {
if (! interface_exists('UnitEnum')) {
/**
* @since 8.1
*/
interface UnitEnum
{
/**
* @since 8.1
* @return static[]
*/
interface UnitEnum
{
/**
* @return static[]
*/
public static function cases(): array;
}
public static function cases(): array;
}
}

if (! interface_exists('BackedEnum')) {
if (! interface_exists('BackedEnum')) {
/**
* @since 8.1
*/
interface BackedEnum extends UnitEnum {
/**
* @since 8.1
* @param int|string $value
* @return $this
*/
interface BackedEnum extends UnitEnum {
/**
* @param int|string $value
* @return $this
*/
public static function from($value);

/**
* @param int|string $value
* @return $this|null
*/
public static function tryFrom($value);
}
}
}
public static function from($value);

// @see https://github.com/rectorphp/rector/issues/6531
namespace PHPUnit\Framework
{
if (! class_exists('PHPUnit\Framework\TestCase')) {
abstract class TestCase
{
}
/**
* @param int|string $value
* @return $this|null
*/
public static function tryFrom($value);
}
}
4 changes: 4 additions & 0 deletions src/Autoloading/BootstrapFilesIncluder.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,9 @@ public function includeBootstrapFiles(): void
throw new ShouldNotHappenException($errorMessage, $throwable->getCode(), $throwable);
}
}

if (is_file(__DIR__ . '/../../stubs-rector/PHPUnit/Framework/TestCase.php')) {
require_once __DIR__ . '/../../stubs-rector/PHPUnit/Framework/TestCase.php';
}
}
}

4 comments on commit 8f86f52

@Wirone
Copy link
Contributor

@Wirone Wirone commented on 8f86f52 Mar 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need this fix related to PHPUnit\Framework\TestCase because changes in bootstrap introduced in 0.12.8 broke our Codeception - there's PHPUnit wrapper for PHPUnit\Framework\Test interface and it stopped working:

Fatal error: Declaration of Codeception\Test\Test::run(?PHPUnit\Framework\TestResult $result = NULL) must be compatible with PHPUnit\Framework\Test::run(?PHPUnit\Framework\TestResult $result = NULL): PHPUnit\Framework\TestResult in /app/vendor/codeception/codeception/src/Codeception/Test/Test.php on line 68

@TomasVotruba when new release will be available?

@TomasVotruba
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you confirm latest dev main works?

@Wirone
Copy link
Contributor

@Wirone Wirone commented on 8f86f52 Mar 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TomasVotruba It looks like it's working πŸŽ‰

www-data@php:/app$ vim composer.json
www-data@php:/app$ composer update rector/rector -w
Loading composer repositories with package information
Updating dependencies
Lock file operations: 0 installs, 1 update, 0 removals
  - Upgrading rector/rector (0.12.18 => dev-main 9728a66)
Writing lock file
Installing dependencies from lock file (including require-dev)
Package operations: 0 installs, 1 update, 0 removals
  - Downloading rector/rector (dev-main 9728a66)
  - Upgrading rector/rector (0.12.18 => dev-main 9728a66): Extracting archive
Generating autoload files
www-data@php:/app$ vendor/bin/codecept run Integration -c ./packages/Ecommerce/.gitlab-ci-codeception.yml --steps
Codeception PHP Testing Framework v4.1.31 https://helpukrainewin.org
Powered by PHPUnit 9.5.19 #StandWithUkraine

##### 'Importing': db/shard/schema.sql

on 0.12.8 there was fatal error right after Powered by PHPUnit πŸ™‚

@TomasVotruba
Copy link
Member

@TomasVotruba TomasVotruba commented on 8f86f52 Mar 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In that case, let's ship the new version πŸ˜‰

https://github.com/rectorphp/rector/releases/tag/0.12.19

Please sign in to comment.