PHP code example of zrnik / phpunit-exceptions

1. Go to this page and download the library: Download zrnik/phpunit-exceptions library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

zrnik / phpunit-exceptions example snippets


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);
            }
        );
    }
}

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!
    }
}

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!
        }

    }
}

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