Skip to content

Commit

Permalink
Fix Expecting E_ERROR and E_USER_ERROR is deprecated
Browse files Browse the repository at this point in the history
Expecting E_ERROR and E_USER_ERROR is deprecated and will no longer be
possible in PHPUnit 10.

helper_array_transpose() now throws a ClientException instead of calling
trigger_error() when detecting a non-bidimensional array.

Adapt HelperTest::testArrayTransposeInvalid() to handle the exception
instead of E_USER_ERROR.
  • Loading branch information
dregad committed Feb 21, 2024
1 parent ee5194f commit 981d8d9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
8 changes: 6 additions & 2 deletions core/helper_api.php
Expand Up @@ -88,14 +88,18 @@ function helper_alternate_class( $p_index = null, $p_odd_class = 'row-1', $p_eve
* becomes array('k1'=>array('a'=>1,'b'=>3),'k2'=>array('a'=>2,'b'=>4))
*
* @param array $p_array The array to transpose.
*
* @return array|mixed transposed array or $p_array if not 2-dimensional array
* @throws ClientException
*/
function helper_array_transpose( array $p_array ) {
$t_out = array();
foreach( $p_array as $t_key => $t_sub ) {
if( !is_array( $t_sub ) ) {
# This function can only handle bidimensional arrays
trigger_error( ERROR_GENERIC, ERROR );
throw new ClientException(
__FUNCTION__ . " can only handle bidimensional arrays",
ERROR_GENERIC
);
}

foreach( $t_sub as $t_subkey => $t_value ) {
Expand Down
9 changes: 6 additions & 3 deletions tests/Mantis/HelperTest.php
Expand Up @@ -24,6 +24,8 @@
*/

# Includes
use Mantis\Exceptions\ClientException;

require_once 'MantisCoreBase.php';

/**
Expand All @@ -41,6 +43,7 @@ class HelperTest extends MantisCoreBase {
* @return void
*
* @dataProvider providerArrayTransposeValid
* @throws ClientException
*/
public function testArrayTransposeValid( $p_in, $p_out ) {
$this->assertEquals( $p_out, helper_array_transpose( $p_in ) );
Expand All @@ -55,9 +58,9 @@ public function testArrayTransposeValid( $p_in, $p_out ) {
* @dataProvider providerArrayTransposeInvalid
*/
public function testArrayTransposeInvalid( $p_in ) {
$this->expectException(PHPUnit\Framework\Error\Error::class);
$this->expectExceptionCode(E_USER_ERROR);
$this->expectExceptionMessage((string)ERROR_GENERIC);
$this->expectException( ClientException::class );
$this->expectExceptionMessage( 'helper_array_transpose can only handle bidimensional arrays' );

helper_array_transpose( $p_in );
}

Expand Down

0 comments on commit 981d8d9

Please sign in to comment.