sebastianbergmann / phpunit

The PHP Unit Testing framework.
https://phpunit.de/
BSD 3-Clause "New" or "Revised" License
19.69k stars 2.2k forks source link

Testing a method in a Custom Extension class #5829

Closed MocioF closed 6 months ago

MocioF commented 6 months ago
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.

sebastianbergmann commented 6 months ago

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.

MocioF commented 6 months ago

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

mfn commented 6 months ago

$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 commented 6 months ago

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

sebastianbergmann commented 6 months ago

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