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

Ignore throws in function mocks #245

Merged
merged 5 commits into from
Jun 28, 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
96 changes: 49 additions & 47 deletions php/WP_Mock/API/function-mocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,186 +83,188 @@ function apply_filters($tag, $value)

if (! function_exists('esc_html')) {
/**
* @return string|mixed
* @throws ExpectationFailedException|Exception
* @return string
*/
function esc_html()
function esc_html() : string
{
/** @phpstan-ignore-next-line to prevent flagging the function as throwing exception in codebases requiring WP_Mock */
return Handler::handlePredefinedReturnFunction(__FUNCTION__, func_get_args());
}
}

if (! function_exists('esc_attr')) {
/**
* @return string|mixed
* @throws ExpectationFailedException|Exception
* @return string
*/
function esc_attr()
function esc_attr() : string
{
/** @phpstan-ignore-next-line to prevent flagging the function as throwing exception in codebases requiring WP_Mock */
return Handler::handlePredefinedReturnFunction(__FUNCTION__, func_get_args());
}
}

if (! function_exists('esc_url')) {
/**
* @return string|mixed
* @throws ExpectationFailedException|Exception
* @return string
*/
function esc_url()
function esc_url() : string
{
/** @phpstan-ignore-next-line to prevent flagging the function as throwing exception in codebases requiring WP_Mock */
return Handler::handlePredefinedReturnFunction(__FUNCTION__, func_get_args());
}
}

if (! function_exists('esc_url_raw')) {
/**
* @return string|mixed
* @throws ExpectationFailedException|Exception
* @return string
*/
function esc_url_raw()
function esc_url_raw() : string
{
/** @phpstan-ignore-next-line to prevent flagging the function as throwing exception in codebases requiring WP_Mock */
return Handler::handlePredefinedReturnFunction(__FUNCTION__, func_get_args());
}
}

if (! function_exists('esc_js')) {
/**
* @return string|mixed
* @throws ExpectationFailedException|Exception
* @return string
*/
function esc_js()
function esc_js() : string
{
/** @phpstan-ignore-next-line to prevent flagging the function as throwing exception in codebases requiring WP_Mock */
return Handler::handlePredefinedReturnFunction(__FUNCTION__, func_get_args());
}
}

if (! function_exists('esc_textarea')) {
/**
* @return string|mixed
* @throws ExpectationFailedException|Exception
* @return string
*/
function esc_textarea()
function esc_textarea() : string
{
/** @phpstan-ignore-next-line to prevent flagging the function as throwing exception in codebases requiring WP_Mock */
return Handler::handlePredefinedReturnFunction(__FUNCTION__, func_get_args());
}
}

if (! function_exists('__')) {
/**
* @return string|mixed
* @throws ExpectationFailedException|Exception
* @return string
*/
function __()
function __() : string
{
/** @phpstan-ignore-next-line to prevent flagging the function as throwing exception in codebases requiring WP_Mock */
return Handler::handlePredefinedReturnFunction(__FUNCTION__, func_get_args());
}
}

if (! function_exists('_e')) {
/**
* @return void
* @throws ExpectationFailedException|Exception
*/
function _e(): void
function _e() : void
{
/** @phpstan-ignore-next-line to prevent flagging the function as throwing exception in codebases requiring WP_Mock */
Handler::handlePredefinedEchoFunction(__FUNCTION__, func_get_args());
}
}

if (! function_exists('_x')) {
/**
* @return string|mixed
* @throws ExpectationFailedException|Exception
* @return string
*/
function _x()
function _x() : string
{
/** @phpstan-ignore-next-line to prevent flagging the function as throwing exception in codebases requiring WP_Mock */
return Handler::handlePredefinedReturnFunction(__FUNCTION__, func_get_args());
}
}

if (! function_exists('esc_html__')) {
/**
* @return string|mixed
* @throws ExpectationFailedException|Exception
* @return string
*/
function esc_html__()
function esc_html__() : string
{
/** @phpstan-ignore-next-line to prevent flagging the function as throwing exception in codebases requiring WP_Mock */
return Handler::handlePredefinedReturnFunction(__FUNCTION__, func_get_args());
}
}

if (! function_exists('esc_html_e')) {
/**
* @return void
* @throws ExpectationFailedException|Exception
*/
function esc_html_e(): void
function esc_html_e() : void
{
/** @phpstan-ignore-next-line to prevent flagging the function as throwing exception in codebases requiring WP_Mock */
Handler::handlePredefinedEchoFunction(__FUNCTION__, func_get_args());
}
}

if (! function_exists('esc_html_x')) {
/**
* @return string|mixed
* @throws ExpectationFailedException|Exception
* @return string
*/
function esc_html_x()
function esc_html_x() : string
{
/** @phpstan-ignore-next-line to prevent flagging the function as throwing exception in codebases requiring WP_Mock */
return Handler::handlePredefinedReturnFunction(__FUNCTION__, func_get_args());
}
}

if (! function_exists('esc_attr__')) {
/**
* @return string|mixed
* @throws ExpectationFailedException|Exception
* @return string
*/
function esc_attr__()
function esc_attr__() : string
{
/** @phpstan-ignore-next-line to prevent flagging the function as throwing exception in codebases requiring WP_Mock */
return Handler::handlePredefinedReturnFunction(__FUNCTION__, func_get_args());
}
}

if (! function_exists('esc_attr_e')) {
/**
* @return void
* @throws ExpectationFailedException|Exception
*/
function esc_attr_e(): void
function esc_attr_e() : void
{
/** @phpstan-ignore-next-line to prevent flagging the function as throwing exception in codebases requiring WP_Mock */
Handler::handlePredefinedEchoFunction(__FUNCTION__, func_get_args());
}
}

if (! function_exists('esc_attr_x')) {
/**
* @return string|mixed
* @throws ExpectationFailedException
* @return string
*/
function esc_attr_x()
function esc_attr_x() : string
{
/** @phpstan-ignore-next-line to prevent flagging the function as throwing exception in codebases requiring WP_Mock */
return Handler::handlePredefinedReturnFunction(__FUNCTION__, func_get_args());
}
}

if (! function_exists('_n')) {
/**
* @return string|mixed singular or plural string based on number
* @throws ExpectationFailedException if too few arguments passed
* @return string
*/
function _n()
function _n() : string
{
$args = func_get_args();

if (count($args) >= 3) {
/** @phpstan-ignore-next-line */
if (isset($args[0]) && 1 >= intval($args[2])) {
return $args[0];
/** @phpstan-ignore-next-line */
return (string) $args[0];
} else {
return $args[1];
/** @phpstan-ignore-next-line */
return (string) $args[1];
}
} else {
/** @phpstan-ignore-next-line to prevent flagging the function as throwing exception in codebases requiring WP_Mock */
throw new ExpectationFailedException(sprintf('Too few arguments to function %s', __FUNCTION__));
}
}
Expand Down
3 changes: 2 additions & 1 deletion php/WP_Mock/Functions/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public static function handlePredefinedReturnFunction(string $functionName, arra
* @param string $functionName function name
* @param array<mixed> $args function arguments
* @return void
* @throws Exception|ExpectationFailedException
* @throws ExpectationFailedException
*/
public static function handlePredefinedEchoFunction(string $functionName, array $args = []): void
{
Expand All @@ -115,6 +115,7 @@ public static function handlePredefinedEchoFunction(string $functionName, array
} catch (Exception $exception) {
ob_end_clean();

/** @var ExpectationFailedException $exception */
throw $exception;
}

Expand Down
3 changes: 1 addition & 2 deletions tests/Integration/WP_MockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ public function providerCommonFunctionsDefaultFunctionality(): array
* @preserveGlobalState disabled
*
* @return void
* @throws Exception
*/
public function testDefaultFailsInStrictMode(): void
{
Expand Down Expand Up @@ -175,7 +174,7 @@ public function testMockingOverridesDefaults(): void
*/
public function testBotchedMocksStillOverridesDefault(): void
{
WP_Mock::userFunction('esc_html');
WP_Mock::userFunction('esc_html')->andReturn('');

/** @phpstan-ignore-next-line function "exists" */
$this->assertEmpty(esc_html('Input'));
Expand Down
Loading