orchestral / testbench

Laravel Testing Helper for Packages Development
https://packages.tools/testbench
MIT License
2.1k stars 136 forks source link

BindingResolutionException : Unresolvable dependency resolving [Parameter #0 [ <required> string or NULL $apiToken ]] in class Facade\FlareClient\Http\Client #322

Closed Stevemoretz closed 2 years ago

Stevemoretz commented 2 years ago

Description:

throwing any exception in any route would cause the following errors.

Illuminate\Contracts\Container\BindingResolutionException : Unresolvable dependency resolving [Parameter #0 [ <required> string or NULL $apiToken ]] in class Facade\FlareClient\Http\Client
 /Volumes/HDD/Websites/wordpress-clean/wp-content/mu-plugins/fast/Laravel/packages/stevemoretz/push/vendor/laravel/framework/src/Illuminate/Container/Container.php:1108
 /Volumes/HDD/Websites/wordpress-clean/wp-content/mu-plugins/fast/Laravel/packages/stevemoretz/push/vendor/laravel/framework/src/Illuminate/Container/Container.php:1015
 /Volumes/HDD/Websites/wordpress-clean/wp-content/mu-plugins/fast/Laravel/packages/stevemoretz/push/vendor/laravel/framework/src/Illuminate/Container/Container.php:950
 /Volumes/HDD/Websites/wordpress-clean/wp-content/mu-plugins/fast/Laravel/packages/stevemoretz/push/vendor/laravel/framework/src/Illuminate/Container/Container.php:912
 /Volumes/HDD/Websites/wordpress-clean/wp-content/mu-plugins/fast/Laravel/packages/stevemoretz/push/vendor/laravel/framework/src/Illuminate/Container/Container.php:758
 /Volumes/HDD/Websites/wordpress-clean/wp-content/mu-plugins/fast/Laravel/packages/stevemoretz/push/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:841
 /Volumes/HDD/Websites/wordpress-clean/wp-content/mu-plugins/fast/Laravel/packages/stevemoretz/push/vendor/laravel/framework/src/Illuminate/Container/Container.php:694
 /Volumes/HDD/Websites/wordpress-clean/wp-content/mu-plugins/fast/Laravel/packages/stevemoretz/push/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:826
 /Volumes/HDD/Websites/wordpress-clean/wp-content/mu-plugins/fast/Laravel/packages/stevemoretz/push/vendor/laravel/framework/src/Illuminate/Container/Container.php:1031
 /Volumes/HDD/Websites/wordpress-clean/wp-content/mu-plugins/fast/Laravel/packages/stevemoretz/push/vendor/laravel/framework/src/Illuminate/Container/Container.php:951
 /Volumes/HDD/Websites/wordpress-clean/wp-content/mu-plugins/fast/Laravel/packages/stevemoretz/push/vendor/laravel/framework/src/Illuminate/Container/Container.php:912
 /Volumes/HDD/Websites/wordpress-clean/wp-content/mu-plugins/fast/Laravel/packages/stevemoretz/push/vendor/laravel/framework/src/Illuminate/Container/Container.php:758
 /Volumes/HDD/Websites/wordpress-clean/wp-content/mu-plugins/fast/Laravel/packages/stevemoretz/push/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:841
 /Volumes/HDD/Websites/wordpress-clean/wp-content/mu-plugins/fast/Laravel/packages/stevemoretz/push/vendor/laravel/framework/src/Illuminate/Container/Container.php:694
 /Volumes/HDD/Websites/wordpress-clean/wp-content/mu-plugins/fast/Laravel/packages/stevemoretz/push/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:826
 /Volumes/HDD/Websites/wordpress-clean/wp-content/mu-plugins/fast/Laravel/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php:119
 /Volumes/HDD/Websites/wordpress-clean/wp-content/mu-plugins/fast/Laravel/packages/stevemoretz/push/vendor/spatie/laravel-ray/src/Watchers/ExceptionWatcher.php:66
 /Volumes/HDD/Websites/wordpress-clean/wp-content/mu-plugins/fast/Laravel/packages/stevemoretz/push/vendor/spatie/laravel-ray/src/Watchers/ExceptionWatcher.php:35
 /Volumes/HDD/Websites/wordpress-clean/wp-content/mu-plugins/fast/Laravel/packages/stevemoretz/push/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php:404
 /Volumes/HDD/Websites/wordpress-clean/wp-content/mu-plugins/fast/Laravel/packages/stevemoretz/push/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php:249
 /Volumes/HDD/Websites/wordpress-clean/wp-content/mu-plugins/fast/Laravel/packages/stevemoretz/push/vendor/laravel/framework/src/Illuminate/Log/Logger.php:245
 /Volumes/HDD/Websites/wordpress-clean/wp-content/mu-plugins/fast/Laravel/packages/stevemoretz/push/vendor/laravel/framework/src/Illuminate/Log/Logger.php:186
 /Volumes/HDD/Websites/wordpress-clean/wp-content/mu-plugins/fast/Laravel/packages/stevemoretz/push/vendor/laravel/framework/src/Illuminate/Log/Logger.php:94
 /Volumes/HDD/Websites/wordpress-clean/wp-content/mu-plugins/fast/Laravel/packages/stevemoretz/push/vendor/laravel/framework/src/Illuminate/Log/LogManager.php:583
 /Volumes/HDD/Websites/wordpress-clean/wp-content/mu-plugins/fast/Laravel/packages/stevemoretz/push/vendor/laravel/framework/src/Illuminate/Foundation/Exceptions/Handler.php:252
 /Volumes/HDD/Websites/wordpress-clean/wp-content/mu-plugins/fast/Laravel/packages/stevemoretz/push/vendor/orchestra/testbench-core/src/Exceptions/Handler.php:40
 /Volumes/HDD/Websites/wordpress-clean/wp-content/mu-plugins/fast/Laravel/packages/stevemoretz/push/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php:415
 /Volumes/HDD/Websites/wordpress-clean/wp-content/mu-plugins/fast/Laravel/packages/stevemoretz/push/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php:113
 /Volumes/HDD/Websites/wordpress-clean/wp-content/mu-plugins/fast/Laravel/packages/stevemoretz/push/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php:510
 /Volumes/HDD/Websites/wordpress-clean/wp-content/mu-plugins/fast/Laravel/packages/stevemoretz/push/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php:476
 /Volumes/HDD/Websites/wordpress-clean/wp-content/mu-plugins/fast/Laravel/packages/stevemoretz/push/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php:335
 /Volumes/HDD/Websites/wordpress-clean/wp-content/mu-plugins/fast/Laravel/packages/stevemoretz/push/tests/Unit/Individual/IndividualTest.php:21
 /Volumes/HDD/Websites/wordpress-clean/wp-content/mu-plugins/fast/Laravel/vendor/phpunit/phpunit/src/TextUI/Command.php:143
 /Volumes/HDD/Websites/wordpress-clean/wp-content/mu-plugins/fast/Laravel/vendor/phpunit/phpunit/src/TextUI/Command.php:96

Steps To Reproduce:

Define the route:

Route::post("/subscribe", function () {
      throw new \Exception('test');
}

Call it in a test.

 $response = $this->postJson("/subscribe", []);

after some debugging I found that the error is happening because of this line:

/vendor/spatie/laravel-ray/src/Watchers/ExceptionWatcher.php:66

which is

        $flare = app(Flare::class);

doing the same thing in the test without routes gives the same error.

Stevemoretz commented 2 years ago

How are you even supposed to get that exception if you don't have ray anyways?

crynobone commented 2 years ago

This doesn't occur in normal package development. I not going to debug it when the Testbench own tests work fine. Our CI doesn't have ray installed and it doesn't failed.

Stevemoretz commented 2 years ago

What do you mean by normal package development? Am I doing something abnormal?

Stevemoretz commented 2 years ago

I have to add IgnitionServiceProvider::class to getPackageProviders in there the flare gets instantiated in the container. But if you don't add that, it gives the above error, please explain why this happens, and why isn't this documented?


Just put this in a test app(Flare::class) and you'll see that your test will fail.

crynobone commented 2 years ago

I have to add IgnitionServiceProvider::class to getPackageProviders in there the flare gets instantiated in the container.

If you want to add that, then you need to handle whatever dependency it requires.

Just put this in a test app(Flare::class) and you'll see that your test will fail.

Testbench doesn't require this. If you add a code that would failed with Testbench it not my responsibility to fix it.

/vendor/spatie/laravel-ray/src/Watchers/ExceptionWatcher.php:66

        if (! class_exists(Flare::class)) {
            return null;
        }

        /** @var \Facade\FlareClient\Flare $flare */
        $flare = app(Flare::class);

You purposely left out the condition before line 66?

Stevemoretz commented 2 years ago

Thank you for the response, the problem occurs in the testbench itself, that's why I created the issue.

You purposely left out the condition before line 66?

The condition is there, however since Laravel includes the Flare::class that passes that condition and won't return null, I installed the latest version of Laravel yesterday, since you suggested that I leave lumen, in another issue, and the Flare::class is included in the latest version, (I'm not sure if it was also included before the latest version or not.)

//add this to your test
dd(class_exists(Flare::class)); //returns true

That's why this error occurs, however if you take a look at IgnitionServiceProvider.php:272 you'll face this

        $this->app->singleton(Flare::class, function () {
            $client = new Flare($this->app->get('flare.http'), new LaravelContextDetector(), $this->app);
            $client->applicationPath(base_path());
            $client->stage(config('app.env'));

            return $client;
        });

So only by adding IgnitionServiceProvider.php I could pass the test without problems.


To sum it up, using this route:

Route::post("/subscribe", function () {
      throw new \Exception('test');
}

and then calling it:

 $response = $this->postJson("/subscribe", []);

on the latest version of laravel, you'll receive this error:

Illuminate\Contracts\Container\BindingResolutionException : Unresolvable dependency resolving [Parameter #0 [ <required> string or NULL $apiToken ]] in class Facade\FlareClient\Http\Client

Which is because of the app(Flare::class) at /vendor/spatie/laravel-ray/src/Watchers/ExceptionWatcher.php:66 which is a dependency of testbench library, and that causes it to fail.

It probably isn't still your responsibility, because it is probably a fault of ray library but well, I thought you might wanna know about this bug, because you use that library as a dependency, this could have been fixed in a newer version though you might want to test that . (I checked ray has newer versions that you have used available)