Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow custom processing of WebAssert::assert through callback #835

Closed
mondrake opened this issue Sep 26, 2022 · 9 comments
Closed

Allow custom processing of WebAssert::assert through callback #835

mondrake opened this issue Sep 26, 2022 · 9 comments

Comments

@mondrake
Copy link

Right now, the WebAssert::assert() throws a ExpectationException when the condition is not fulfilled.

When running tests with PHPUnit + Mink, this results in PHPUnit 'errors' instead of 'fails', which is not correct, because

A failure is a violated PHPUnit assertion such as a failing assertSame() call. An error is an unexpected exception or a PHP error.

And when we assert with Mink, we check for a specific condition.

This came up in Drupal, see more details in Change BrowserTestBase to flag PHPUnit failures instead of throwing Mink exceptions.

It would be good to have the possibility to specify a callback that WebAssert::assert() could use, falling back to current behaviour if no callback is specified.

@stof
Copy link
Member

stof commented Sep 28, 2022

instead of forcing to customize WebAssert (which also means we will never be able to add new kinds of failures in the proposed API as that would break BC for your callback), a cleaner solution would be to use the dedicated PHPUnit feature to handle some exceptions as failures.

See https://github.com/minkphp/driver-testsuite/blob/af6107c1f16f4c43c19ea162e0da88d55c6cf21a/tests/TestCase.php#L74-L81 where our driver testsuite uses that hook to mark tests as skipped instead of errored when a Behat\Mink\Exception\UnsupportedDriverActionException exception is thrown. You could do the same to turn Behat\Mink\Exception\ExpectationException into a failure instead

@alexpott
Copy link
Contributor

Yes but doing that means re-throwing the exception from the onNotSuccessful and therefore changing the exception type. We have tried to propose changes to PHPUnit to facilitate this see sebastianbergmann/phpunit#4983 (comment)

Atm if we want to mark Mink exceptions as PHPUnit failures we have do something like:


    if ($t instanceof MinkException) {
      $e = new AssertionFailedError($t->getMessage(), $t->getCode(), $t);
      $trace = $t->getTrace();
      foreach ($trace as $i => $call) {
        unset($trace[$i]['args']);
      }
      $reflection = new \ReflectionProperty($e, 'serializableTrace');
      $reflection->setValue($e, $trace);
      throw $e;
    }

in order to preserve the trace. Which in my mind not very sensible.

@stof
Copy link
Member

stof commented Sep 29, 2022

AFAIU, the need for this complex code is caused by AssertionFailedError loosing the chained exception when serializing it for process isolation. I would rather work on fixing that in PHPUnit (as chaining exceptions was the suggested solution in sebastianbergmann/phpunit#4983 (comment))

@mondrake
Copy link
Author

mondrake commented Oct 2, 2022

I have filed sebastianbergmann/phpunit#5068 which, I think, would solve the trace issue while using the solution proposed here in #835 (comment) which seems to me the cleanest one.

@mondrake
Copy link
Author

mondrake commented Oct 6, 2022

PHPUnit rejected sebastianbergmann/phpunit#5069 and will not pursue sebastianbergmann/phpunit#5068.

@stof
Copy link
Member

stof commented Oct 6, 2022

Well, if he decided that mapping the details is the way to go, he should probably bundle an utility in PHPUnit to make it easy to copy the trace, given the hacks it requires. but that should be discussed with PHPUnit, as that's clearly a PHPUnit limitation here.

The proposal done here adds lots of complexity in Mink and means that we cannot add new assertions outside of major versions as it would change the requirements for that callback.

@mondrake
Copy link
Author

mondrake commented Oct 6, 2022

Alright, I understand the position here. I will try something else on Drupal side, before calling time of death. Thanks

@stof
Copy link
Member

stof commented Feb 14, 2023

sebastianbergmann/phpunit#5201 might allow to solve the root cause without the need for this weird feature request. So you can subscribe to that issue.

@mondrake
Copy link
Author

Thanks for the pointer, subscribed there.

I’d say what’s weird here could be the solution - the feature request per se is quite sensible.

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 a pull request may close this issue.

3 participants