New issue

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

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

Already on GitHub? Sign in to your account

PHPunit not expect generic exception? #454

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

Comments

8 participants
@yangzizhe

yangzizhe commented Jan 6, 2012

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

This comment has been minimized.

Show comment
Hide comment
@sebastianbergmann

sebastianbergmann Jan 6, 2012

Owner

Fixed in the documentation.

Owner

sebastianbergmann commented Jan 6, 2012

Fixed in the documentation.

@digitalprecision

This comment has been minimized.

Show comment
Hide comment
@digitalprecision

digitalprecision May 24, 2012

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?

digitalprecision commented May 24, 2012

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

This comment has been minimized.

Show comment
Hide comment
@sebastianbergmann

sebastianbergmann May 25, 2012

Owner

The restriction has been removed for PHPUnit 3.7.

Owner

sebastianbergmann commented May 25, 2012

The restriction has been removed for PHPUnit 3.7.

@digitalprecision

This comment has been minimized.

Show comment
Hide comment
@digitalprecision

digitalprecision May 25, 2012

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

digitalprecision commented May 25, 2012

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

@breerly

This comment has been minimized.

Show comment
Hide comment
@breerly

breerly Aug 22, 2012

Any way this can get brought into 3.6?

breerly commented Aug 22, 2012

Any way this can get brought into 3.6?

@digitalprecision

This comment has been minimized.

Show comment
Hide comment

digitalprecision commented Aug 24, 2012

@breerly

This comment has been minimized.

Show comment
Hide comment
@breerly

breerly Aug 27, 2012

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

breerly commented Aug 27, 2012

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

@sebastianbergmann

This comment has been minimized.

Show comment
Hide comment
@sebastianbergmann

sebastianbergmann Aug 27, 2012

Owner

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

Owner

sebastianbergmann commented Aug 27, 2012

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

@iNamik

This comment has been minimized.

Show comment
Hide comment
@iNamik

iNamik 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.

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

This comment has been minimized.

Show comment
Hide comment
@joaoinacio

joaoinacio Feb 25, 2013

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?

joaoinacio commented Feb 25, 2013

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

This comment has been minimized.

Show comment
Hide comment
@digitalprecision

digitalprecision Feb 26, 2013

@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.

digitalprecision commented Feb 26, 2013

@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

This comment has been minimized.

Show comment
Hide comment
@srcspider

srcspider Jan 13, 2014

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());
}

srcspider commented Jan 13, 2014

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

This comment has been minimized.

Show comment
Hide comment
@jtsternberg

jtsternberg Feb 19, 2015

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

jtsternberg commented Feb 19, 2015

@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

Work around a PHPUnit issue on PHP 5.2
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment