Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Behat dependency injection is not working, replaced by Laravel's DI #84

Open
tentacode opened this issue Mar 22, 2019 · 1 comment
Open

Comments

@tentacode
Copy link

tentacode commented Mar 22, 2019

Hello there ! Just a little thing that bugs me, you can't use Behat's own dependency injection system (it's not documented a lot so not a lot of person are using it) you can learn more about it here :

So, for example, this does not work :

    suites:
        default:
            contexts:
                - Contexts\Api\JsonContext:
                    - '@api_client'
            services:
                api_client: 
                    class: "Contexts\\Api\\Service\\ApiClient"
                    arguments: ['http://127.0.0.1:8000']

And produce the error :

In LaravelArgumentResolver.php line 48:

  [ErrorException]
  substr() expects parameter 1 to be string, object given

Exception trace:
 () at /Users/gabriel/Workspace/taskl/vendor/laracasts/behat-laravel-extension/src/Context/Argument/LaravelArgumentResolver.php:48
 Illuminate\Foundation\Bootstrap\HandleExceptions->handleError() at n/a:n/a

This is because LaravelArgumentResolver only resolve argument "Laravel way" and don't care if the argument has aleady be resolved by Behat (which is the case here). This can be solved by changing the behat.yml to :

    suites:
        default:
            contexts:
                - Contexts\Api\JsonContext:
                    - '@Contexts\Api\Service\ApiClient'

But this forces us to use Laravel DI (and I can't set constructor arguments in behat.yml this way).

This is probably an easy fix, I could do it if I take the time, at least it's documented now :) Thanks for the good work.

@bakome
Copy link

bakome commented Sep 22, 2020

Easy quick fix is to add __toString method in service that is injected. That will avoid substr error. Ex.

public function __toString(): string { return self::class; }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants