Skip to content

Commit

Permalink
Merge pull request #149 from Brain-WP/feature/php-8.4-fix-for-trigger…
Browse files Browse the repository at this point in the history
…-error

PHP 8.4 | Fix passing `E_USER_ERROR` to `trigger_error()`
  • Loading branch information
gmazzap authored Aug 29, 2024
2 parents f5b3384 + 8183d50 commit d95a9d8
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 21 deletions.
21 changes: 21 additions & 0 deletions src/Expectation/Exception/MissingFunctionExpectations.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php # -*- coding: utf-8 -*-
/*
* This file is part of the BrainMonkey package.
*
* (c) Giuseppe Mazzapica
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Brain\Monkey\Expectation\Exception;

/**
* @author Giuseppe Mazzapica <[email protected]>
* @package BrainMonkey
* @license http://opensource.org/licenses/MIT MIT
*/
class MissingFunctionExpectations extends Exception
{

}
5 changes: 2 additions & 3 deletions src/Expectation/FunctionStub.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,8 @@ public function __construct(FunctionName $function_name)
$function = <<<PHP
namespace {$namespace} {
function {$name}() {
trigger_error(
'"{$name}" is not defined nor mocked in this test.',
E_USER_ERROR
throw new \Brain\Monkey\Expectation\Exception\MissingFunctionExpectations(
'"{$name}" is not defined nor mocked in this test.'
);
}
}
Expand Down
37 changes: 19 additions & 18 deletions tests/cases/unit/Api/FunctionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace Brain\Monkey\Tests\Unit\Api;

use Brain\Monkey\Expectation\Exception\MissingFunctionExpectations;
use Brain\Monkey\Functions;
use Brain\Monkey\Tests\UnitTestCase;
use Mockery\Exception\InvalidCountException;
Expand Down Expand Up @@ -146,53 +147,53 @@ public function testEchoArgFirst()
i_do_not_exists('Cool!');
}

public function testUndefinedFunctionTriggerErrorRightAfterDefinition()
public function testUndefinedFunctionThrowsExceptionRightAfterDefinition()
{
$this->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()
Expand Down

0 comments on commit d95a9d8

Please sign in to comment.