From 8183d505fc70eb49f0594e15e19f40be4623789f Mon Sep 17 00:00:00 2001 From: jrfnl Date: Thu, 29 Aug 2024 11:57:21 +0200 Subject: [PATCH] PHP 8.4 | Fix passing `E_USER_ERROR` to `trigger_error()` PHP 8.4 deprecates passing `E_USER_ERROR` to `trigger_error()`, with the recommendation being to replace these type of calls with either an Exception or an `exit` statement. This commit fixes the one instance found in this codebase by introducing a new `MissingFunctionExpectations` exception. Includes updating the related tests (and test names) to match. Ref: https://wiki.php.net/rfc/deprecations_php_8_4#deprecate_passing_e_user_error_to_trigger_error Related to #146 --- .../Exception/MissingFunctionExpectations.php | 21 +++++++++++ src/Expectation/FunctionStub.php | 5 +-- tests/cases/unit/Api/FunctionsTest.php | 37 ++++++++++--------- 3 files changed, 42 insertions(+), 21 deletions(-) create mode 100644 src/Expectation/Exception/MissingFunctionExpectations.php diff --git a/src/Expectation/Exception/MissingFunctionExpectations.php b/src/Expectation/Exception/MissingFunctionExpectations.php new file mode 100644 index 0000000..89f5494 --- /dev/null +++ b/src/Expectation/Exception/MissingFunctionExpectations.php @@ -0,0 +1,21 @@ + + * @package BrainMonkey + * @license http://opensource.org/licenses/MIT MIT + */ +class MissingFunctionExpectations extends Exception +{ + +} diff --git a/src/Expectation/FunctionStub.php b/src/Expectation/FunctionStub.php index 2521e81..1cdce6e 100644 --- a/src/Expectation/FunctionStub.php +++ b/src/Expectation/FunctionStub.php @@ -42,9 +42,8 @@ public function __construct(FunctionName $function_name) $function = <<expectErrorException(); - Functions\when('since_i_am_not_defined_i_will_trigger_error'); - $this->expectExceptionMsgRegex('/since_i_am_not_defined_i_will_trigger_error.+not defined/'); + $this->expectException(MissingFunctionExpectations::class); + Functions\when('since_i_am_not_defined_i_will_throw_exception'); + $this->expectExceptionMsgRegex('/since_i_am_not_defined_i_will_throw_exception.+not defined/'); /** @noinspection PhpUndefinedFunctionInspection */ - since_i_am_not_defined_i_will_trigger_error(); + since_i_am_not_defined_i_will_throw_exception(); } /** - * @depends testUndefinedFunctionTriggerErrorRightAfterDefinition + * @depends testUndefinedFunctionThrowsExceptionRightAfterDefinition */ public function testUndefinedFunctionSurviveTests() { - static::assertTrue(function_exists('since_i_am_not_defined_i_will_trigger_error')); + static::assertTrue(function_exists('since_i_am_not_defined_i_will_throw_exception')); } /** * @depends testUndefinedFunctionSurviveTests */ - public function testSurvivedFunctionStillTriggerError() + public function testSurvivedFunctionStillThrowsException() { - $this->expectErrorException(); - $this->expectExceptionMsgRegex('/since_i_am_not_defined_i_will_trigger_error.+not defined/'); + $this->expectException(MissingFunctionExpectations::class); + $this->expectExceptionMsgRegex('/since_i_am_not_defined_i_will_throw_exception.+not defined/'); /** @noinspection PhpUndefinedFunctionInspection */ - since_i_am_not_defined_i_will_trigger_error(); + since_i_am_not_defined_i_will_throw_exception(); } /** - * @depends testSurvivedFunctionStillTriggerError + * @depends testSurvivedFunctionStillThrowsException */ public function testNothingJustMockASurvivedFunction() { - Functions\when('since_i_am_not_defined_i_will_trigger_error')->justReturn(1234567890); + Functions\when('since_i_am_not_defined_i_will_throw_exception')->justReturn(1234567890); /** @noinspection PhpUndefinedFunctionInspection */ - static::assertSame(1234567890, since_i_am_not_defined_i_will_trigger_error()); + static::assertSame(1234567890, since_i_am_not_defined_i_will_throw_exception()); } /** * @depends testNothingJustMockASurvivedFunction */ - public function testSurvivedFunctionStillTriggerErrorAfterBeingMocked() + public function testSurvivedFunctionStillThrowsExceptionAfterBeingMocked() { - $this->expectErrorException(); - $this->expectExceptionMsgRegex('/since_i_am_not_defined_i_will_trigger_error.+not defined/'); + $this->expectException(MissingFunctionExpectations::class); + $this->expectExceptionMsgRegex('/since_i_am_not_defined_i_will_throw_exception.+not defined/'); /** @noinspection PhpUndefinedFunctionInspection */ - since_i_am_not_defined_i_will_trigger_error(); + since_i_am_not_defined_i_will_throw_exception(); } public function testAndAlsoExpectIt()