简体   繁体   English

Lumen PHPUNIT 不使用 .env.testing

[英]Lumen PHPUNIT not using the .env.testing

I am having an issue in Lumen with testing.我在 Lumen 中遇到了测试问题。 I have my phpunit.xml as such:我有我的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>

And I have a .env.testing file as well.我也有一个 .env.testing 文件。 But when I run vendor/bin/phpunit tests/ , it's using the main .env and not the .env.testing .但是当我运行vendor/bin/phpunit tests/ ,它使用主.env而不是.env.testing

Also my bootstrap/app.php being loaded from test/TestCase.php is as such:另外我从test/TestCase.php加载的bootstrap/app.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;

Any ideas?有任何想法吗?

For my Lumen project I edited app.php and used this to load the environment files instead of the default code:对于我的 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 will load first, then .env.<envName> will add and override it. .env将首先加载,然后.env.<envName>将添加并覆盖它。

The code isn't as simple as I'd like and relies on knowledge of Lumen's internals, but it's the best solution I could come up with.代码并不像我想的那么简单,并且依赖于 Lumen 内部的知识,但它是我能想出的最佳解决方案。


My first attempt looked like the code below, but it only loads the first file it finds due to the shortCircuit parameter that I override above.我的第一次尝试看起来像下面的代码,但它只加载它找到的第一个文件,因为我在上面覆盖了shortCircuit参数。

(new Laravel\Lumen\Bootstrap\LoadEnvironmentVariables(
    dirname(__DIR__),
    [
        '.env.' . env('APP_ENV'),
        '.env',
    ]
))->bootstrap();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM