kenjis / ci-phpunit-test

An easier way to use PHPUnit with CodeIgniter 3.x.
http://kenjis.github.io/ci-phpunit-test/
MIT License
587 stars 195 forks source link

TestCase not found #374

Closed pgee70 closed 3 years ago

pgee70 commented 3 years ago

i have been using your framework for a number of years now.

using kenjis/ci-phpunit-test installed via composer, "kenjis/ci-phpunit-test": "3.x-dev", phpunit is also installed via composer: "phpunit/phpunit": "9.5.4"

I haven't changed my unit tests, which were working before the upgrade. i removed the cache folder after upgrading.

After recent updates i am having the problem of Error: Class 'TestCase' not found This error only occurs in tests that use controllers. for example:

# this works fine
mbp2016:tests pgee$pwd 
[site-path]/application/tests
mbp2016:tests pgee$ php ../../vendor/phpunit/phpunit/phpunit --no-coverage --group siteHelper
Unit test begins on 23:12:43 AEST 29th April 2021 with PHP v7.4.16
PHPUnit 9.5.4 by Sebastian Bergmann and contributors.

...............................................................  63 / 180 ( 35%)
............................................................... 126 / 180 ( 70%)
......................................................          180 / 180 (100%)

Time: 00:10.459, Memory: 93.50 MB

OK (180 tests, 302 assertions)
$php ../../vendor/phpunit/phpunit/phpunit --no-coverage --group my404Controller

Unit test begins on 23:15:07 AEST 29th April 2021 with PHP v7.4.16
PHPUnit 9.5.4 by Sebastian Bergmann and contributors.

EEE                                                                 3 / 3 (100%)

Time: 00:06.348, Memory: 74.50 MB

There were 3 errors:

1) i3Soft\tests\controllers\My404_test::test_index_cli
Error: Class 'TestCase' not found

[site-path]/vendor/kenjis/ci-phpunit-test/application/tests/_ci_phpunit_test/replacing/core/Common.php:112
[site-path]/vendor/kenjis/ci-phpunit-test/application/tests/_ci_phpunit_test/CIPHPUnitTestRequest.php:353
[site-path]/vendor/kenjis/ci-phpunit-test/application/tests/_ci_phpunit_test/CIPHPUnitTestRequest.php:297
[site-path]/vendor/kenjis/ci-phpunit-test/application/tests/_ci_phpunit_test/CIPHPUnitTestRequest.php:160
[site-path]/vendor/kenjis/ci-phpunit-test/application/tests/_ci_phpunit_test/CIPHPUnitTestCase.php:155
[site-path]/application/tests/controllers/My404_test.php:30
[site-path]/vendor/phpunit/phpunit/phpunit:61
... more errors continue

my tracing found that in the file vendor/kenjis/ci-phpunit-test/application/tests/_ci_phpunit_test/replacing/core/Common.php line 304:

    // Everything is done, so return
    // added by ci-phpunit-test
    if (TestCase::isTestingEnv())
    {
        return;
    }

the line 304 is TestCase::isTestingEnv(), and for some reason TestCase is not loaded. Furthermore my spl_autoload_register has a case to load TestCase, which it does, and the line still fails to find the class.

my TestCase.php is in application/tests and extends CIPHPUnitTestCase abstract class TestCase extends CIPHPUnitTestCase all my tests then extend TestCase.

So any help working out what i have done wrong with the update from the earlier version would be appreciated!

pgee70 commented 3 years ago

ok, i have found a fix for this in your library, on version 3.0.0 i changed all the instances of TestCase::isTestingEnv() to CIPHPUnitTestCase::isTestingEnv() and the issue resolves. This makes sense because your code-base is not name-spaced, and mine is. ie my tests are name-spaced as i3soft\tests and use psr autoloading. By explicitly referencing CIPHPUnitTestCase i run your code.

kenjis commented 3 years ago

@pgee70 Yes, ci-phpunit-test requires not name-spaced TestCase::isTestingEnv(). This is so that CIPHPUnitTestCase::isTestingEnv() can be overridden. To override the method you can change the environment testing during test execution: https://github.com/kenjis/ci-phpunit-test/blob/202441b1d2143eb602a3804854297da1d23dc4d5/application/tests/_ci_phpunit_test/CIPHPUnitTestCase.php#L467-L470

If you don't change testing, your solution is no problem.

Another solution is to make a class alias:

class_alias('i3soft\tests\TestCase', 'TestCase');