From cdade69d7baf3d5ddc6a43a76dffd052f871fb65 Mon Sep 17 00:00:00 2001 From: Max Date: Fri, 30 Apr 2021 21:54:55 +1000 Subject: [PATCH 1/2] Improve laravel 8 testing (application stub & version detection) --- tests/ChecksLaravelVersionTrait.php | 29 +++- tests/Stubs/Application8Stub.php | 199 ++++++++++++++++++++++++++++ 2 files changed, 222 insertions(+), 6 deletions(-) create mode 100644 tests/Stubs/Application8Stub.php diff --git a/tests/ChecksLaravelVersionTrait.php b/tests/ChecksLaravelVersionTrait.php index 9667fb56..2a692bff 100644 --- a/tests/ChecksLaravelVersionTrait.php +++ b/tests/ChecksLaravelVersionTrait.php @@ -2,25 +2,40 @@ namespace Dingo\Api\Tests; -use Dingo\Api\Tests\Stubs\Application7Stub; use Dingo\Api\Tests\Stubs\ApplicationStub; +use Dingo\Api\Tests\Stubs\Application8Stub; +use Dingo\Api\Tests\Stubs\Application7Stub; use Dingo\Api\Tests\Stubs\Application6Stub; use Dingo\Api\Tests\Stubs\Application58Stub; trait ChecksLaravelVersionTrait { public $installed_file_path = __DIR__.'/../vendor/composer/installed.json'; - public $current_release = '7.0'; + public $current_release = '8.0'; private function getFrameworkVersion() { $contents = file_get_contents($this->installed_file_path); $parsed_data = json_decode($contents, true); - $just_laravel = array_filter($parsed_data, function ($val) { - if ('laravel/framework' === $val['name'] || 'laravel/lumen-framework' === $val['name']) { - return true; + + // Changed array format in newer versions of composer (v2?) + if (array_key_exists('packages', $parsed_data)) { + $parsed_data = $parsed_data['packages']; + } + + // Find laravel/framework or lumen package + $just_laravel = array_filter($parsed_data, function ($composerPackageData) { + if (is_array($composerPackageData) && array_key_exists('name', $composerPackageData)) { + if ('laravel/framework' === $composerPackageData['name'] || 'laravel/lumen-framework' === $composerPackageData['name']) { + return true; + } } }); + + if (empty($just_laravel)) { + die (PHP_EOL . 'No Laravel version detected, please do a "composer require laravel/framework:x" prior to testing' . PHP_EOL); + } + $laravelVersion = array_map(function ($val) { return $val['version']; }, array_values($just_laravel))[0]; @@ -39,7 +54,9 @@ private function getApplicationStub() $version = str_replace('v', '', $version); // Return the version stub for the right version - if (version_compare($version, '7.0.0', '>=')) { + if (version_compare($version, '8.0.0', '>=')) { + return new Application8Stub; + } else if (version_compare($version, '7.0.0', '>=')) { return new Application7Stub; } elseif (version_compare($version, '6.0.0', '>=')) { return new Application6Stub; diff --git a/tests/Stubs/Application8Stub.php b/tests/Stubs/Application8Stub.php new file mode 100644 index 00000000..0c70ce69 --- /dev/null +++ b/tests/Stubs/Application8Stub.php @@ -0,0 +1,199 @@ + Date: Fri, 30 Apr 2021 21:56:08 +1000 Subject: [PATCH 2/2] cs --- tests/ChecksLaravelVersionTrait.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/ChecksLaravelVersionTrait.php b/tests/ChecksLaravelVersionTrait.php index 2a692bff..20a14dc0 100644 --- a/tests/ChecksLaravelVersionTrait.php +++ b/tests/ChecksLaravelVersionTrait.php @@ -33,7 +33,7 @@ private function getFrameworkVersion() }); if (empty($just_laravel)) { - die (PHP_EOL . 'No Laravel version detected, please do a "composer require laravel/framework:x" prior to testing' . PHP_EOL); + exit(PHP_EOL.'No Laravel version detected, please do a "composer require laravel/framework:x" prior to testing'.PHP_EOL); } $laravelVersion = array_map(function ($val) { @@ -56,7 +56,7 @@ private function getApplicationStub() // Return the version stub for the right version if (version_compare($version, '8.0.0', '>=')) { return new Application8Stub; - } else if (version_compare($version, '7.0.0', '>=')) { + } elseif (version_compare($version, '7.0.0', '>=')) { return new Application7Stub; } elseif (version_compare($version, '6.0.0', '>=')) { return new Application6Stub;