Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class 'Behat\Behat\Context\BehatContext' not found in PHP with Behat

Tags:

php

behat

I am trying to learn Behat using the tutorial on the website.

The first step goes OK, no errors appear.

But when I am changing the ls_project/features/bootstrap/FeatureContext.php, as shown in the tutorial second step, I am getting the following error: 'Behat\Behat\Context\BehatContext' not found.

The tutorial code, to which the change is applied:

# features/bootstrap/FeatureContext.php
<?php

use Behat\Behat\Context\BehatContext,
    Behat\Behat\Exception\PendingException;
use Behat\Gherkin\Node\PyStringNode,
    Behat\Gherkin\Node\TableNode;

class FeatureContext extends BehatContext
{
    /**
     * @Given /^I am in a directory "([^"]*)"$/
     */
    public function iAmInADirectory($dir)
    {
        if (!file_exists($dir)) {
            mkdir($dir);
        }
        chdir($dir);
    }
}

The full error log:

11:51:33 / ME : /var/www/test-driven/behat/ls_project
$ behat
# features/bootstrap/FeatureContext.php
PHP Fatal error:  Class 'Behat\Behat\Context\BehatContext' not found in /var/www/test-driven/behat/ls_project/features/bootstrap/FeatureContext.php on line 10
PHP Stack trace:
PHP   1. {main}() /opt/Behat/bin/behat:0
PHP   2. Symfony\Component\Console\Application->run() /opt/Behat/bin/behat:31
PHP   3. Behat\Testwork\Cli\Application->doRun() /opt/Behat/vendor/symfony/console/Symfony/Component/Console/Application.php:121
PHP   4. Symfony\Component\Console\Application->doRun() /opt/Behat/src/Behat/Testwork/Cli/Application.php:90
PHP   5. Symfony\Component\Console\Application->doRunCommand() /opt/Behat/vendor/symfony/console/Symfony/Component/Console/Application.php:191
PHP   6. Symfony\Component\Console\Command\Command->run() /opt/Behat/vendor/symfony/console/Symfony/Component/Console/Application.php:892
PHP   7. Behat\Testwork\Cli\Command->execute() /opt/Behat/vendor/symfony/console/Symfony/Component/Console/Command/Command.php:241
PHP   8. Behat\Testwork\Tester\Cli\ExerciseController->execute() /opt/Behat/src/Behat/Testwork/Cli/Command.php:63
PHP   9. Behat\Testwork\Tester\Cli\ExerciseController->testSpecifications() /opt/Behat/src/Behat/Testwork/Tester/Cli/ExerciseController.php:106
PHP  10. Behat\Testwork\EventDispatcher\Tester\EventDispatchingExercise->test() /opt/Behat/src/Behat/Testwork/Tester/Cli/ExerciseController.php:137
PHP  11. Behat\Testwork\Tester\Runtime\RuntimeExercise->test() /opt/Behat/src/Behat/Testwork/EventDispatcher/Tester/EventDispatchingExercise.php:65
PHP  12. Behat\Testwork\Environment\EnvironmentManager->buildEnvironment() /opt/Behat/src/Behat/Testwork/Tester/Runtime/RuntimeExercise.php:67
PHP  13. Behat\Behat\Context\Environment\Handler\ContextEnvironmentHandler->buildEnvironment() /opt/Behat/src/Behat/Testwork/Environment/EnvironmentManager.php:69
PHP  14. Behat\Behat\Context\Environment\UninitializedContextEnvironment->registerContextClass() /opt/Behat/src/Behat/Behat/Context/Environment/Handler/ContextEnvironmentHandler.php:75

Could anyone please help me resolving this issue?

like image 809
fabrice Avatar asked Mar 14 '14 11:03

fabrice


2 Answers

It appears you've installed Behat v3, but you're following Behat 2 docs.

Behat 3

Behat 3 doesn't have the Behat\Behat\Context\BehatContext class. It's got a Behat\Behat\Context\Context interface:

use Behat\Behat\Context\Context;

class FeatureContext implements Context
{
    // ...
}

In composer.json:

{
    "require-dev": {
        "behat/behat": "~3.1"
    },
    "config": {
        "bin-dir": "bin/"
    }
}

Behat 2

Behat 2 uses the Behat\Behat\Context\BehatContext base class:

use Behat\Behat\Context\BehatContext;

class FeatureContext extends BehatContext
{
    // ...
}

In composer.json:

{
    "require-dev": {
        "behat/behat": "~2.5"
    },
    "config": {
        "bin-dir": "bin/"
    }
}
like image 104
Jakub Zalas Avatar answered Sep 20 '22 21:09

Jakub Zalas


In behat 3 there is different structure so you need to use new path to behat context which is in Behat/Behat/Context/Context

<?php

use Behat\Behat\Context\Context;
use Behat\MinkExtension\Context\RawMinkContext;
use Behat\Behat\Context\CustomSnippetAcceptingContext;
use Behat\Behat\Tester\Exception\PendingException;
use Behat\Gherkin\Node\PyStringNode;
use Behat\Gherkin\Node\TableNode;

class FeatureContext extends RawMinkContext implements Context {
}

So use this for example :-)

like image 41
Tominek Avatar answered Sep 19 '22 21:09

Tominek