Skip to content

Commit

Permalink
Fixed mocker function for _n() (#163)
Browse files Browse the repository at this point in the history
Fixes #162

Co-authored-by: Jignesh Nakrani <jignesh.nakrani@rtcamp.com>
Co-authored-by: Fulvio Notarstefano <unfulvio@godaddy.com>
  • Loading branch information
3 people authored Sep 23, 2022
1 parent 61e95de commit 2226576
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
22 changes: 21 additions & 1 deletion php/WP_Mock/API/function-mocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,27 @@ function esc_attr_x() {
}

if ( ! function_exists( '_n' ) ) {

/**
* Dummy method for _n().
*
* @throws \PHPUnit\Framework\ExpectationFailedException Throws error if too few arguments.
*
* @return mixed singular or plural string based on number.
*/
function _n() {
return \WP_Mock\Handler::predefined_return_function_helper( __FUNCTION__, func_get_args() );
$args = func_get_args();

if ( count( $args ) >= 3 ) {
if ( isset( $args[0] ) && 1 >= intval( $args[2] ) ) {
return $args[0];
} else {
return $args[1];
}
} else {
throw new \PHPUnit\Framework\ExpectationFailedException(
sprintf( 'Too few arguments to function %s', __FUNCTION__ )
);
}
}
}
7 changes: 6 additions & 1 deletion tests/FunctionMocksTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,12 @@ public function testCommonFunctionsDefaultFunctionality( $function, $action ) {
$this->expectOutputString( $input );
$expected = null;
}
$this->assertEquals( $expected, call_user_func( $function, $input ) );

if ('_n' === $function) {
$this->assertEquals($expected, call_user_func($function, $input, 'foo', 1, 'bar'));
} else {
$this->assertEquals($expected, call_user_func($function, $input));
}
}

/**
Expand Down

0 comments on commit 2226576

Please sign in to comment.