Skip to content
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

cannot expect empty exception message #2731

Closed
keradus opened this issue Jul 6, 2017 · 8 comments · Fixed by N0rthernL1ghts/eloquent-bootstrap#2 or Zemhart/project_UAS_PHP#20
Closed
Assignees
Labels
type/bug Something is broken

Comments

@keradus
Copy link
Contributor

keradus commented Jul 6, 2017

Q A
PHPUnit version 5.7.21, but probably lowers as well
PHP version 7.1.1-1, but not related
Installation Method Composer

I do know it's an edge case, but today I wanted to ensure that exception was raised with empty message:

$tihs->expectExceptionMessage('');

sadly, it's not working - it behaves like method is not called at all.

reason is here:
https://github.com/sebastianbergmann/phpunit/blob/5.7/src/Framework/TestCase.php#L1089

setting up the value with null and with empty string are treated the same way.
IMO, only null shall be checked there

@sebastianbergmann sebastianbergmann self-assigned this Oct 13, 2017
@sebastianbergmann sebastianbergmann added type/bug Something is broken 5.7 labels Oct 13, 2017
@wesolowski
Copy link
Contributor

i can't reproduce them. This code work fine:

<?php

class Example
{
    public function foo()
    {
        throw new \Exception('');
    }
}

class ExampleTest extends PHPUnit_Framework_TestCase
{
    public function testFoo()
    {
        $this->expectExceptionMessage('');
        (new \Example())->foo();
    }
}

Console Output:
screen


But i find another error.
This Code don't throws any error.

<?php

class Example
{
    public function foo()
    {
        throw new \Exception('One');
    }
}

class ExampleTest extends PHPUnit_Framework_TestCase
{
    public function testFoo()
    {
        $this->expectExceptionMessage('');
        (new \Example())->foo();
    }
}

@keradus
Copy link
Contributor Author

keradus commented Oct 13, 2017

it's not throwing error because expectExceptionMessage expect exception to be raised (configured previously via expectException or any \Exception if not), and then msg is not checked as I described

@sebastianbergmann
Copy link
Owner

expectExceptionMessage() etc. can be used without using expectException():

<?php
class Test extends PHPUnit\Framework\TestCase
{
    public function testOne()
    {
        $this->expectExceptionMessage('message');
    }
}

Before 6727744 the above would result in

PHPUnit 6.4.1-5-ge0bc901f2 by Sebastian Bergmann and contributors.

F                                                                   1 / 1 (100%)

Time: 33 ms, Memory: 4.00MB

There was 1 failure:

1) Test::testOne
Failed asserting that exception of type "Exception" is thrown.

FAILURES!
Tests: 1, Assertions: 1, Failures: 1.

I found that confusing and have changed it to

PHPUnit 6.4.1-6-g672774443 by Sebastian Bergmann and contributors.

F                                                                   1 / 1 (100%)

Time: 46 ms, Memory: 4.00MB

There was 1 failure:

1) Test::testOne
Failed asserting that exception with message "message" is thrown

FAILURES!
Tests: 1, Assertions: 1, Failures: 1.

now.

@sebastianbergmann
Copy link
Owner

I probably/subconsciously fixed this issue as part of 6727744.

@keradus
Copy link
Contributor Author

keradus commented Oct 13, 2017

@sebastianbergmann is 5.x line closed? bug is there since 5.x

@sebastianbergmann
Copy link
Owner

I'll look into backporting this to PHPUnit 5.7 (which still gets bug fixes until February 2018).

@keradus
Copy link
Contributor Author

keradus commented Oct 14, 2017

damn, it's hard to track changes when they are commited directly to branch... :(

567ca23#commitcomment-24975103

@remicollet
Copy link
Contributor

Notice that empty exception can be test without any change

$this->expectExceptionMessageRegExp('/^$/')

This fix in 5.7 introduce a BC break in how $this->expectExceptionMessage('') is handled.

See

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment