Skip to content

[PhpUnitBridge] Add compatibility to PHPUnit 9 #35662

@Hakihiro

Description

@Hakihiro

Symfony version(s) affected: 4.4.4 (Tested version)

Description
Impossible to use PHPUnit 9 with the actual version of Symfony/PHPUnit-bridge
Because of: sebastianbergmann/phpunit#3914

How to reproduce
In a Symfony project (Version 4.4.4)
Change SYMFONY_PHPUNIT_VERSION to 9 (in phpunit.xml or env var)
Launch your test you will get: PHP Fatal error: Uncaught Error: Call to undefined method PHPUnit\TextUI\Configuration\Configuration::getInstance()

Possible Solution
I suggest to :

  • add a new CommandForV9 file with these changes :
<?php

/*
 * This file is part of the Symfony package.
 *
 * (c) Fabien Potencier <fabien@symfony.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Symfony\Bridge\PhpUnit\Legacy;

use PHPUnit\TextUI\Command as BaseCommand;
- use PHPUnit\Util\Configuration;
+ use PHPUnit\TextUI\Configuration\Configuration;
+ use PHPUnit\TextUI\Configuration\Registry;
use PHPUnit\TextUI\TestRunner as BaseRunner;
use Symfony\Bridge\PhpUnit\SymfonyTestsListener;

/**
 * {@inheritdoc}
 *
 * @internal
 */
- class CommandForV6 extends BaseCommand
+ class CommandForV9 extends BaseCommand
{
    /**
     * {@inheritdoc}
     */
    protected function createRunner(): BaseRunner
    {
        $this->arguments['listeners'] = isset($this->arguments['listeners']) ? $this->arguments['listeners'] : [];

        $registeredLocally = false;

        foreach ($this->arguments['listeners'] as $registeredListener) {
            if ($registeredListener instanceof SymfonyTestsListener) {
                $registeredListener->globalListenerDisabled();
                $registeredLocally = true;

                break;
            }
        }

        if (isset($this->arguments['configuration'])) {
            $configuration = $this->arguments['configuration'];
            if (!$configuration instanceof Configuration) {
-                $configuration = Configuration::getInstance($this->arguments['configuration']);
+                $configuration = Registry::getInstance()->get($this->arguments['configuration']);
            }
-            foreach ($configuration->getListenerConfiguration() as $registeredListener) {
+            foreach ($configuration->listeners() as $registeredListener) {
-                 if ('Symfony\Bridge\PhpUnit\SymfonyTestsListener' === ltrim($registeredListener['class'], '\\')) {
+                 if ('Symfony\Bridge\PhpUnit\SymfonyTestsListener' === ltrim($registeredListener->className(), '\\')) {
                    $registeredLocally = true;

                    break;
                }
            }
        }

        if (!$registeredLocally) {
            $this->arguments['listeners'][] = new SymfonyTestsListener();
        }

        return parent::createRunner();
    }
}

And add the new file in https://github.com/symfony/phpunit-bridge/blob/master/TextUI/Command.php to switch to the right Command depending on the version.

At this moment I don't know if more changes are required to be fully compatible with PHPUnit 9 but in my case, it's enough to be able to test my code.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions