Skip to content

Conversation

@carlos-granados
Copy link
Contributor

When the error reporting level was set (either by using withErrorReporting() or by using the default value of E_ALL), we were calling set_error_handler() with this level but were not calling error_reporting(). This meant that event though we had set a handler to be called on certain error levels, this handler might not be called if the error_reporting level set in php.ini was more restrictive. I saw this in a system where error_reporting was set to E_ALL & ~E_DEPRECATED & ~E_STRICT and the test With default error reporting reports all PHP errors and deprecations failed because the deprecation was not raised

This PR fixes this problem by actually setting the error_reporting level, not just using it in the set_error_handler call

Fixes #1663

Copy link
Contributor

@acoulton acoulton left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@carlos-granados ahhh - that makes sense. Thanks for doing this, sorry I didn't get a chance to get back to it.

One tiny comment on the implementation.

I guess when this ships, people who were using their PHP configuration (rather than their Behat configuration) to e.g. ignore E_DEPRECATED may see failures.

However, I think that's acceptable if we highlight this in the changelog - it's better that we fix the issue that the ->withErrorReporting config doesn't currently work as expected.

Comment on lines 126 to 127
$this->previousErrorReporting = error_reporting();
error_reporting($errorReporting);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't this just be a single call to error_reporting?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, yes, of course

@carlos-granados
Copy link
Contributor Author

@acoulton I was already thinking about what you mentioned and I think it would be problematic to release the fix as it is because many people would not have set the error level using the config, so they may suddenly start seeing deprecations and other errors which are silenced by their php.ini config and I think that should be avoided.
So what I was thinking was:

  • If you set the error level using the config, use the fix and respect the configured value
  • If you don't set the error level using the config, go back to the previous behaviour where error reporting used the value defined in the ini config
    What do you think?

@acoulton
Copy link
Contributor

@carlos-granados I guess... The only thing is, can we tell whether the user has set the error_reporting in config?

Obviously we could add some state tracking to the ->withErrorReporting call in the PHP config interface, but if they're still using YAML that won't help.

I suppose we could change the default value from E_ALL to error_reporting() so that if the user doesn't explicitly configure it for Behat then it follows the system default?

->scalarNode('error_reporting')
->info('Call executor will catch exceptions matching this level')
->defaultValue(E_ALL)

And then we can use your patch as-is (even though the call to "change" the error_reporting level will essentially be a no-op).

Off the top of my head I think that would match the current behaviour if the user has not explicitly configured Behat specifically, while fixing the issue if they have?

@carlos-granados
Copy link
Contributor Author

@carlos-granados I guess... The only thing is, can we tell whether the user has set the error_reporting in config?

Obviously we could add some state tracking to the ->withErrorReporting call in the PHP config interface, but if they're still using YAML that won't help.

I suppose we could change the default value from E_ALL to error_reporting() so that if the user doesn't explicitly configure it for Behat then it follows the system default?

->scalarNode('error_reporting')
->info('Call executor will catch exceptions matching this level')
->defaultValue(E_ALL)

And then we can use your patch as-is (even though the call to "change" the error_reporting level will essentially be a no-op).

Off the top of my head I think that would match the current behaviour if the user has not explicitly configured Behat specifically, while fixing the issue if they have?

Sounds like a good solution, let me test it. For now I am marking this PR as draft

@carlos-granados carlos-granados marked this pull request as draft October 22, 2025 13:04
@carlos-granados carlos-granados marked this pull request as ready for review October 22, 2025 17:22
@carlos-granados
Copy link
Contributor Author

@acoulton updated with your suggestion and tests modified to check all needed cases

Copy link
Contributor

@acoulton acoulton left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, thanks @carlos-granados - just one small comment on BC

Comment on lines 41 to 43
private $errorReportingLevel = E_ALL,
private $errorReportingLevel,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It could technically be a BC break to make this parameter non-optional?

I think we could safely leave it with the default E_ALL, if we know that in practice in our usage we're always providing a value?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, you are right, we don't really need to remove this default value to get this working. Restored

Copy link
Contributor

@acoulton acoulton left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect. Thanks for adding such comprehensive feature coverage :-)

@carlos-granados carlos-granados merged commit b759ab6 into Behat:master Oct 24, 2025
19 checks passed
@carlos-granados
Copy link
Contributor Author

@acoulton perhaps the time for a new release?

@carlos-granados carlos-granados deleted the fix-error-reporting branch October 24, 2025 08:49
@acoulton
Copy link
Contributor

I was thinking I'd try and update/replace the two existing JUnit-related PRs (#1459 &#1601) before we release: possibly easier for users if all the changes to junit content happen in the same release?

Not sure if I'll get to those today, but hopefully by early next week (if not then we can ship as we are, I - don't think they're blocking, just nice-to-have).

@carlos-granados
Copy link
Contributor Author

I

I was thinking I'd try and update/replace the two existing JUnit-related PRs (#1459 &#1601) before we release: possibly easier for users if all the changes to junit content happen in the same release?

Not sure if I'll get to those today, but hopefully by early next week (if not then we can ship as we are, I - don't think they're blocking, just nice-to-have).

If you want, I can work on the second one (#1601) as it is related to paths, where I have done a lot of work recently, and you can work on the first one which is more familiar to you

@acoulton
Copy link
Contributor

If you have time that would be great :)

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.

Set error_reporting level when running tests

2 participants