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

Strange error in tests after update to phpunit 6 #2460

Closed
Mararok opened this issue Feb 3, 2017 · 19 comments
Closed

Strange error in tests after update to phpunit 6 #2460

Mararok opened this issue Feb 3, 2017 · 19 comments
Assignees
Labels
type/bug Something is broken

Comments

@Mararok
Copy link

Mararok commented Feb 3, 2017

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.

@sebastianbergmann sebastianbergmann added the type/bug Something is broken label Feb 3, 2017
@sebastianbergmann sebastianbergmann self-assigned this Feb 3, 2017
@sebastianbergmann
Copy link
Owner

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.

@Mararok
Copy link
Author

Mararok commented Feb 3, 2017

<?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');
    }
}

@sebastianbergmann
Copy link
Owner

sebastianbergmann commented Feb 3, 2017

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)

@Mararok
Copy link
Author

Mararok commented Feb 3, 2017

I used composer autoload

@sebastianbergmann
Copy link
Owner

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

@Mararok
Copy link
Author

Mararok commented Feb 3, 2017

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:

<?xml version="1.0" encoding="UTF-8"?>

<phpunit backupGlobals="false"
         backupStaticAttributes="false"
         bootstrap="vendor/autoload.php"
         colors="true"
         convertErrorsToExceptions="true"
         convertNoticesToExceptions="true"
         convertWarningsToExceptions="true"
         processIsolation="false"
         stopOnFailure="false"
         syntaxCheck="false"
>
    <testsuites>
        <testsuite name="integration">
            <directory suffix="Test.php">./tests/integration/</directory>
        </testsuite>
        <testsuite name="unit">
            <directory suffix="Test.php">./tests/unit/</directory>
        </testsuite>
    </testsuites>

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

@sebastianbergmann
Copy link
Owner

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

@Mararok
Copy link
Author

Mararok commented Feb 3, 2017

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.

@Mararok
Copy link
Author

Mararok commented Feb 3, 2017

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

@bug-tape
Copy link

bug-tape commented Feb 3, 2017

I have just the same problem:

Fatal error: Cannot use PHPUnit\Framework\Exception 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();
	}
}

@okoehler
Copy link

okoehler commented Feb 3, 2017

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 PHPUnit\Framework\Exception 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. PHPUnit\TextUI\Command::main($exit = uninitialized) /data/warehouse/vendor/phpunit/phpunit/phpunit:53
PHP 3. PHPUnit\TextUI\Command->run($argv = uninitialized, $exit = uninitialized) /data/warehouse/vendor/phpunit/phpunit/src/TextUI/Command.php:135
PHP 4. PHPUnit\TextUI\TestRunner->doRun($suite = uninitialized, $arguments = uninitialized, $exit = uninitialized) /data/warehouse/vendor/phpunit/phpunit/src/TextUI/Command.php:206
PHP 5. PHPUnit\Framework\TestSuite->run($result = uninitialized) /data/warehouse/vendor/phpunit/phpunit/src/TextUI/TestRunner.php:512
PHP 6. PHPUnit\Framework\TestSuite->run($result = uninitialized) /data/warehouse/vendor/phpunit/phpunit/src/Framework/TestSuite.php:739
PHP 7. PHPUnit\Framework\TestCase->run($result = uninitialized) /data/warehouse/vendor/phpunit/phpunit/src/Framework/TestSuite.php:739
PHP 8. PHPUnit\Framework\TestResult->run($test = uninitialized) /data/warehouse/vendor/phpunit/phpunit/src/Framework/TestCase.php:867
PHP 9. PHPUnit\Framework\TestCase->runBare() /data/warehouse/vendor/phpunit/phpunit/src/Framework/TestResult.php:688
PHP 10. PHPUnit\Framework\TestCase->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. Application\SimpleTest->testEquals() /data/warehouse/vendor/phpunit/phpunit/src/Framework/TestCase.php:1053
PHP 13. PHPUnit\Framework\Assert::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. Composer\Autoload\ClassLoader->loadClass($class = uninitialized) /data/warehouse/vendor/phpunit/phpunit/src/Framework/Assert.php:533
PHP 16. Composer\Autoload\includeFile($file = uninitialized) /data/warehouse/vendor/composer/ClassLoader.php:322

Process finished with exit code 255

If I rename PHPUnit\Framework\Exception to PHPUnit\Framework\PHPUnitException everything works fine.

@sebastianbergmann
Copy link
Owner

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);
    }
}
$ ./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)

@Trainmaster
Copy link

Trainmaster commented Feb 3, 2017

@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

sebastianbergmann added a commit that referenced this issue Feb 3, 2017
@sebastianbergmann
Copy link
Owner

Can you please check whether d109afe solves the problem?

@okoehler
Copy link

okoehler commented Feb 3, 2017

d109afe seems to have fixed the issue with PHP 7.0.12

@bug-tape
Copy link

bug-tape commented Feb 6, 2017

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

@devantoine
Copy link

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.

@MohammedAttya2
Copy link

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

stesie added a commit to stesie/phpcs-doctrine-annotation-rules that referenced this issue Aug 5, 2017
To get rid of sebastianbergmann/phpunit#2460
which hits this project on Travis CI.
@tajuddinkhandaker
Copy link

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 PHPUnit\Framework\TestSuite 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type/bug Something is broken
Projects
None yet
Development

No branches or pull requests

8 participants