Skip to main content
added note about throwable
Source Link
jchook
  • 7.1k
  • 5
  • 40
  • 42

Since I strongly disagreewanted more withthan the current expectException implementation, I made a trait to use on my test cases. It's only 50~50 lines of code.

  • Supports multiple exceptions per test
  • Supports assertions called after the exception is thrown
  • Robust and clear usage examples
  • Standard assert syntax
  • Supports assertions for more than just message, code, and class
  • Supports inverse assertion, assertNotThrows
  • Supports PHP 7 Throwable errors

Since I strongly disagree with the current expectException implementation, I made a trait to use on my test cases. It's only 50 lines.

  • Supports multiple exceptions per test
  • Supports assertions called after the exception is thrown
  • Robust and clear usage examples
  • Standard assert syntax
  • Supports assertions for more than just message, code, and class
  • Supports inverse assertion, assertNotThrows

Since I wanted more than the current expectException implementation, I made a trait to use on my test cases. It's only ~50 lines of code.

  • Supports multiple exceptions per test
  • Supports assertions called after the exception is thrown
  • Robust and clear usage examples
  • Standard assert syntax
  • Supports assertions for more than just message, code, and class
  • Supports inverse assertion, assertNotThrows
  • Supports PHP 7 Throwable errors
Positivism
Source Link
jchook
  • 7.1k
  • 5
  • 40
  • 42

PHPUnit's current "best practices" for exception testing seem.. lackluster (docs).

Since I strongly disagree with the current expectException implementation, I made a trait to use on my test cases. It's only 50 lines.

  • Doesn't supportSupports multiple exceptions per test
  • Doesn't supportSupports assertions called after the exception is thrown
  • Documentation lacks robust orRobust and clear usage examples
  • Non-standard and confusing syntaxStandard ("expect" vs "assert")assert syntax
  • Only supportsSupports assertions for more than just message, code, and class
  • NoSupports inverse assertion, such as "expectNoException"assertNotThrows

I opened a Github issue for PHPUnit and was immediately dismissed by the maintainer.

Since I strongly disagree with the current expectException implementation, I made a trait to use on my test cases.

PHPUnit's current "best practices" for exception testing seem.. lackluster.

  • Doesn't support multiple exceptions per test
  • Doesn't support assertions called after the exception is thrown
  • Documentation lacks robust or clear examples
  • Non-standard and confusing syntax ("expect" vs "assert")
  • Only supports assertions for message, code, and class
  • No inverse, such as "expectNoException"

I opened a Github issue for PHPUnit and was immediately dismissed by the maintainer.

Since I strongly disagree with the current expectException implementation, I made a trait to use on my test cases.

PHPUnit's current "best practices" for exception testing seem.. lackluster (docs).

Since I strongly disagree with the current expectException implementation, I made a trait to use on my test cases. It's only 50 lines.

  • Supports multiple exceptions per test
  • Supports assertions called after the exception is thrown
  • Robust and clear usage examples
  • Standard assert syntax
  • Supports assertions for more than just message, code, and class
  • Supports inverse assertion, assertNotThrows
E-Prime
Source Link
jchook
  • 7.1k
  • 5
  • 40
  • 42

Comprehensive Solution

PHPUnit's current "best practices" for exception testing areseem.. lackluster.

  • Doesn't support multiple exceptions per test, or
  • Doesn't support assertions called after the exception is thrown
  • Documentation lacks robust or clear examples
  • Non-standard and potentially confusing syntax ("expect" vs "assert")
  • Only supports assertions for message, code, and class
  • No inverse, such as "expectNoException"

TheI published the AssertThrows trait is published to Github and packagist so it can be installed with composer.

<?php

// WithinUsing yoursimple testcallback
$this->assertThrows(MyException::class, case...[$obj, 'doSomethingBad']);

// Using anonymous function
$this->assertThrows(MyException::class, function() use ($obj) {
    $obj->doSomethingBad();
});

Here is an actual TestCase class that showsPlease see below for a more comprehensive usage example:

PHPUnit's current "best practices" for exception testing are.. lackluster.

  • Doesn't support multiple exceptions per test, or assertions called after the exception is thrown
  • Documentation lacks robust or clear examples
  • Non-standard and potentially confusing syntax ("expect" vs "assert")
  • Only supports assertions for message, code, and class
  • No inverse, such as "expectNoException"

The AssertThrows trait is published to Github and packagist so it can be installed with composer.

<?php

// Within your test case...
$this->assertThrows(MyException::class, function() use ($obj) {
    $obj->doSomethingBad();
});

Here is an actual TestCase class that shows a more comprehensive usage example:

Comprehensive Solution

PHPUnit's current "best practices" for exception testing seem.. lackluster.

  • Doesn't support multiple exceptions per test
  • Doesn't support assertions called after the exception is thrown
  • Documentation lacks robust or clear examples
  • Non-standard and confusing syntax ("expect" vs "assert")
  • Only supports assertions for message, code, and class
  • No inverse, such as "expectNoException"

I published the AssertThrows trait to Github and packagist so it can be installed with composer.

<?php

// Using simple callback
$this->assertThrows(MyException::class, [$obj, 'doSomethingBad']);

// Using anonymous function
$this->assertThrows(MyException::class, function() use ($obj) {
    $obj->doSomethingBad();
});

Please see below for a more comprehensive usage example:

improved formatting
Source Link
jchook
  • 7.1k
  • 5
  • 40
  • 42
Loading
published AssertThrows to packagist
Source Link
jchook
  • 7.1k
  • 5
  • 40
  • 42
Loading
Source Link
jchook
  • 7.1k
  • 5
  • 40
  • 42
Loading