Phpunit: Strange error in tests after update to phpunit 6

Created on 3 Feb 2017  路  19Comments  路  Source: sebastianbergmann/phpunit

One of my tests was green in 5.7 now is red :( . In that test I used $this->callback() and error is

PHP Fatal error:  Cannot use PHPUnit\Framework\Exception as Exception because the name is already in use in <pathToMyProject>\vendor\phpunit\phpunit\src\Framework\Constraint\Callback.php on line 12

Its strange :). When I removed line:

use PHPUnit\Framework\Exception;

from that file all tests passed.

typbug

Most helpful comment

Can you please check whether d109afe3e4bf2f5e176a9baa95f6cf015135071e solves the problem?

All 19 comments

Thank you for your report.

Please provide a minimal, self-contained, reproducing test case that shows the problem you are reporting.

Without such a minimal, self-contained, reproducing test case I will not be able to investigate this issue.

<?php
use PHPUnit\Framework\TestCase;

class Simple
{
    public function a($a1)
    {
    }
}

class SimpleTest extends TestCase
{
    public function testSimple()
    {
        $mock = $this->createMock(Simple::class);

        $mock->expects($this->once())
             ->method('a')
             ->with($this->callback(
                 function ($a) { return $a === 'b'; }
             ));

        $mock->a('b');
    }
}

I cannot reproduce this:

$ phpunit SimpleTest     
PHPUnit 6.0.0 by Sebastian Bergmann and contributors.

.                                                                   1 / 1 (100%)

Time: 42 ms, Memory: 4.00MB

OK (1 test, 1 assertion)

I used composer autoload

As am I. I just don't use it to load the class under test.

I am running tests in PhpStorm with run configuration and settings of PHPUnit: "use composer autoload" with path to vendor. This settings are working good in 5.7.

command in console:

<pathToPhp>\php.exe <pathToMyProject>/vendor/phpunit/phpunit/phpunit --configuration <pathToMyProject>\phpunit.xml --teamcity

My phpunit.xml:
```

backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
>


./tests/integration/


./tests/unit/

<filter>
    <whitelist>
        <directory>./src/</directory>
    </whitelist>
</filter>

Are you absolutely sure that you are not mixing different versions of PHPUnit?

I changed only my composer.json deps from "phpunit/phpunit": "5." to "phpunit/phpunit": "6."
And in output I can see line:
```
PHPUnit 6.0.0 by Sebastian Bergmann and contributors.

SimpleTestCase is failed only when is in suite with other tests in my project, I tested it alone in other suite and it passed. After selective check I found what can give fatal error you need second TestCase:

<?php

use PHPUnit\Framework\TestCase;

class Simple2Test extends TestCase {

    public function testSimple() {
        $this->expectException(\InvalidArgumentException::class);
        throw new \InvalidArgumentException();
    }
}

And run it in same suite with processIsolation="false".

I have just the same problem:

Fatal error: Cannot use PHPUnitFrameworkException as Exception because the name is already in use in .../vendor/phpunit/phpunit/src/Framework/Constraint/ExceptionMessageRegularExpression.php

Following is enough to get this error:

<?php
use PHPUnit\Framework\TestCase;

class MyClass {
    public function do() {
        throw new \Exception('FATAL');
    }
}

class MyClassTest extends TestCase {
    /**
     * @expectedException \Exception
     * @expectedExceptionMessageRegExp FATAL
     */
    public function testDo() {
        (new MyClass())->do();
    }
}

Same problem here:

<?php
namespace Application;

use PHPUnit\Framework\TestCase;

class SimpleTest extends TestCase
{
    public function testSimpleException()
    {
        $this->expectException(\Exception::class);
        throw new \Exception('simple');
    }

    public function testEquals()
    {
        $this->assertEquals(1, 1);
    }
}

PHPUnit 6.0.1 by Sebastian Bergmann and contributors.

PHP Fatal error: Cannot use PHPUnitFrameworkException as Exception because the name is already in use in /data/warehouse/vendor/phpunit/phpunit/src/Framework/Constraint/IsEqual.php on line 12
PHP Stack trace:
PHP 1. {main}() /data/warehouse/vendor/phpunit/phpunit/phpunit:0
PHP 2. PHPUnitTextUICommand::main($exit = uninitialized) /data/warehouse/vendor/phpunit/phpunit/phpunit:53
PHP 3. PHPUnitTextUICommand->run($argv = uninitialized, $exit = uninitialized) /data/warehouse/vendor/phpunit/phpunit/src/TextUI/Command.php:135
PHP 4. PHPUnitTextUITestRunner->doRun($suite = uninitialized, $arguments = uninitialized, $exit = uninitialized) /data/warehouse/vendor/phpunit/phpunit/src/TextUI/Command.php:206
PHP 5. PHPUnitFrameworkTestSuite->run($result = uninitialized) /data/warehouse/vendor/phpunit/phpunit/src/TextUI/TestRunner.php:512
PHP 6. PHPUnitFrameworkTestSuite->run($result = uninitialized) /data/warehouse/vendor/phpunit/phpunit/src/Framework/TestSuite.php:739
PHP 7. PHPUnitFrameworkTestCase->run($result = uninitialized) /data/warehouse/vendor/phpunit/phpunit/src/Framework/TestSuite.php:739
PHP 8. PHPUnitFrameworkTestResult->run($test = uninitialized) /data/warehouse/vendor/phpunit/phpunit/src/Framework/TestCase.php:867
PHP 9. PHPUnitFrameworkTestCase->runBare() /data/warehouse/vendor/phpunit/phpunit/src/Framework/TestResult.php:688
PHP 10. PHPUnitFrameworkTestCase->runTest() /data/warehouse/vendor/phpunit/phpunit/src/Framework/TestCase.php:912
PHP 11. ReflectionMethod->invokeArgs(uninitialized, uninitialized) /data/warehouse/vendor/phpunit/phpunit/src/Framework/TestCase.php:1053
PHP 12. ApplicationSimpleTest->testEquals() /data/warehouse/vendor/phpunit/phpunit/src/Framework/TestCase.php:1053
PHP 13. PHPUnitFrameworkAssert::assertEquals($expected = uninitialized, $actual = uninitialized, $message = uninitialized, $delta = uninitialized, $maxDepth = uninitialized, $canonicalize = uninitialized, $ignoreCase = uninitialized) /data/warehouse/module/Application/test/SimpleTest.php:29
PHP 14. spl_autoload_call(uninitialized) /data/warehouse/vendor/phpunit/phpunit/src/Framework/Assert.php:533
PHP 15. ComposerAutoloadClassLoader->loadClass($class = uninitialized) /data/warehouse/vendor/phpunit/phpunit/src/Framework/Assert.php:533
PHP 16. ComposerAutoloadincludeFile($file = uninitialized) /data/warehouse/vendor/composer/ClassLoader.php:322

Process finished with exit code 255

If I rename PHPUnitFrameworkException to PHPUnitFrameworkPHPUnitException everything works fine.

I cannot reproduce this:

$ cat composer.json 
{
    "require": {
        "phpunit/phpunit": "^6.0"
    }
}
$ cat tests/SimpleTest.php 
<?php
namespace Application;

use PHPUnit\Framework\TestCase;

class SimpleTest extends TestCase
{
    public function testSimpleException()
    {
        $this->expectException(\Exception::class);
        throw new \Exception('simple');
    }

    public function testEquals()
    {
        $this->assertEquals(1, 1);
    }
}



md5-565237e8cdd842d5ee946b6e0e286c60



$ ./vendor/bin/phpunit --bootstrap vendor/autoload.php tests
PHPUnit 6.0.2 by Sebastian Bergmann and contributors.

..                                                                  2 / 2 (100%)

Time: 27 ms, Memory: 4.00MB

OK (2 tests, 2 assertions)

@sebastianbergmann I'm facing the same problems and I tested with PHP 7.0.5. After updating to PHP 7.0.15 the problem is gone.

Update: It's also not working with PHP 7.0.11

Update #2: PHP 7.0.12 also quits with an error, but it works with PHP 7.0.13

Update #3: Confirmed with https://travis-ci.org/Trainmaster/Vision/builds/198133136

Can you please check whether d109afe3e4bf2f5e176a9baa95f6cf015135071e solves the problem?

d109afe seems to have fixed the issue with PHP 7.0.12

With PHP 7.0.15 the problem is gone for me, too.
Thanks to @sebastianbergmann and @Trainmaster

I can confirm this issue with PHP 7.0.15 on Ubuntu 16.04 though mine is not exactly the same.
I had PHPUnit 5.1.3 installed localy and version 6.2.1 using composer. When running tests with phpunit 6.2.1 and --teamcity option (used by PHPStorm), I got the following error:

PHP Fatal error: Class 'PHPUnit_TextUI_ResultPrinter' not found in /usr/share/php/PHPUnit/Util/Log/TeamCity.php on line 19

If I remove local PHPUnit 5.1.3 it works fine.

I had this problem after installing phpunit 6.2.3 globally and using laravel framework 5.4
the problem solved after updating the composer.json file

For phpunit 6.5.5 version the problem exists.

$ phpunit

PHP Warning: is_dir() expects parameter 1 to be a valid path, object given in /usr/share/php/PHPUnit/Runner/BaseTestRunner.php on line 56
PHP Recoverable fatal error: Object of class PHPUnitFrameworkTestSuite could not be converted to string in /usr/share/php/PHPUnit/Runner/StandardTestSuiteLoader.php on line 32

$ phpunit --version
PHPUnit 6.5.5 by Sebastian Bergmann and contributors.

Was this page helpful?
0 / 5 - 0 ratings