Skip to content
This repository was archived by the owner on Apr 12, 2018. It is now read-only.

Exceptions note clarification #300

Closed
serbanghita opened this issue Apr 23, 2015 · 1 comment
Closed

Exceptions note clarification #300

serbanghita opened this issue Apr 23, 2015 · 1 comment

Comments

@serbanghita
Copy link

https://phpunit.de/manual/current/en/writing-tests-for-phpunit.html#writing-tests-for-phpunit.exceptions.examples.ErrorTest.php

There is a not on the link above saying:

You should be as specific as possible when testing exceptions. Testing for classes that are too generic might lead to undesirable side-effects. Accordingly, testing for the Exception class with @ExpectedException or setExpectedException() is no longer permitted.

I tested this and the tests pass:

<?php
namespace UnitTests\ExceptionsTest;

class MyClass
{
    protected $aString;

    public function __construct($aString)
    {
        if (empty($aString)) {
            throw new \Exception('Empty string provided.');
        }

        $this->aString = $aString;
    }

    public function checkString()
    {
        if (is_string($this->aString)) {
            return true;
        } else {
            throw new \Exception('This is not a string!');
        }
    }
}
<?php
use UnitTests\ExceptionsTest\MyClass;

class MyClassTest extends \PHPUnit_Framework_TestCase
{

    /**
     * instantiating the constructor with an empty string throws an exception
     * @expectedException \Exception
     */
    public function testInstantiatingTheConstructorWithAnEmptyStringThrowsAnException()
    {
        $myClass = new MyClass('');
    }


    /**
     * instantiating the constructor with a number and calling checkString will throw an exception
     * @expectedException \Exception
     */
    public function testInstantiatingTheConstructorWithANumberAndCallingCheckStringWillThrowAnException()
    {
        $myClass = new MyClass(11);
        $myClass->checkString();
    }


}

Aparently testing for Exception is permitted. I'm using:

$ php phpunit.phar -c tests/phpunit.xml
PHPUnit 4.6.4 by Sebastian Bergmann and contributors.
@sebastianbergmann
Copy link
Owner

Dear contributor,

thanks to the hard work by @belanur, @starikovm, @trueromio, and others, the changes outlined in #471 (comment) have been implemented.

This repository, https://github.com/sebastianbergmann/phpunit-documentation, is now archived and read-only. The documentation for PHPUnit version prior to PHPUnit 7.0 will no longer be changed or updated. It is still hosted at

  • Multiple HTML files: https://phpunit.de/manual/6.5/en/index.html
  • Multiple HTML files (in a .tar.bz2 archive): https://phpunit.de/manual/current/en/download.tar.bz2
  • Single HTML file: https://phpunit.de/manual/current/en/phpunit-book.html
  • PDF: https://phpunit.de/manual/current/en/phpunit-book.pdf
  • ePub: https://phpunit.de/manual/current/en/phpunit-book.epub

Simply replace 6.5 with the version number you are looking for. Simply replace en with fr, pt_br, ja, or zh_cn to access the French, Brazilian Portuguese, Japanese, or Simplified Chinese translation, respectively.

Starting with the documentation for PHPUnit 7.0, the PHPUnit documentation is hosted at https://phpunit.readthedocs.io/.

I am sorry that I have to close this issue here as GitHub does not support moving issues from one project to another. Please open a new ticket for this issue in one of the new issue trackers (see above).

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants