Lumen PHPUNIT 不使用 .env.testing
Posted
技术标签:
【中文标题】Lumen PHPUNIT 不使用 .env.testing【英文标题】:Lumen PHPUNIT not using the .env.testing 【发布时间】:2020-05-23 19:40:37 【问题描述】:我在 Lumen 中遇到了测试问题。我有我的phpunit.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="Application Test Suite">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./app</directory>
</whitelist>
</filter>
<php>
<env name="APP_ENV" value="testing"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="QUEUE_CONNECTION" value="sync"/>
</php>
</phpunit>
我也有一个 .env.testing 文件。但是当我运行vendor/bin/phpunit tests/
时,它使用的是主.env
而不是.env.testing
。
我的bootstrap/app.php
也是从test/TestCase.php
加载的:
<?php
require_once __DIR__.'/../vendor/autoload.php';
(new Laravel\Lumen\Bootstrap\LoadEnvironmentVariables(
dirname(__DIR__)
))->bootstrap();
/*
|--------------------------------------------------------------------------
| Create The Application
|--------------------------------------------------------------------------
|
| Here we will load the environment and create the application instance
| that serves as the central piece of this framework. We'll use this
| application as an "IoC" container and router for this framework.
|
*/
$app = new Laravel\Lumen\Application(
dirname(__DIR__)
);
$app->withFacades();
$app->withEloquent();
/*
|--------------------------------------------------------------------------
| Register Container Bindings
|--------------------------------------------------------------------------
|
| Now we will register a few bindings in the service container. We will
| register the exception handler and the console kernel. You may add
| your own bindings here if you like or you can make another file.
|
*/
$app->singleton(
Illuminate\Contracts\Debug\ExceptionHandler::class,
App\Exceptions\Handler::class
);
$app->singleton(
Illuminate\Contracts\Console\Kernel::class,
App\Console\Kernel::class
);
$app->instance('path.config', app()->basePath() . DIRECTORY_SEPARATOR . 'config');
/*
|--------------------------------------------------------------------------
| Register Middleware
|--------------------------------------------------------------------------
|
| Next, we will register the middleware with the application. These can
| be global middleware that run before and after each request into a
| route or middleware that'll be assigned to some specific routes.
|
*/
// $app->middleware([
// App\Http\Middleware\ExampleMiddleware::class
// ]);
$app->routeMiddleware([
'auth' => App\Http\Middleware\Authenticate::class,
'client' => \Laravel\Passport\Http\Middleware\CheckClientCredentials::class,
]);
$app->middleware([
Spatie\Cors\Cors::class,
]);
$app->configure('cors');
$app->configure('lighthouse');
$app->configure('graphql-playground');
/*
|--------------------------------------------------------------------------
| Register Service Providers
|--------------------------------------------------------------------------
|
| Here we will register all of the application's service providers which
| are used to bind services into the container. Service providers are
| totally optional, so you are not required to uncomment this line.
|
*/
// $app->register(App\Providers\AuthServiceProvider::class);
// $app->register(App\Providers\EventServiceProvider::class);
$app->register(App\Providers\AppServiceProvider::class);
$app->register(Illuminate\Mail\MailServiceProvider::class);
$app->register(Laravel\Passport\PassportServiceProvider::class);
$app->register(Dusterio\LumenPassport\PassportServiceProvider::class);
$app->register(Laravel\Tinker\TinkerServiceProvider::class);
$app->register(Illuminate\Notifications\NotificationServiceProvider::class);
$app->register(MLL\GraphQLPlayground\GraphQLPlaygroundServiceProvider::class);
$app->register(Appzcoder\LumenRoutesList\RoutesCommandServiceProvider::class);
$app->register(Spatie\Cors\CorsServiceProvider::class);
\Dusterio\LumenPassport\LumenPassport::routes($app, ['prefix' => 'api/v1/oauth']);
$app->withFacades(true, [
'Illuminate\Support\Facades\Notification' => 'Notification',
]);
/*
|--------------------------------------------------------------------------
| Load The Application Configuration Files
|--------------------------------------------------------------------------
*/
collect(scandir(__DIR__ . '/../config'))->each(function ($item) use ($app)
$app->configure(basename($item, '.php'));
);
$app->alias('mailer', Illuminate\Mail\Mailer::class);
$app->alias('mailer', Illuminate\Contracts\Mail\Mailer::class);
$app->alias('mailer', Illuminate\Contracts\Mail\MailQueue::class);
/*
|--------------------------------------------------------------------------
| Load The Application Routes
|--------------------------------------------------------------------------
|
| Next we will include the routes file so that they can all be added to
| the application. This will provide all of the URLs the application
| can respond to, as well as the controllers that may handle them.
|
*/
$app->router->group([
'namespace' => 'App\Http\Controllers',
], function ($router)
require __DIR__.'/../routes/web.php';
);
return $app;
有什么想法吗?
【问题讨论】:
你使用的是什么版本的 Lumen? @Rwd im 使用 Lumen 6 这能回答你的问题吗? (见博格丹的回答)How to specify a different .env file for phpunit in Laravel 5? 【参考方案1】:对于我的 Lumen 项目,我编辑了 app.php
并使用它来加载环境文件而不是默认代码:
use Dotenv\Dotenv;
use Illuminate\Support\Env;
// ... //
(new class(
dirname(__DIR__),
[
'.env',
'.env.' . env('APP_ENV'),
])
extends Laravel\Lumen\Bootstrap\LoadEnvironmentVariables
protected function createDotenv()
return Dotenv::create(
Env::getRepository(),
$this->filePath,
$this->fileName,
false // disable the short circuit
);
)->bootstrap();
.env
将首先加载,然后.env.<envName>
将添加并覆盖它。
代码并不像我想的那么简单,它依赖于 Lumen 内部的知识,但它是我能想到的最佳解决方案。
我的第一次尝试看起来像下面的代码,但由于我在上面覆盖了shortCircuit
参数,它只加载找到的第一个文件。
(new Laravel\Lumen\Bootstrap\LoadEnvironmentVariables(
dirname(__DIR__),
[
'.env.' . env('APP_ENV'),
'.env',
]
))->bootstrap();
【讨论】:
以上是关于Lumen PHPUNIT 不使用 .env.testing的主要内容,如果未能解决你的问题,请参考以下文章