简体   繁体   中英

Under Laravel Phpunit, BindingResolutionException is thrown when calling Illuminate config() helper inside Guzzle Promise

I received BindingResolutionException() when calling the global helper function config() under Guzzle Promise inside a controller.

It seems that the Illuminate helpers.php is not loaded under the Guzzle Promise when running phpunit. Is it a bug or did I misuse something?

The following is my code. (This is a new Laravel project)

Script

./vendor/phpunit/phpunit/phpunit tests/MyTest.php

Exception

Illuminate\Contracts\Container\BindingResolutionException {#448
  #message: "Target class [config] does not exist."
  #code: 0
  #file: "./vendor/laravel/framework/src/Illuminate/Container/Container.php"
  #line: 809
  -previous: ReflectionException {#540
    #message: "Class config does not exist"
    #code: -1
    #file: "./vendor/laravel/framework/src/Illuminate/Container/Container.php"
    #line: 807
    trace: {
      ./vendor/laravel/framework/src/Illuminate/Container/Container.php:807 { …}
      ./vendor/laravel/framework/src/Illuminate/Container/Container.php:685 { …}
      ./vendor/laravel/framework/src/Illuminate/Foundation/Application.php:794 { …}
      ./vendor/laravel/framework/src/Illuminate/Container/Container.php:633 { …}
      ./vendor/laravel/framework/src/Illuminate/Foundation/Application.php:779 { …}
      ./vendor/laravel/framework/src/Illuminate/Foundation/helpers.php:119 { …}
      ./vendor/laravel/framework/src/Illuminate/Foundation/helpers.php:275 { …}
      ./app/Http/Controllers/Controller.php:22 {
        App\Http\Controllers\Controller->App\Http\Controllers\{closure}
        › try {
        ›     dump(config('app.env'));
        › } catch (\Exception $e) {
        arguments: {
          $key: "app.env"
        }
      }
      ./vendor/guzzlehttp/promises/src/FulfilledPromise.php:39 { …}
      ./vendor/guzzlehttp/promises/src/TaskQueue.php:47 { …}
      ./vendor/guzzlehttp/promises/src/TaskQueue.php:26 { …}
      GuzzleHttp\Promise\TaskQueue->GuzzleHttp\Promise\{closure}() {}
    }
  }
  trace: {
    ./vendor/laravel/framework/src/Illuminate/Container/Container.php:809 { …}
    ./vendor/laravel/framework/src/Illuminate/Container/Container.php:685 { …}
    ./vendor/laravel/framework/src/Illuminate/Foundation/Application.php:794 { …}
    ./vendor/laravel/framework/src/Illuminate/Container/Container.php:633 { …}
    ./vendor/laravel/framework/src/Illuminate/Foundation/Application.php:779 { …}
    ./vendor/laravel/framework/src/Illuminate/Foundation/helpers.php:119 { …}
    ./vendor/laravel/framework/src/Illuminate/Foundation/helpers.php:275 { …}
    ./app/Http/Controllers/Controller.php:22 {
      App\Http\Controllers\Controller->App\Http\Controllers\{closure}
      › try {
      ›     dump(config('app.env'));
      › } catch (\Exception $e) {
      arguments: {
        $key: "app.env"
      }
    }
    ./vendor/guzzlehttp/promises/src/FulfilledPromise.php:39 { …}
    ./vendor/guzzlehttp/promises/src/TaskQueue.php:47 { …}
    ./vendor/guzzlehttp/promises/src/TaskQueue.php:26 { …}
    GuzzleHttp\Promise\TaskQueue->GuzzleHttp\Promise\{closure}() {}
  }
}

Controller

...
    public function index()
    {
        dump(config('app.env'));

        $promise = new FulfilledPromise(null);
        $promise->then(function () {
            try {
                dump(config('app.env'));
            } catch (\Exception $e) {
                dump($e);
            }
        });
        return response('ok', 200);
    }
...

Test

...
    public function testExample()
    {
        $response = $this->get('/');

        $response->assertStatus(200);
    }
...

composer.json

...
    "require": {
        "php": "^7.2.5",
        "fideloper/proxy": "^4.2",
        "fruitcake/laravel-cors": "^1.0",
        "guzzlehttp/guzzle": "^6.3",
        "laravel/framework": "^7.0",
        "laravel/tinker": "^2.0"
    },
    "require-dev": {
        "facade/ignition": "^2.0",
        "fzaninotto/faker": "^1.9.1",
        "mockery/mockery": "^1.3.1",
        "nunomaduro/collision": "^4.1",
        "phpunit/phpunit": "^8.5"
    },
...
    "autoload": {
        "psr-4": {
            "App\\": "app/"
        },
        "classmap": [
            "database/seeds",
            "database/factories"
        ]
    },
    "autoload-dev": {
        "psr-4": {
            "Tests\\": "tests/"
        }
    },

web.php

...
Route::get('/', 'Controller@index');

I met this problem recently and it was due to a permission problem. phpunit need to have write perms on .phpunit.result.cache (at least).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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