Skip to content

Commit

Permalink
Improve laravel 8 testing (application stub & version detection)
Browse files Browse the repository at this point in the history
  • Loading branch information
specialtactics committed Apr 30, 2021
1 parent 0f3394d commit cdade69
Show file tree
Hide file tree
Showing 2 changed files with 222 additions and 6 deletions.
29 changes: 23 additions & 6 deletions tests/ChecksLaravelVersionTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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;
Expand Down
199 changes: 199 additions & 0 deletions tests/Stubs/Application8Stub.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
<?php

namespace Dingo\Api\Tests\Stubs;

use Illuminate\Container\Container;
use Illuminate\Contracts\Foundation\Application;

class Application8Stub extends Container implements Application
{
public function version()
{
return 'v1';
}

public function basePath($path = '')
{
//
}

public function bootstrapPath($path = '')
{
//
}

public function configPath($path = '')
{
//
}

public function databasePath($path = '')
{
//
}

public function environmentPath()
{
//
}

public function resourcePath($path = '')
{
//
}

public function storagePath()
{
//
}

public function environment(...$environments)
{
return 'testing';
}

public function runningInConsole()
{
//
}

public function runningUnitTests()
{
// TODO: Implement runningUnitTests() method.
}

public function isDownForMaintenance()
{
return false;
}

public function registerConfiguredProviders()
{
//
}

public function register($provider, $options = [], $force = false)
{
//
}

public function registerDeferredProvider($provider, $service = null)
{
//
}

public function resolveProvider($provider)
{
//
}

public function boot()
{
//
}

public function booting($callback)
{
//
}

public function booted($callback)
{
//
}

public function bootstrapWith(array $bootstrappers)
{
//
}

public function configurationIsCached()
{
//
}

public function detectEnvironment(\Closure $callback)
{
//
}

public function environmentFile()
{
//
}

public function environmentFilePath()
{
//
}

public function getCachedConfigPath()
{
//
}

public function getCachedServicesPath()
{
//
}

public function getCachedPackagesPath()
{
//
}

public function getCachedRoutesPath()
{
//
}

public function getLocale()
{
//
}

public function getNamespace()
{
//
}

public function getProviders($provider)
{
//
}

public function hasBeenBootstrapped()
{
//
}

public function loadDeferredProviders()
{
//
}

public function loadEnvironmentFrom($file)
{
//
}

public function routesAreCached()
{
//
}

public function setLocale($locale)
{
//
}

public function shouldSkipMiddleware()
{
//
}

public function terminate()
{
//
}
}

0 comments on commit cdade69

Please sign in to comment.