zrnik/phpunit-exceptions

用于在PHPUnit中简化异常测试的Trait。

v0.1.0 2024-03-09 11:53 UTC

This package is auto-updated.

Last update: 2024-09-09 13:11:47 UTC


README

用于在PHPUnit中简化异常测试的Trait。

需求

{
    "php": "^8",
    "phpunit/phpunit": "^9|^10|^11"
}

用法

将trait use Zrnik\PHPUnit\Exceptions; 添加到您的测试用例中。然后测试断言就可用。

use PHPUnit\Framework\TestCase;
use Tests\ExampleObject;
use Tests\NotInRangeException;

class ExampleTest extends TestCase
{
    use \Zrnik\PHPUnit\Exceptions; // add this trait to your TestCase

    public function textExample(): void
    {        
        $exampleObject = new ExampleObject();
        
        $this->assertExceptionThrown(
            NotInRangeException::class, // Expected Exception Type
            
            // Closure running the code we expect to get an exception from.
            function () use ($exampleObject) {
                $exampleObject->assertRange(0);
            }
        );
        
        $this->assertNoExceptionThrown(
            function () use ($exampleObject) {
                $exampleObject->assertRange(1);
                $exampleObject->assertRange(10);
            }
        );
        
        $this->assertExceptionThrown(
            NotInRangeException::class,
            function () use ($exampleObject) {
                $exampleObject->assertRange(11);
            }
        );
    }
}

原因

我在使用默认的expectException时遇到了问题。问题在于创建过多不必要的函数或使用try/catch块来检查异常。所有异常都可在./tests/AssertExceptionTest.php文件中找到。

注意: 也许,我只是测试能力差。这完全有可能...

示例1:使用过多方法...

use PHPUnit\Framework\TestCase;
use Tests\ExampleObject;
use Tests\NotInRangeException;

class ExampleTest extends TestCase
{
    public function test_ExpectException_First(): void
    {
        $exampleObject = new ExampleObject();
        $this->expectException(NotInRangeException::class);
        $exampleObject->assertRange(0);
        //The execution ends here, the method will not continue,
        // after first exception thrown, so I need to create
        // method for every exception tested...
    }

    public function test_ExpectException_Second(): void
    {
        $exampleObject = new ExampleObject();
        $this->expectException(NotInRangeException::class);
        $exampleObject->assertRange(11);
    }

    public function test_OK_Values(): void
    {
        $exampleObject = new ExampleObject();

        $exampleObject->assertRange(1);
        $exampleObject->assertRange(10);

        $this->addToAssertionCount(2); // Yey! Not thrown!
    }
}

示例2:使用try/catch块...

use PHPUnit\Framework\AssertionFailedError;
use PHPUnit\Framework\TestCase;
use Tests\ExampleObject;
use Tests\NotInRangeException;

class ExampleTest extends TestCase
{
    public function test_TryCatch(): void
    {
        $exampleObject = new ExampleObject();

        try {
            $exampleObject->assertRange(0);
            // I don't want to write so long error text everytime I am checking for exceptions!
            throw new AssertionFailedError(sprintf("Exception '%s' expected, but not thrown!", NotInRangeException::class));
        } catch (NotInRangeException $ex) {
            $this->addToAssertionCount(1); // Yey! Thrown!
        }

        $exampleObject->assertRange(1);
        $exampleObject->assertRange(10);
        $this->addToAssertionCount(2); // Yey! Not thrown!

        try {
            $exampleObject->assertRange(11);
            throw new AssertionFailedError(sprintf("Exception '%s' expected, but not thrown!", NotInRangeException::class));
        } catch (NotInRangeException $ex) {
            $this->addToAssertionCount(1); // Yey! Thrown!
        }

    }
}