diff -u b/composer.json b/composer.json --- b/composer.json +++ b/composer.json @@ -49,12 +49,10 @@ }, "scripts": { "pre-autoload-dump": "Drupal\\Core\\Composer\\Composer::preAutoloadDump", - "post-autoload-dump": [ - "Drupal\\Core\\Composer\\Composer::ensureHtaccess", - "Drupal\\Core\\Composer\\Composer::upgradePHPUnit" - ], + "post-autoload-dump": "Drupal\\Core\\Composer\\Composer::ensureHtaccess", "post-package-install": "Drupal\\Core\\Composer\\Composer::vendorTestCodeCleanup", "post-package-update": "Drupal\\Core\\Composer\\Composer::vendorTestCodeCleanup", + "post-install-cmd": "Drupal\\Core\\Composer\\Composer::upgradePHPUnit", "drupal-phpunit-upgrade": "@composer upgrade phpunit/phpunit --with-dependencies --no-progress", "phpcs": "phpcs --standard=core/phpcs.xml.dist --runtime-set installed_paths $($COMPOSER_BINARY config vendor-dir)/drupal/coder/coder_sniffer --", "phpcbf": "phpcbf --standard=core/phpcs.xml.dist --runtime-set installed_paths $($COMPOSER_BINARY config vendor-dir)/drupal/coder/coder_sniffer --" diff -u b/core/lib/Drupal/Core/Composer/Composer.php b/core/lib/Drupal/Core/Composer/Composer.php --- b/core/lib/Drupal/Core/Composer/Composer.php +++ b/core/lib/Drupal/Core/Composer/Composer.php @@ -6,7 +6,6 @@ use Composer\Script\Event; use Composer\Installer\PackageEvent; use Composer\Semver\Constraint\Constraint; -use PHPUnit\Runner\Version; /** * Provides static functions for composer script events. @@ -150,15 +149,23 @@ * @param \Composer\Script\Event $event */ public static function upgradePHPUnit(Event $event) { - if (class_exists('PHPUnit_Runner_Version')) { - $phpunit_version = \PHPUnit_Runner_Version::id(); + $repository = $event->getComposer()->getRepositoryManager()->getLocalRepository(); + // This is, essentially, a null constraint. We only care whether the package + // is present in the vendor directory yet, but findPackage() requires it. + $constraint = new Constraint('>', ''); + $phpunit_package = $repository->findPackage('phpunit/phpunit', $constraint); + if (!$phpunit_package) { + // There is nothing to do. The user is probably installing using the + // --no-dev flag. + return; } - if (class_exists('\PHPUnit\Runner\Version')) { - $phpunit_version = Version::id(); - } - // @todo if we haven't determine version throw an error. - if (version_compare(PHP_MAJOR_VERSION . '.' . PHP_MINOR_VERSION, '7.1') >= 0 && version_compare($phpunit_version, '6.0') < 0) { - $event->getComposer()->getEventDispatcher()->dispatchScript('drupal-phpunit-upgrade'); + + // If the PHP version is 7.1 or above and PHPUnit is less than version 6 + // call the drupal-phpunit-upgrade script to upgrade PHPUnit. + if (version_compare(PHP_MAJOR_VERSION . '.' . PHP_MINOR_VERSION, '7.1') >= 0 && version_compare($phpunit_package->getVersion(), '6.0') < 0) { + $event->getComposer() + ->getEventDispatcher() + ->dispatchScript('drupal-phpunit-upgrade'); } } diff -u b/core/tests/Drupal/Tests/Component/DependencyInjection/ContainerTest.php b/core/tests/Drupal/Tests/Component/DependencyInjection/ContainerTest.php --- b/core/tests/Drupal/Tests/Component/DependencyInjection/ContainerTest.php +++ b/core/tests/Drupal/Tests/Component/DependencyInjection/ContainerTest.php @@ -987,8 +987,11 @@ * Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. * * @param mixed $class + * The expected exception class. * @param string $message + * The expected exception message. * @param int $exception_code + * The expected exception code. */ public function setExpectedException($class, $message = '', $exception_code = NULL) { if (method_exists($this, 'expectException')) { diff -u b/core/tests/Drupal/Tests/Component/Plugin/DefaultFactoryTest.php b/core/tests/Drupal/Tests/Component/Plugin/DefaultFactoryTest.php --- b/core/tests/Drupal/Tests/Component/Plugin/DefaultFactoryTest.php +++ b/core/tests/Drupal/Tests/Component/Plugin/DefaultFactoryTest.php @@ -148,8 +148,11 @@ * Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. * * @param mixed $class + * The expected exception class. * @param string $message + * The expected exception message. * @param int $exception_code + * The expected exception code. */ public function setExpectedException($class, $message = '', $exception_code = NULL) { if (method_exists($this, 'expectException')) { only in patch2: unchanged: --- a/core/tests/Drupal/Tests/PhpunitCompatibilityTrait.php +++ b/core/tests/Drupal/Tests/PhpunitCompatibilityTrait.php @@ -116,6 +116,31 @@ public function createMock($originalClassName) { } } + /** + * Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. + * + * @param mixed $class + * The expected exception class. + * @param string $message + * The expected exception message. + * @param int $exception_code + * The expected exception code. + */ + public function setExpectedException($class, $message = '', $exception_code = NULL) { + if (method_exists($this, 'expectException')) { + $this->expectException($class); + if (!empty($message)) { + $this->expectExceptionMessage($message); + } + if ($exception_code !== NULL) { + $this->expectExceptionCode($exception_code); + } + } + else { + parent::setExpectedException($class, $message, $exception_code); + } + } + /** * Checks if the trait is used in a class that has a method. *