PHP code example of vladahejda / phpunit-assert-exception

1. Go to this page and download the library: Download vladahejda/phpunit-assert-exception 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/ */

    

vladahejda / phpunit-assert-exception example snippets




class MyTest extends \PHPUnit\Framework\TestCase
{
	use VladaHejda\AssertException;

	public function testMultipleExceptionsAtOnce()
	{
		$test = function () {
			// here comes some test stuff of your unit (tested class)
			// which you expect that will throw an Exception
			throw new InvalidArgumentException('Some message 12345', 100);
		};

		// just test if function throws an Exception
		$this->assertException($test); // pass

		// test class of an Exception
		$this->assertException($test, InvalidArgumentException::class); // pass

		// test Exception code
		$this->assertException($test, null, 100); // pass

		// test Exception message
		$this->assertException($test, null, null, 'Some message 12345'); // pass
		$this->assertException($test, null, null, 'Some message'); // also pass, because it checks on substring level

		// test all
		$this->assertException($test, InvalidArgumentException::class, 100, 'Some message 12345'); // pass

		// and here some failing tests
		// wrong class
		$this->assertException($test, ErrorException::class); // fail
		// wrong code
		$this->assertException($test, InvalidArgumentException::class, 200); // fail
		// wrong message
		$this->assertException($test, InvalidArgumentException::class, 100, 'Bad message'); // fail
	}
}