- Notifications
You must be signed in to change notification settings - Fork194
An easier way to use PHPUnit with CodeIgniter 3.x.
License
kenjis/ci-phpunit-test
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
An easier way to use PHPUnit withCodeIgniter 3.x.
- You don't have to modify CodeIgniter core files at all.
- You can write controller tests easily.
- Nothing is untestable, maybe.
- Well documented.
- PHP 7.3 or later
- If you use Monkey Patching, you must use PHP-Parser 4.2 or later as a Composer dependency.
- CodeIgniter 3.x
- If you want to upgrade to CodeIgniter4, see#361.
- PHPUnit 9.3 or later
- If you want to use PHPUnit 9.2 or earlier, please use ci-phpunit-test2.x.
- NetBeans
- Go toProject Properties > Testing > PHPUnit, checkUse Custom Test Suite checkbox, and select
application/tests/_ci_phpunit_test/TestSuiteProvider.php
.
- Go toProject Properties > Testing > PHPUnit, checkUse Custom Test Suite checkbox, and select
SeeChange Log.
codeigniter/├── application/│ └── tests/│ ├── _ci_phpunit_test/ ... don't touch! files ci-phpunit-test uses│ ├── Bootstrap.php ... bootstrap file for PHPUnit│ ├── DbTestCase.php ... DbTestCase class│ ├── TestCase.php ... TestCase class│ ├── controllers/ ... put your controller tests│ ├── libraries/ ... put your library tests│ ├── mocks/│ │ └── libraries/ ... mock libraries│ ├── models/ ... put your model tests│ └── phpunit.xml ... config file for PHPUnit└── vendor/
- Download latest
ci-phpunit-test
fromhttps://github.com/kenjis/ci-phpunit-test/releases. - Unzip and copy
application/tests
folder into yourapplication
folder in CodeIgniter project.
That's it.
$cd /path/to/codeigniter/$composer require kenjis/ci-phpunit-test:^3.0 --dev
And runinstall.php
:
$php vendor/kenjis/ci-phpunit-test/install.php --from-composer
- The above command always overwrites existing files.
- You must run it at CodeIgniter project root folder.
- You can specify your
application
andpublic
folder with option arguments, if you use custom folder paths.
$php vendor/kenjis/ci-phpunit-test/install.php -a<application_dir> -p<public_dir> -t<unittest_dir>
So the default would be:
$php vendor/kenjis/ci-phpunit-test/install.php -a application -p public -t application/tests
- But some paths may be not correct, in that case, please fix them intests/Bootstrap.php.
- Download latest
ci-phpunit-test
fromhttps://github.com/kenjis/ci-phpunit-test/releases. - Unzip and replace
application/tests/_ci_phpunit_test
folder. - ReadChange Log.
$cd /path/to/codeigniter/$composer update kenjis/ci-phpunit-test
ReadChange Log.
If you're upgrading from a previous version ofci-phpunit-test
that createdanapplication/test/_ci_phpunit_test
directory and now want to directly useci-phpunit-test
from Composer, you have a couple of additional steps:
- Delete the old test library directory:
$rm -rf /path/to/codeigniter/application/tests/_ci_phpunit_test
- Edit the
application/tests/Bootstrap.php
file. At the bottom of the "set the main path constants"section, add the following:And replace any references todefine('CI_PHPUNIT_TESTPATH',implode(DIRECTORY_SEPARATOR, [dirname(APPPATH),'vendor','kenjis','ci-phpunit-test','application','tests','_ci_phpunit_test']).DIRECTORY_SEPARATOR);
__DIR__ . '/_ci_phpunit_test/
orTESTPATH . '_ci_phpunit_test
withCI_PHPUNIT_TESTPATH . '
. (So, for example,__DIR__ . '/_ci_phpunit_test/CIPHPUnitTest.php'
would becomeCI_PHPUNIT_TESTPATH . '/CIPHPUnitTest.php'
.)
You need to install PHPUnit before running tests.
If you use Composer:
$composer require phpunit/phpunit --dev
$cd /path/to/codeigniter/$vendor/bin/phpunit -c application/tests/PHPUnit 9.5.4 by Sebastian Bergmann and contributors.... 3 / 3 (100%)Time: 00:00.102, Memory: 12.00 MBOK (3 tests, 3 assertions)Generating code coverage report in Clover XML format ... done [00:00.002]Generating code coverage report in HTML format ... done [00:00.012]
To generate coverage report, Xdebug is needed.
If you want to run a single test case file:
$vendor/bin/phpunit -c application/tests/ application/tests/models/Category_model_test.php
As an example, a test case class forInventory_model
would be as follows:
<?phpclass Inventory_model_testextends TestCase{publicfunctionsetUp():void {$this->resetInstance();$this->CI->load->model('Inventory_model');$this->obj =$this->CI->Inventory_model; }publicfunctiontest_get_category_list() {$expected = [1 =>'Book',2 =>'CD',3 =>'DVD', ];$list =$this->obj->get_category_list();foreach ($listas$category) {$this->assertEquals($expected[$category->id],$category->name); } }publicfunctiontest_get_category_name() {$actual =$this->obj->get_category_name(1);$expected ='Book';$this->assertEquals($expected,$actual); }}
As an example, a test case class for Welcome controller would be as follows:
<?phpclass Welcome_testextends TestCase{publicfunctiontest_index() {$output =$this->request('GET','welcome/index');$this->assertStringContainsString('<title>Welcome to CodeIgniter</title>',$output ); }}
SeeHow to Write Tests for details.
SeeFunction and Class Reference.
SeeTips.
Some features ofci-phpunit-test are available in the following standalone packages.
About
An easier way to use PHPUnit with CodeIgniter 3.x.