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

PHP 8.4 | Fix passing E_USER_ERROR to trigger_error() #149

Merged
merged 1 commit into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading