Behat: Symfony 4 - context - dependency Injection - inject service

Created on 23 Sep 2018  路  8Comments  路  Source: Behat/Behat

Hi,
I spent 2 days on it and I'm getting crazy.
I'm trying to inject either kernel or container or any other service into a context.
However, my service (i.e '@kernel' ) is interpreted as the string '@kernel'.

I followed the rafal-muszynski tutorial (https://blog.rafalmuszynski.pl/how-to-configure-behat-with-symfony-4/) and I installed behat/symfony2-extension but I got the error:

PHP Warning: require_once(/var/www/html/symfony/tracking): failed to open stream: Success in /var/www/html/symfony/tracking/vendor/symfony/dependency-injection/ContainerBuilder.php on line 1099
PHP Fatal error: require_once(): Failed opening required '/var/www/html/symfony/tracking/' (include_path='.:/usr/share/php') in /var/www/html/symfony/tracking/vendor/symfony/dependency-injection/ContainerBuilder.php on line 1099

:warning: /var/www/html/symfony/tracking is my project directory

I've also noticed there's not any behat recipe for symfony 4.

So, what is the best approach to install behat on symfony 4 ? And how to inject service into context ?
Thanks for help

EDIT: following @timiTao suggestion, here are my files:

# behat.yml.dist
default:
    suites:
        default:
            contexts:
                - FeatureContext:
                    kernel: '@kernel'
    extensions:
        Behat\Symfony2Extension:
            kernel:
                class: App\Kernel

features/demo.feature

Feature: Demo
  Scenario: This is a scenario
    When I test Behat
    Then It should work

features/bootstrap/FeatureContext.php

<?php

namespace App\Behat\Context;

use Behat\Behat\Context\Context;
use Behat\Gherkin\Node\PyStringNode;
use Behat\Gherkin\Node\TableNode;
use Behat\Symfony2Extension\Context\KernelAwareContext;
use Behat\Symfony2Extension\Context\KernelDictionary;
use Symfony\Component\HttpKernel\KernelInterface;

/**
 * Defines application features from the specific context.
 */
class FeatureContext implements KernelAwareContext
{
    use KernelDictionary;

    /**
     * Initializes context.
     *
     * Every scenario gets its own context instance.
     * You can also pass arbitrary arguments to the
     * context constructor through behat.yml.
     */
    public function __construct(
        KernelInterface $kernel
    )
    {
        var_dump(get_class($kernel));
    }
}

Most helpful comment

Hi,
we ran in the same Fatal Error you formerly received as well:

Warning: require_once(/var/www/html): failed to open stream: Success in /var/www/html/vendor/symfony/dependency-injection/ContainerBuilder.php on line 1089

Fatal error: require_once(): Failed opening required '/var/www/html/' (include_path='.:/usr/local/lib/php') in /var/www/html/vendor/symfony/dependency-injection/ContainerBuilder.php on line 1089

Turned out we used the behat/symfony2-extension with dev-master stability.
"Downgrading" to 2.1.5 resolved the issue.

Regarding your parameter issue:
Maybe it's because you are missing the config entry to point the symfony2-extension to the bootstrap file, which basically only loads the .env file.

 extensions:
         Behat\Symfony2Extension:
             kernel:
                 bootstrap: features/bootstrap/bootstrap.php
                 class: App\Kernel

All 8 comments

There should be KernelAwareContext I guess. It's not worked out of the box - something should happen for Behat that it aware about Kernel.

@blixit You need show some code if you want help to reproduce it.
behat.yml, Behat version.

@spolischook @timiTao
I just edited my first post with my files.
For version,
./vendor/bin/behat -V gives me the same error I described sooner

but i guess I'm using "behat/behat": "^3.5@dev" (says my composer.json file)

I checked the vendor/behat and

  • I followed the 'testapp'
  • then I changed my FeatureContext content
//namespace App\Behat\Context;  // I removed namespace

use Behat\Behat\Context\Context;
use Behat\Gherkin\Node\PyStringNode;
use Behat\Gherkin\Node\TableNode;
use Behat\Symfony2Extension\Context\KernelAwareContext;
use Behat\Symfony2Extension\Context\KernelDictionary;
use Symfony\Component\HttpKernel\KernelInterface;

/**
 * Defines application features from the specific context.
 */
class FeatureContext implements KernelAwareContext
{

    private $kernel;
    private $containerParameters;
    private $parameterKey;

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

    /**
     * Sets Kernel instance.
     *
     * @param KernelInterface $kernel
     */
    public function setKernel(KernelInterface $kernel)
    {
        $this->kernel = $kernel;
    }
}

Now I have this error:

PHP Fatal error:  Uncaught TypeError: Argument 1 passed to FeatureContext::__construct() must implement interface Symfony\Component\HttpKernel\KernelInterface, string given in /var/www/html/symfony/tracking/features/bootstrap/FeatureContext.php:22
Stack trace:
#0 [internal function]: FeatureContext->__construct('@kernel')
#1 /var/www/html/symfony/tracking/vendor/behat/behat/src/Behat/Behat/Context/ContextFactory.php(133): ReflectionClass->newInstanceArgs(Array)
#2 /var/www/html/symfony/tracking/vendor/behat/behat/src/Behat/Behat/Context/ContextFactory.php(88): Behat\Behat\Context\ContextFactory->createInstance(Object(ReflectionClass), Array)
#3 /var/www/html/symfony/tracking/vendor/behat/behat/src/Behat/Behat/Context/Environment/Handler/ContextEnvironmentHandler.php(121): Behat\Behat\Context\ContextFactory->createContext('FeatureContext', Array, Array)
#4 /var/www/html/symfony/tracking/vendor/behat/behat/src/Behat/Testwork/Environment/EnvironmentManager.php(93): Behat\Behat\Context\Environment\Handler\ContextEnvironmentHan in /var/www/html/symfony/tracking/features/bootstrap/FeatureContext.php on line 22

I was inside the current function and I noticed that parameters starting with '@' are not resolved as symfony services:

public function createContext($class, array $arguments = array(), array $singleUseResolvers = array())
    {
        $reflection = new ReflectionClass($class);
        $resolvers = array_merge($singleUseResolvers, $this->argumentResolvers);
        $resolvedArguments = $this->resolveArguments($reflection, $arguments, $resolvers);
        $context = $this->createInstance($reflection, $resolvedArguments);
        $this->initializeInstance($context);

        return $context;
    }

Hi,
we ran in the same Fatal Error you formerly received as well:

Warning: require_once(/var/www/html): failed to open stream: Success in /var/www/html/vendor/symfony/dependency-injection/ContainerBuilder.php on line 1089

Fatal error: require_once(): Failed opening required '/var/www/html/' (include_path='.:/usr/local/lib/php') in /var/www/html/vendor/symfony/dependency-injection/ContainerBuilder.php on line 1089

Turned out we used the behat/symfony2-extension with dev-master stability.
"Downgrading" to 2.1.5 resolved the issue.

Regarding your parameter issue:
Maybe it's because you are missing the config entry to point the symfony2-extension to the bootstrap file, which basically only loads the .env file.

 extensions:
         Behat\Symfony2Extension:
             kernel:
                 bootstrap: features/bootstrap/bootstrap.php
                 class: App\Kernel

ok. It looks like the problem is solved by explicitly installing 2.1.5 and without having to add
bootstrap: features/bootstrap/bootstrap.php

Thank you @blixit and @pueppiblue!

Hi,
we ran in the same Fatal Error you formerly received as well:

Warning: require_once(/var/www/html): failed to open stream: Success in /var/www/html/vendor/symfony/dependency-injection/ContainerBuilder.php on line 1089
Fatal error: require_once(): Failed opening required '/var/www/html/' (include_path='.:/usr/local/lib/php') in /var/www/html/vendor/symfony/dependency-injection/ContainerBuilder.php on line 1089

Turned out we used the behat/symfony2-extension with dev-master stability.
"Downgrading" to 2.1.5 resolved the issue.

Regarding your parameter issue:
Maybe it's because you are missing the config entry to point the symfony2-extension to the bootstrap file, which basically only loads the .env file.

 extensions:
         Behat\Symfony2Extension:
             kernel:
                 bootstrap: features/bootstrap/bootstrap.php
                 class: App\Kernel

Fixed for me downgrading to 2.1.5 resolved the issue. Thanks

Was this page helpful?
0 / 5 - 0 ratings

Related issues

legovaer picture legovaer  路  6Comments

patxi1980 picture patxi1980  路  9Comments

Yogarine picture Yogarine  路  10Comments

adamcameron picture adamcameron  路  3Comments

Cochonours picture Cochonours  路  3Comments