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

Must implement interface PHPUnit_Framework_Test #376

Closed codeasashu closed 3 years ago

codeasashu commented 3 years ago

composer.json

{
  "description": "The CodeIgniter framework",
  "name": "codeigniter/framework",
  "type": "project",
  "homepage": "https://codeigniter.com",
  "license": "MIT",
  "require": {
    "php": ">=5.3",
  },
  "require-dev": {
    "mikey179/vfsstream": "1.1.*",
    "phpunit/phpunit": "^9.3"
  }
}

I've installed this project using zip method. Now, whenever I try to run:

vendor/bin/phpunit -c application/tests/phpunit.xml application/tests/controllers/Example_test.php

it results in following error:

An uncaught Exception was encountered

Type:        PHPUnit\TextUI\RuntimeException
Message:     Argument 1 passed to PHPUnit_Framework_TestSuite::addTest() must implement interface PHPUnit_Framework_Test, instance of Example_Test given, called in /Users/ashutoshchaudhary/code/test/application/vendor/phpunit/phpunit/src/Framework/TestSuite.php on line 564
Filename:    /Users/ashutoshchaudhary/code/test/vendor/phpunit/phpunit/src/TextUI/Command.php
Line Number: 98

Backtrace:
        File: /Users/ashutoshchaudhary/code/test/vendor/phpunit/phpunit/phpunit
        Line: 76
        Function: main

When I remove the configuration, it works but then I can not use resetInstances method. For example:

vendor/bin/phpunit application/tests/controllers/Example_test.php

Results in following:

PHPUnit 9.5.6 by Sebastian Bergmann and contributors.

E                                                                   1 / 1 (100%)

Time: 00:00.025, Memory: 4.00 MB

There was 1 error:

1) Example_Test::testExample
Error: Call to undefined method Example_Test::resetInstance()

/Users/ashutoshchaudhary/code/test/application/tests/controllers/Example_test.php:9

ERRORS!
Tests: 1, Assertions: 0, Errors: 1.
kenjis commented 3 years ago

It seems it is a problem in Example_test.php, not in ci-phpunit-test. Can you show it?

If you remove configuration, ci-phpunit-test does not work correctly.

codeasashu commented 3 years ago

As it turns out, I had two composer.json installed, one in <project root>/vendor and the other in <project root>/application/vendor. I had composer_autoload=true set in config, which was autoloading application/vendor, while I was using PHPUnit from root vendor location. Setting composer_autoload=false fixed the issue for me

kenjis commented 3 years ago

Good to hear it.

If you want to autoload composer packages in <project root>/vendor:

$config['composer_autoload'] = realpath(APPPATH . '../vendor/autoload.php');