Skip to content

VladaHejda/PhpUnitAssertException

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PhpUnitAssertException

AssertException is a trait, so it can be easily used in your test case.

  • For PHP 7.1 and PHPUnit 6 compatibility require version 1.3.
  • For PHP 7.1 compatibility require version 1.2.
  • For PHP 7 compatibility require version 1.1.
  • For PHP 5 compatibility require version 1.0.

Install

$ composer require vladahejda/phpunit-assert-exception

Usage

<?php

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

Also see an assertError and assertThrowable methods, which tests if PHP internal Error was thrown, or any Throwable respectively.

About

Assert exception/throwable/error PHPUnit trait

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •  

Languages