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

Fix error_reporting() with PHP >= 8 #1378

Merged

Conversation

AlexandruGG
Copy link
Contributor

@AlexandruGG AlexandruGG commented Jun 4, 2021

This PR improves on #1376 with a Behat feature and clearer specs.

Solves #1375.

The code change is based on:

In a nutshell this is the main issue:
Warning Prior to PHP 8.0.0, the value of the severity passed to the custom error handler was always 0 if the diagnostic was suppressed. This is no longer the case as of PHP 8.0.0.

The code change allows use of the error control operator @ in PHP 8 without causing PHPSpec to fail.

Comment on lines +44 to +80
Scenario: A suppressed error will not cause a spec failure
Given the spec file "spec/Runner/ErrorSuppression2/ErrorControlSpec.php" contains:
"""
<?php

namespace spec\Runner\ErrorSuppression2;

use PhpSpec\ObjectBehavior;

class ErrorControlSpec extends ObjectBehavior
{
function it_returns_string()
{
$this->suppressing()->shouldBe('it works!');
}
}

"""
And the class file "src/Runner/ErrorSuppression2/ErrorControl.php" contains:
"""
<?php

namespace Runner\ErrorSuppression2;

class ErrorControl
{
public function suppressing(): string
{
@trigger_error('Nope!', E_USER_WARNING);

return 'it works!';
}
}

"""
When I run phpspec
Then the suite should pass
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this Behat scenario will fail in PHP 8 without the code change, but not in PHP 7

}

function it_throws_error_exception_when_message_not_match()
{
$oldLevel = error_reporting(E_ALL);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

adding these in all the specs removes the hidden dependency on the value set in php.ini and clarifies the purpose of the spec a bit more

@AlexandruGG AlexandruGG marked this pull request as ready for review June 4, 2021 23:02
@drupol drupol mentioned this pull request Jun 5, 2021
@AlexandruGG
Copy link
Contributor Author

hey @ciaranmcnulty, this is ready for review on my side. Let me know if there's anything else you'd like to see on here

@drupol
Copy link
Contributor

drupol commented Jun 9, 2021

This PR is indeed what we should do. Thanks for this!
Going to close the other one.

*
* @link https://www.php.net/manual/en/language.operators.errorcontrol.php
*/
function it_returns_false_when_error_suppressed_in_php_8()
Copy link

Choose a reason for hiding this comment

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

No need to have _in_php_8 in the name, because the same behavior should be present for other versions.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think it's useful to see the distinction because I'm making error reporting return this code, which is specific to php 8:

error_reporting(E_ERROR | E_PARSE | E_CORE_ERROR | E_COMPILE_ERROR | E_USER_ERROR | E_RECOVERABLE_ERROR)

The spec above it uses error_reporting(0), which is the behaviour when suppressing errors in php < 8

Copy link
Contributor

Choose a reason for hiding this comment

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

@kelunik Are you ok with this?

Copy link

Choose a reason for hiding this comment

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

Sure, it was just a suggestion, because from the outside, there shouldn't be a difference, regardless of the error level set with error_reporting.

@AlexandruGG
Copy link
Contributor Author

@ciaranmcnulty hey, could you give this a review please? Cheers

@drupol
Copy link
Contributor

drupol commented Jun 16, 2021

Hi all,

It would be nice to have some feedback here.

We had to add a specific condition in our tests in order to prevent failures with PHP 8.

Is there anything I can do to help, feel free to let me know, I guess @AlexandruGG share the same feelings.

Thanks!

@ciaranmcnulty
Copy link
Member

Thanks for this @AlexandruGG, it looks great

@ciaranmcnulty ciaranmcnulty merged commit cbb0243 into phpspec:main Jun 19, 2021
@AlexandruGG
Copy link
Contributor Author

@ciaranmcnulty thank you for reviewing and getting this merged!

@AlexandruGG AlexandruGG deleted the feature/php8-error-suppression branch June 20, 2021 07:27
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.

None yet

4 participants