FriendsOfBehat / SymfonyExtension

:musical_score: Extension integrating Behat with Symfony.
MIT License
472 stars 62 forks source link

Dependency injection not working fresh install #133

Open kennaar opened 3 years ago

kennaar commented 3 years ago

Hi,

I'm trying to install this package but I get an error at the first step already. I should mention that I install this package through a symlink (path repository). The steps I'm doing:

# path repository
composer require --dev friends-of-behat/symfony-extension:^2.0

# actual application
composer require --dev friends-of-behat/symfony-extension:*@dev
# which outputs something like this:
# ...
# Installing behat/behat (dev-master): Symlinking from ../packages/vendor/behat/behat
# Installing friends-of-behat/symfony-extension (dev-master): Symlinking from ../packages/vendor/friends-of-behat/symfony-extension
# ...

I allow the contrib recipe to be executed.

$ vendor/bin/behat

Feature:
    In order to prove that the Behat Symfony extension is correctly installed
    As a user
    I want to have a demo scenario

In Validator.php line 61:

  Can not find a matching value for an argument `$kernel` of the method `App\Tests\Behat\DemoContext::__construct()`.

I'm a bit stuck here because installing the "official" behat symfony extension also doesn't work because I'm using Symfony 5.1. Thanks in advance.

onatskyy commented 3 years ago

the same problem

unixslayer commented 3 years ago

@knnnrd had the same problem #132

All I did was to change context class constructor. Can you be more specific on what you are doing? Please paste your container configuration services_test.yaml and your context class. Also behat.yaml configuration will be helpful.

kennaar commented 3 years ago

@knnnrd had the same problem #132

All I did was to change context class constructor. Can you be more specific on what you are doing? Please paste your container configuration services_test.yaml and your context class. Also behat.yaml configuration will be helpful.

I haven't "done" anything yet except for installing the package and trying to run the example feature file.

config/services_test.yaml

services:
    _defaults:
        autowire: true
        autoconfigure: true

    App\Tests\Behat\:
        resource: '../tests/Behat/*'

tests/Behat/DemoContext.php

<?php

declare(strict_types=1);

namespace App\Tests\Behat;

use Behat\Behat\Context\Context;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\KernelInterface;

/**
 * This context class contains the definitions of the steps used by the demo
 * feature file. Learn how to get started with Behat and BDD on Behat's website.
 *
 * @see http://behat.org/en/latest/quick_start.html
 */
final class DemoContext implements Context
{
    /** @var KernelInterface */
    private $kernel;

    /** @var Response|null */
    private $response;

    public function __construct(KernelInterface $kernel)
    {
        $this->kernel = $kernel;
    }

    /**
     * @When a demo scenario sends a request to :path
     */
    public function aDemoScenarioSendsARequestTo(string $path): void
    {
        $this->response = $this->kernel->handle(Request::create($path, 'GET'));
    }

    /**
     * @Then the response should be received
     */
    public function theResponseShouldBeReceived(): void
    {
        if ($this->response === null) {
            throw new \RuntimeException('No response received');
        }
    }
}

behat.yml.dist

default:
    suites:
        default:
            contexts:
                - App\Tests\Behat\DemoContext

    extensions:
        FriendsOfBehat\SymfonyExtension: null

features/demo.feature

# This file contains a user story for demonstration only.
# Learn how to get started with Behat and BDD on Behat's website:
# http://behat.org/en/latest/quick_start.html

Feature:
    In order to prove that the Behat Symfony extension is correctly installed
    As a user
    I want to have a demo scenario

    Scenario: It receives a response from Symfony's kernel
        When a demo scenario sends a request to "/"
        Then the response should be received
kennaar commented 3 years ago

@unixslayer Any idea?

unixslayer commented 3 years ago

@knnnrd sorry for delay.

My current configuration looks like this

;behat.yaml.dist
default:
    suites:
        default:
            contexts:
                - behatch:context:rest
                - behatch:context:json
                - behatch:context:debug
                - Behat\MinkExtension\Context\MinkContext
                - Acme\Behat\Context\MessengerContext

    extensions:
        Behat\MinkExtension:
            sessions:
                default:
                    symfony: ~
        Behatch\Extension: ~
        FriendsOfBehat\SymfonyExtension:
            bootstrap: 'features/bootstrap.php'

;config/services_test.yaml
services:
    _defaults:
        autowire: true
        autoconfigure: true

    Acme\Behat\:
        resource: '../src/Acme/Behat'

I haven't done anything really and everything works just fine. But what I've noticed is that Symfony cache is not rebuild after running Behat when something got change. For example, when I create new route, I have to manually remove cache rm -rf var/cache/* or feature that uses new route will always get 404. Perhaps this is a cache issue. Didn't have much time to investigate yet.

kennaar commented 3 years ago

@unixslayer Unfortunately clearing the cache also doesn't help. I tried installing all the extensions that you also have (some weird dependency maybe?), but still the same issue. I will try setting up a completely new and fresh symfony project with this library and see if the issue is also there.

kennaar commented 3 years ago

No problem at all in a fresh symfony 5.1 project. I'm kinda at a loss here on what to do. I can't show my project since it's closed-source. I haven't deviated that much I think from a standard Symfony/API Platform installation. Any more ideas?

EVDW commented 3 years ago

I don't know if it can be useful, but I had this kind of issue last time and I solved it with this conf: behat.yml.dist

FriendsOfBehat\SymfonyExtension:
    kernel:
        path: src/Kernel.php
        class: App\Kernel
        environment: test

services_test

services:
    Symfony\Bundle\FrameworkBundle\Test\TestContainer: '@test.service_container'

    _defaults:
        public: true
        autowire: true
        autoconfigure: true

    Context\:
        resource: '../features/Context/*Context.php'

(my Contexts use TestContainer in their construct)

kennaar commented 3 years ago

@EVDW Thanks for your input but sadly the same error

hulkthedev commented 2 years ago

I have this error too, just after installing the recipe. Doesn't the developer want to solve it? There are some with this bug here... The problem on my mashine is, the services_test.yml is not read by the framework because Behat runs in dev mode, not test.

I moved this configuration App\Tests\Behat\: resource: '../tests/Behat/*' to services.yml and it works.

mpdude commented 1 year ago

    /**
     * @When a demo scenario sends a request to :path
     */
    public function aDemoScenarioSendsARequestTo(string $path): void
    {
        $this->response = $this->kernel->handle(Request::create($path, 'GET'));
    }

This is not how this is supposed to be used.

The $kernel instance here is the one being used to autowire your context classes, but for making requests against your application you should use the Mink driver included in this package here.

toby-griffiths commented 4 months ago

I have this error too, just after installing the recipe. Doesn't the developer want to solve it? There are some with this bug here... The problem on my mashine is, the services_test.yml is not read by the framework because Behat runs in dev mode, not test.

I moved this configuration App\Tests\Behat\: resource: '../tests/Behat/*' to services.yml and it works.

Please remember that "the developer" is actually "any developer", since this is an open source, so you're always welcome to contribute to fix things If you ask questions, the lovely people here, I'm sure, will help guide you.

However, I might have the solution.

I was also having this issue on a project, so I took the time to spin up a sandbox environment with a fresh symfony install, installed Behat & this extension and it worked, so then Xdebugged the 2 side by side, and found that the problem was that they DemoContext was not being found in my service container, so wasn't being autowired.

The reason… I had converted my services.yaml file to a services.php file, which apparently means that my services_test.yaml file was not being loaded. As soon as I converted my services_test.yaml file to a services_test.php file it worked again.

I hope this info helps all you others who might still be having this problem 🍀