View on GitHub

CI PHPUnit Test

An easier way to use PHPUnit with CodeIgniter 3.0.

Download this project as a .zip file Download this project as a tar.gz file

CI PHPUnit Test for CodeIgniter 3.0

Latest Stable Version Total Downloads Latest Unstable Version License

Scrutinizer Code Quality Coverage Status Build Status

An easier way to use PHPUnit with CodeIgniter 3.0.

Screenshot: Running tests on NetBeans

Requirements

Folder Structure

codeigniter/
├── application/
│   └── tests/
│        ├── _ci_phpunit_test/ ... don't touch! files CI PHPUnit Test uses
│        ├── Bootstrap.php     ... bootstrap file for PHPUnit
│        ├── TestCase.php      ... TestCase class
│        ├── controllers/      ... put your controller tests
│        ├── mocks/
│        │   └── libraries/    ... mock libraries
│        ├── models/           ... put your model tests
│        └── phpunit.xml       ... config file for PHPUnit
└── vendor/

Installation

Download latest ci-phpunit-test: https://github.com/kenjis/ci-phpunit-test/releases

Unzip and copy application/tests folder into your application folder in CodeIgniter project. That's it.

If you like Composer:

$ cd /path/to/codeigniter/
$ composer require kenjis/ci-phpunit-test --dev

And run install.php:

$ php vendor/kenjis/ci-phpunit-test/install.php

Upgrading

Download latest ci-phpunit-test: https://github.com/kenjis/ci-phpunit-test/releases

Unzip and replace application/tests/_ci_phpunit_test folder.

If you like Composer:

$ cd /path/to/codeigniter/
$ composer update kenjis/ci-phpunit-test
$ php vendor/kenjis/ci-phpunit-test/update.php

How to Run Tests

You have to install PHPUnit before running tests.

$ cd /path/to/codeigniter/
$ cd application/tests/
$ phpunit
PHPUnit 4.6.10 by Sebastian Bergmann and contributors.

Configuration read from /.../codeigniter/application/tests/phpunit.xml

...

Time: 635 ms, Memory: 4.50Mb

OK (3 tests, 4 assertions)

Generating code coverage report in Clover XML format ... done

Generating code coverage report in HTML format ... done

To generate coverage report, Xdebug is needed.

How to Write Tests

See How To Write Tests.

Can and Can't

CI PHPUnit Test does not want to modify CodeIgniter core files. The more you modify core, the more you get difficulities when you update CodeIgniter.

In fact, it uses a modified class and a few functions. But I try to modify as little as possible.

The functions and the class which are modified:

They are in tests/_ci_phpunit_test/replacing folder.

MY_Loader

CI PHPUnit Test replaces CI_Loader and modifies below methods:

But if you place MY_Loader, your MY_Loader extends the loader of CI PHPUnit Test.

If your MY_Loader overrides the above methods, probably CI PHPUnit Test does not work correctly.

exit()

CI PHPUnit Test does not care functions/classes which exit() or die() (Except for show_error() and show_404()).

So, for example, if you use URL helper redirect() in your application code, your testing ends with it.

I recommend you not to use exit() or die() in your code. And you have to skip exit() somehow in CodeIgniter code.

For example, you can modify redirect() using MY_url_helper.php in your application. I put a sample MY_url_helper.php. (I think CodeIgniter code itself should be changed testable.)

See How to Write Tests for details.

Reset CodeIgniter object

CodeIgniter has a function get_instance() to get the CodeIgniter object (CodeIgniter instance or CodeIgniter super object).

CI PHPUnit Test has a new function reset_instance() which reset the current CodeIgniter object. After resetting, you can create a new your Controller instance with new state.

You can see how to use it in application/tests/_ci_phpunit_test/CIPHPUnitTestCase.php.

Function/Class Reference

See Function and Class Reference.

Related Projects for CodeIgniter 3.0