richardmiller-zz / ErrorExtension

Behat extension to handle fatal errors in a slightly nicer way. [READ-ONLY] Subtree split from https://github.com/richardmiller/BehatSpec
MIT License
7 stars 2 forks source link

ErrorTester don't work with Behat Exception #2

Open pocky opened 7 years ago

pocky commented 7 years ago

error_get_last() return NULL for Behat Exception so exemplify can't work. Could you confirm this?

Feature: Test project

  Scenario:                               # features/beer.feature:3
    Given I am a customer                 # FeatureContext::iAmACustomer()
    When I want a beer                    # FeatureContext::iWantABeer()
    Then the bartender should give me one # FeatureContext::theBartenderShouldGiveOne()
    And I say thanks "Tom"                # FeatureContext::iSayThanks()
      Fatal error: Call to undefined method Customer::thanks() (Behat\Testwork\Call\Exception\FatalThrowableError)
    And I drink my beer                   # FeatureContext::iDrinkMyBeer()
    And I am "drunk"                      # FeatureContext::iAm()

--- Failed scenarios:

    features/beer.feature:3

1 scenario (1 failed)
6 steps (3 passed, 1 failed, 2 skipped)
0m0.02s (8.77Mb)
/Users/pocky/Sites/behat-phpspec/BehatSpec/src/Extension/ErrorExtension/src/Tester/ErrorTester.php:93:
NULL
    /**
     * @Then I say thanks :name
     */
    public function iSayThanks($name)
    {
        if ($name !== $this->customer->thanks()) {
            throw new \Exception();
        }
    }

BehatSpec/src/Extension/ErrorExtension/src/Tester/ErrorTester.php line 89

public function registerShutdownFunction()
    {
        register_shutdown_function(function () {

            var_dump(error_get_last());

            if ($error = error_get_last()) {
                $errorMessages = [
                    sprintf('The error "%s"', $error['message']),
                    sprintf('occurred in file %s', $error['file']),
                    sprintf('at line %s', $error['line']),
                ];

                $formatter = new FormatterHelper();
                $formattedBlock = $formatter->formatBlock($errorMessages, 'error', true);
                $this->output->writeln('');
                $this->output->writeln($formattedBlock);
                $this->output->writeln('');

                foreach ($this->observers as $observer) {
                    $observer->notify($error);
                }
            }
        });
    }
pocky commented 7 years ago

I don't know how to handle the exception, I tried with events and try/catch on setUp but this is (apparently) not the solution.

stof commented 7 years ago

this is totally expected, because error_get_last does not deal with exceptions at all in PHP, but only with errors (warnings, notices, etc...)

pocky commented 7 years ago

Got It! PR incoming!

phpspec

RemiWoler commented 7 years ago

@pocky Any luck with the PR? If it's not PR-ready, could you at least make a gist of the modification needed to get it to work?

richardmiller-zz commented 7 years ago

Is this with the exceptions and handlers introduced in behat 3.2? If so I think they should remove the need for this extension altogether if they are used instead of this extension's error observers.

pocky commented 7 years ago

@RemiWoler The problem is the gap between current version (phpspec 2, multi-repository) and my working one (phpspec3, monorep, many PR on extensions). I didn't take time to apply/tests changes on 0.3.0 with Behat 2/3 and phpspec 2 (I tried quickly and it was not good).

Working version for behat 3/phpspec 3 is here https://github.com/pocky/BehatSpec/commit/f72ea6a5b599a4837cee408511289215c49cad78

pocky commented 7 years ago

@RemiWoler Working version for Behat3/phpspec2 is here https://github.com/richardmiller/BehatSpec/pull/11