Skip to content

Comments

Fix PHP error when step is undefined#1

Merged
Chekote merged 1 commit intov3.4.3-with-patchesfrom
fix_undefined_step
Jan 30, 2019
Merged

Fix PHP error when step is undefined#1
Chekote merged 1 commit intov3.4.3-with-patchesfrom
fix_undefined_step

Conversation

@Chekote
Copy link
Owner

@Chekote Chekote commented Jan 30, 2019

Steps to recreate:

Write a feature file with an undefined step
Execute the feature file with Behat

Expected Behavior

Behat should warn that the suite has undefined steps, and ask you to choose a context to generate the snippet.

Actual Behavior

The following PHP fatal error occurs:

PHP Fatal error:  Class name must be a valid object or a string in /var/www/vendor/behat/behat/src/Behat/Behat/Context/Environment/InitializedContextEnvironment.php on line 118
PHP Fatal Error.
Details: Array
(
    [type] => 1
    [message] => Class name must be a valid object or a string
    [file] => /var/www/vendor/behat/behat/src/Behat/Behat/Context/Environment/InitializedContextEnvironment.php
    [line] => 118
)
Stack trace: Array
(
    [0] => Array
        (
            [file] => /var/www/bin/behat
            [line] => 33
            [function] => fatalHandler
            [args] => Array
                (
                )

        )

    [1] => Array
        (
            [function] => {closure}
            [args] => Array
                (
                )

        )

)

Cause

The Behat\Behat\Context\Snippet\Generator\FixedContextIdentifier class is being initialized with an empty string. When a snippet is undefined, the system attempts to present a list of all of the available contexts to generate the snippet within. One of the contexts that the system tries to list is the context defined in Behat\Behat\Context\Snippet\Generator\FixedContextIdentifier. Since this is an empty string, it causes a PHP error when passed to resolveContextClass() via hasContextClass().

Fix

Add a falsy check on the class parameter to hasContextClass(), and return false the param is falsy rather than passing it to resolveContextClass().

## Steps to recreate:
Write a feature file with an undefined step
Execute the feature file with Behat

## Expected Behavior
Behat should warn that the suite has undefined steps, and ask you to choose a context to generate the snippet.

## Actual Behavior
The following PHP fatal error occurs:

```
PHP Fatal error:  Class name must be a valid object or a string in /var/www/vendor/behat/behat/src/Behat/Behat/Context/Environment/InitializedContextEnvironment.php on line 118
PHP Fatal Error.
Details: Array
(
    [type] => 1
    [message] => Class name must be a valid object or a string
    [file] => /var/www/vendor/behat/behat/src/Behat/Behat/Context/Environment/InitializedContextEnvironment.php
    [line] => 118
)
Stack trace: Array
(
    [0] => Array
        (
            [file] => /var/www/bin/behat
            [line] => 33
            [function] => fatalHandler
            [args] => Array
                (
                )

        )

    [1] => Array
        (
            [function] => {closure}
            [args] => Array
                (
                )

        )

)
```

## Cause
The Behat\Behat\Context\Snippet\Generator\FixedContextIdentifier class is being initialized with an empty string. When a snippet is undefined, the system attempts to present a list of all of the available contexts to generate the snippet within. One of the contexts that the system tries to list is the context defined in Behat\Behat\Context\Snippet\Generator\FixedContextIdentifier. Since this is an empty string, it causes a PHP error when passed to resolveContextClass() via hasContextClass().

## Fix
Add a falsy check on the class parameter to hasContextClass(), and return false the param is falsy rather than passing it to resolveContextClass().
@Chekote Chekote merged commit 3f1bb9f into v3.4.3-with-patches Jan 30, 2019
@Chekote Chekote deleted the fix_undefined_step branch January 30, 2019 15:25
Chekote pushed a commit that referenced this pull request Jun 24, 2020
Chekote pushed a commit that referenced this pull request Aug 20, 2021
This PR was merged into the 3.8.x-dev branch.

Discussion
----------

I added "return 0" to fix this error message:

```
$ vendor/bin/behat --debug

behat version 3.7.0

--- configuration
    environment variable (BEHAT_PARAMS): none
    configuration file: /home/jawira/PhpstormProjects/xxxxxxxx/behat.yml

--- extensions
    extensions loaded: api_extension

PHP Fatal error:  Uncaught TypeError: Return value of "Behat\Testwork\Cli\DebugCommand::execute()" must be of the type int, NULL returned. in /home/jawira/PhpstormProjects/xxxxxxxx/vendor/symfony/console/Command/Command.php:258
Stack trace:
#0 /home/jawira/PhpstormProjects/xxxxxxxx/vendor/symfony/console/Application.php(912): Symfony\Component\Console\Command\Command->run()
#1 /home/jawira/PhpstormProjects/xxxxxxxx/vendor/symfony/console/Application.php(264): Symfony\Component\Console\Application->doRunCommand()
#2 /home/jawira/PhpstormProjects/xxxxxxxx/vendor/behat/behat/src/Behat/Testwork/Cli/Application.php(124): Symfony\Component\Console\Application->doRun()
#3 /home/jawira/PhpstormProjects/xxxxxxxx/vendor/symfony/console/Application.php(140): Behat\Testwork\Cli\Application->doRun()
#4 /home/jawira/PhpstormProjects/xxxxxxxx/vendor/behat/behat/bin/behat(34): Symfony\Component\Console\Application->run()
Behat#5 {main}
  thrown in /home/jawira/PhpstormProjects/xxxxxxxx/vendor/symfony/console/Command/Command.php on line 258
```

Commits
-------

48a3636 Add "return 0" in DebugCommand.php
902b3c6 Add debug test
lorenzodalaqua added a commit to lorenzodalaqua/Behat that referenced this pull request Jul 1, 2022
This is applicable to Scenarios which are a part of a Scenario Outline.
Each example in an outline would be appended an index to its name in the
output (ex: Chekote#1, Chekote#2, Chekote#3...), but in a behat --rerun, they are all
appended Chekote#1, which breaks IFT detection. Not having the Example number
is the way the script currently expects the output to be, so it was
removed.
lorenzodalaqua added a commit to lorenzodalaqua/Behat that referenced this pull request Jul 1, 2022
This is applicable to Scenarios which are a part of a Scenario Outline.
Each example in an outline would be appended an index to its name in the
output (ex: Chekote#1, Chekote#2, Chekote#3...), but in a behat --rerun, they are all
appended Chekote#1, which breaks IFT detection. Not having the Example number
is the way the script currently expects the output to be, so it was
removed.
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

Successfully merging this pull request may close these issues.

1 participant