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

Testing a method in a Custom Extension class #5829

Closed
MocioF opened this issue Apr 25, 2024 · 5 comments
Closed

Testing a method in a Custom Extension class #5829

MocioF opened this issue Apr 25, 2024 · 5 comments

Comments

@MocioF
Copy link

MocioF commented Apr 25, 2024

Q A
PHPUnit version 9.6.19
PHP version 8.1.26
Installation Method Composer

Summary

I am trying to test a method of a Custom Exception Class. This method writes data on the database (it's a logger), but it seems that after the extension is raised and "catched" by PHPUnit, the following code is not executed.
Is there a way to test a custom method of a custom Extension?

namespace MYNS
use MYNS\MyLogger as Log;
class CustomException extends \Exception {
[...]
	public function logexception() {
		// Writes an info message in a db table with fields: VARCHAR loglevel,  VARCHAR message
		Log::info( "a message" );
	}
}

First attempt:

/**
 * @dataProvider raisedExceptions
 * @group exceptions
 */
public function test_logexception( $e, $expected_level, $expected_count) {
	global $wpdb;
	$this->expectException( MYNS\CustomException::class );
	$customException = throw new MYNS\CustomException:( ...$e );
	$customException->logexception();
	$sql = 'SELECT * FROM `' . $wpdb->prefix . 'my_logs`';
	$res = $wpdb->get_results( $sql ); // this returns an array of objects each representing a result line

	$this->assertSame( $expected_count, count( $res ), "db res is:\n" . print_r( $res, true ) );

	$this->assertEquals( $expected_level, $res[0]->loglevel );
}

public static function raisedExceptions() {
		return array(
			array( array( 'message, code and level info', 1001, 1 ), 'info', 1 ),
		);
	}
}

Current behavior

Result is:
OK (1 test, 1 assertion)

Expected behavior

OK (1 test, 3 assertion)

Second try:

/**
 * @dataProvider raisedExceptions
 * @group exceptions
 */
public function test_logexception( $e, $expected_level, $expected_count) {
	global $wpdb;
	$this->expectException( MYNS\CustomException::class );
	$customException = throw new MYNS\CustomException:( ...$e );
	$customException->logexception();
	$sql = 'SELECT * FROM `' . $wpdb->prefix . 'my_logs`';
	$res = $wpdb->get_results( $sql ); // this returns an array of objects each representing a result line
	return $res
}

/**
 * @dataProvider raisedExceptions
 * @depends test_logexception
 * @group exceptions
 */
public function test_logexception2( $e, $expected_level, $expected_count, $res ) {
	$this->assertSame( $expected_count, count( $res ), "db res is:\n" . print_r( $res, true ) );
	$this->assertEquals( $expected_level, $res[0]->loglevel );
}

public static function raisedExceptions() {
		return array(
			array( array( 'message, code and level info', 1001, 1 ), 'info', 1 ),
		);
	}
}

and result is

Current behavior

.E 2 / 2 (100%)

Time: 00:00.059, Memory: 44.50 MB

There was 1 error:
TypeError: count(): Argument #1 ($value) must be of type Countable|array, null given

I also tried to use try...catch...finally blocks in the test unit but with no success.

@MocioF MocioF added the type/bug Something is broken label Apr 25, 2024
@sebastianbergmann
Copy link
Owner

I do not understand what you are trying to report. You write that you want to test a "custom extension class". An extension for what?

Thank you for your report.

Please provide a minimal, self-contained, reproducing test case that shows the problem you are reporting.

Without such a minimal, self-contained, reproducing test case I will not be able to investigate this issue.

@sebastianbergmann sebastianbergmann added the status/waiting-for-feedback Waiting for feedback from original reporter label Apr 25, 2024
@MocioF
Copy link
Author

MocioF commented Apr 25, 2024

Sorry for the typo,
I am trying to test methods of a Custom Exception Class

@mfn
Copy link

mfn commented Apr 25, 2024

$customException = throw new MYNS\CustomException:( ...$e );

Are you sure this line is correct? Shouldn't it be $customException = new MYNS\CustomException:( ...$e ); ?

Anyway, somehow the test doesn't make sense as currently written.

@MocioF
Copy link
Author

MocioF commented Apr 25, 2024

thanks @mfn, the test aims to check that a method in the custom exception class works as expected.

@sebastianbergmann
Copy link
Owner

I agree with @mfn: this test does not make any sense to me, sorry.

@sebastianbergmann sebastianbergmann removed status/waiting-for-feedback Waiting for feedback from original reporter type/bug Something is broken labels Apr 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants