Phpunit: Cannot declare class, because the name is already in use... on this specific class name only

Created on 5 Oct 2017  路  8Comments  路  Source: sebastianbergmann/phpunit

| Q | A
| --------------------| ---------------
| PHPUnit version | 6.3.1
| PHP version | 7.0.22
| Installation Method | Composer

I only have this error for this specific test out of many tests I have.

Test is generated by laravel. The error shows up ONLY if I name it to ReserveCageTest. It works if you remove any letter from it (i.g ReserveCagTest, ReservCageTest)

Location is projectRoot/tests/Unit/Cage/ReserveCageTest.php

tried renaming the file to anything but error still shows up unless I change the class name. Is it a reserved class name? Intended?

Error will always be:

PHP Fatal error: Cannot declare class TestsUnitCageReserveCageTest, because the name is already in use in tests/Unit/Cage/RTest.php (or whatever filename) on line 10

Most helpful comment

@shenmusam It looks as thought you may have a require or include instead of require_once or include_once for the autoload file (Check in the phpunit bootstrap file).

All 8 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.

@sebastianbergmann not sure if this is a laravel issue also. Anyway, here's my test:

https://github.com/laravel/framework/blob/5.5/src/Illuminate/Foundation/Testing/TestCase.php

class ReserveCageTest extends TestCase
{
}

just by declaring that class name it wouldn't work anymore. I just appended an _ to the end ReserveCageTest_ to make it work temporarily

PHPUnit does not have a class named ReserveCageTest, neither does any of PHPUnit's dependencies. There is also no special treatment in PHPUnit's code for test case classes named ReserveCageTest, in face, the string ReserveCageTest does not appear in the code of PHPUnit or its dependencies at all.

This is not a PHPUnit issue. It is either an issue in your code or a library that your code uses.

m 2fr75 jyc nv_9if_qt5g
what should I do? @sebastianbergmann

This is not a discussion or support forum, sorry.

@shenmusam It looks as thought you may have a require or include instead of require_once or include_once for the autoload file (Check in the phpunit bootstrap file).

You probably have an include or require that "imports" that class in another Test , I face the same problem, in my current test I was import it as ..._once but another test with an include to the same file was causing this even when I filter the test to only the specific class with the --filter option.

Im having the same issue here,
Im using laravel 5.6, also tried using require_once but it will break the test,

Now Im using --filter option which should not be, are there any ways to resolve this issue?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

keradus picture keradus  路  3Comments

sebastianbergmann picture sebastianbergmann  路  3Comments

AnmSaiful picture AnmSaiful  路  4Comments

edyan picture edyan  路  4Comments

sebastianbergmann picture sebastianbergmann  路  4Comments