From 5cf26ecb690474c31692fc37809cdb8b2a2dfcc1 Mon Sep 17 00:00:00 2001 From: Graham Campbell Date: Fri, 27 Dec 2019 13:58:46 +0000 Subject: [PATCH] [6.x] PHPUnit 9 support (#30947) * PHPUnit 9 support * Apply fixes from StyleCI (#30946) --- composer.json | 4 +- src/Illuminate/Foundation/Testing/Assert.php | 65 ++++++++++++------- .../FoundationProviderRepositoryTest.php | 2 +- 3 files changed, 44 insertions(+), 27 deletions(-) diff --git a/composer.json b/composer.json index 24b98eaea9dd..c8e37e9ca14c 100644 --- a/composer.json +++ b/composer.json @@ -79,13 +79,14 @@ "aws/aws-sdk-php": "^3.0", "doctrine/dbal": "^2.6", "filp/whoops": "^2.4", + "graham-campbell/testbench-core": "^3.1", "guzzlehttp/guzzle": "^6.3", "league/flysystem-cached-adapter": "^1.0", "mockery/mockery": "^1.3.1", "moontoast/math": "^1.1", "orchestra/testbench-core": "^4.0", "pda/pheanstalk": "^4.0", - "phpunit/phpunit": "^8.3", + "phpunit/phpunit": "^8.4|^9.0", "predis/predis": "^1.1.1", "symfony/cache": "^4.3.4" }, @@ -121,6 +122,7 @@ "doctrine/dbal": "Required to rename columns and drop SQLite columns (^2.6).", "filp/whoops": "Required for friendly error pages in development (^2.4).", "fzaninotto/faker": "Required to use the eloquent factory builder (^1.4).", + "graham-campbell/testbench-core": "Required to use the foundation testing component with PHPUnit 9 (^3.1).", "guzzlehttp/guzzle": "Required to use the Mailgun mail driver and the ping methods on schedules (^6.0).", "laravel/tinker": "Required to use the tinker console command (^1.0).", "league/flysystem-aws-s3-v3": "Required to use the Flysystem S3 driver (^1.0).", diff --git a/src/Illuminate/Foundation/Testing/Assert.php b/src/Illuminate/Foundation/Testing/Assert.php index efd53acb8f29..b6cbc8a0b95e 100644 --- a/src/Illuminate/Foundation/Testing/Assert.php +++ b/src/Illuminate/Foundation/Testing/Assert.php @@ -3,40 +3,55 @@ namespace Illuminate\Foundation\Testing; use ArrayAccess; +use Exception; +use GrahamCampbell\TestBenchCore\ArraySubsetTrait; use PHPUnit\Framework\Assert as PHPUnit; use PHPUnit\Framework\Constraint\ArraySubset; +use PHPUnit\Runner\Version; use PHPUnit\Util\InvalidArgumentHelper; -/** - * @internal This class is not meant to be used or overwritten outside the framework itself. - */ -abstract class Assert extends PHPUnit -{ +if (trait_exists(ArraySubsetTrait::class)) { /** - * Asserts that an array has a specified subset. - * - * This method was taken over from PHPUnit where it was deprecated. See link for more info. - * - * @param \ArrayAccess|array $subset - * @param \ArrayAccess|array $array - * @param bool $checkForObjectIdentity - * @param string $message - * @return void - * - * @link https://github.com/sebastianbergmann/phpunit/issues/3494 + * @internal This class is not meant to be used or overwritten outside the framework itself. */ - public static function assertArraySubset($subset, $array, bool $checkForObjectIdentity = false, string $message = ''): void + abstract class Assert extends PHPUnit { - if (! (is_array($subset) || $subset instanceof ArrayAccess)) { - throw InvalidArgumentHelper::factory(1, 'array or ArrayAccess'); - } + use ArraySubsetTrait; + } +} else { + /** + * @internal This class is not meant to be used or overwritten outside the framework itself. + */ + abstract class Assert extends PHPUnit + { + /** + * Asserts that an array has a specified subset. + * + * This method was taken over from PHPUnit where it was deprecated. See link for more info. + * + * @param \ArrayAccess|array $subset + * @param \ArrayAccess|array $array + * @param bool $checkForObjectIdentity + * @param string $message + * @return void + */ + public static function assertArraySubset($subset, $array, bool $checkForObjectIdentity = false, string $message = ''): void + { + if ((int) Version::series()[0] > 8) { + throw new Exception('For PHPUnit 9 support, please install graham-campbell/testbench-core:"^3.1".'); + } - if (! (is_array($array) || $array instanceof ArrayAccess)) { - throw InvalidArgumentHelper::factory(2, 'array or ArrayAccess'); - } + if (! (is_array($subset) || $subset instanceof ArrayAccess)) { + throw InvalidArgumentHelper::factory(1, 'array or ArrayAccess'); + } - $constraint = new ArraySubset($subset, $checkForObjectIdentity); + if (! (is_array($array) || $array instanceof ArrayAccess)) { + throw InvalidArgumentHelper::factory(2, 'array or ArrayAccess'); + } - static::assertThat($array, $constraint, $message); + $constraint = new ArraySubset($subset, $checkForObjectIdentity); + + static::assertThat($array, $constraint, $message); + } } } diff --git a/tests/Foundation/FoundationProviderRepositoryTest.php b/tests/Foundation/FoundationProviderRepositoryTest.php index 07dde96f1add..06402d8685a2 100755 --- a/tests/Foundation/FoundationProviderRepositoryTest.php +++ b/tests/Foundation/FoundationProviderRepositoryTest.php @@ -92,7 +92,7 @@ public function testWriteManifestStoresToProperLocation() public function testWriteManifestThrowsExceptionIfManifestDirDoesntExist() { $this->expectException(Exception::class); - $this->expectExceptionMessageRegExp('/^The (.*) directory must be present and writable.$/'); + $this->expectExceptionMessageMatches('/^The (.*) directory must be present and writable.$/'); $repo = new ProviderRepository(m::mock(ApplicationContract::class), $files = m::mock(Filesystem::class), __DIR__.'/cache/services.php'); $files->shouldReceive('replace')->never();