Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

PHPunit not expect generic exception? #454

Closed
yangzizhe opened this issue Jan 6, 2012 · 13 comments
Closed

PHPunit not expect generic exception? #454

yangzizhe opened this issue Jan 6, 2012 · 13 comments

Comments

@yangzizhe
Copy link

InvalidArgumentException: You must not expect the generic exception class,
when I test

class MyTest extends PHPUnit_Framework_TestCase
{
/**
* @ExpectedException Exception
* @expectedExceptionMessage Some Message
*/
public function testExceptionHasRightMessage()
{
throw new Exception('Some Message', 20);
}
}

with PHPUnit3.6.7
why ?

IF PHPunit not expect generic exception , why the document has this example?

@sebastianbergmann
Copy link
Owner

Fixed in the documentation.

@digitalprecision
Copy link

For the record, this is a fairly critical issue as we are unable to throw generic exceptions, and personally I don't feel like creating custom exceptions just to pass unit testing. Is there any way you could remove this restriction?

@sebastianbergmann
Copy link
Owner

The restriction has been removed for PHPUnit 3.7.

@digitalprecision
Copy link

Thanks for responding. When will 3.7 be available? Stable?

@HelloGrayson
Copy link

Any way this can get brought into 3.6?

@digitalprecision
Copy link

I came up with a work around until 3.7 is stable. http://melikedev.com/2012/08/24/phpunit-you-must-not-expect-the-generic-exception-class/

@HelloGrayson
Copy link

@digitalprecision that's good but less ideal - cc @sebastianbergmann - do you advise upgrade to 3.7?

@sebastianbergmann
Copy link
Owner

Feel free to test PHPUnit 3.7.0RC1; that is what release candidates are for.

@iNamik
Copy link

iNamik commented Dec 15, 2012

Inspired by @digitalprecision I designed a robust solution that is easier to bolt onto your test cases:

https://gist.github.com/4298350

The gist includes 3 ways to incorporate generic exception handling into your test cases:

  • Via trait if your PHP version supports it
  • Via extending a new base TestCase class
  • Via global functions

The gist code includes all three versions, but you can delete the versions you don't want (or can't compile) from your copy of the code.

There is also a full test suite available to test the functionality.

@joaoinacio
Copy link

Much simpler to patch phpunit, something like so:
/usr/share/php/PHPUnit/Framework/TestCase.php:

-    if ($exceptionName == 'Exception' || $exceptionName == '\\Exception') {
-        throw new InvalidArgumentException(
-            'You must not expect the generic exception class.'
-        );
-    }

Is there a commit from 3.7 that could possibly apply to 3.6?

@digitalprecision
Copy link

@joaoinacio: Patching wasn't an option for us as we have multiple UT servers and we didn't want to be responsible for maintaining vendor code. The author was asked if he would backport to 3.6 but never responded directly to the question, rather he suggested we beta test 3.7. You can use mine or @iNamik suggested work arounds.

@srcspider
Copy link

For anyone using travis with 5.2 due to dependencies to other systems that refuse to drop 5.2 you can use this pattern to bypass the phpunit limitation, it's also slightly better.

try {
    $proc->errors(); // if this doesn't throw an exception, the next line will fail the test
    throw new Exception('Failed asserting blah blah blah exception.');
}
catch (Exception $e) {
    // ensure we got the right exception
    $this->assertEquals('Place here the expected Exception message.', $e->getMessage());
}

@jtsternberg
Copy link

@srcspider sorry, where would I use that? In the test itself? and if so, what is the $proc variable in your example?

JDGrimes added a commit to JDGrimes/wp-filesystem-mock that referenced this issue Mar 26, 2015
PHPUnit 3.6 didn’t like you to use `@expectedException` with a generic
`Exception`:

> InvalidArgumentException: You must not expect the generic exception
class.

See
sebastianbergmann/phpunit#454 (comment)
71137
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants