PHPackages                             kenjis/ci-phpunit-test - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Testing &amp; Quality](/categories/testing)
4. /
5. kenjis/ci-phpunit-test

ActiveLibrary[Testing &amp; Quality](/categories/testing)

kenjis/ci-phpunit-test
======================

An easier way to use PHPUnit with CodeIgniter 3.x

v3.0.4(3y ago)5861.2M—0.4%195[19 issues](https://github.com/kenjis/ci-phpunit-test/issues)[1 PRs](https://github.com/kenjis/ci-phpunit-test/pulls)3MITPHPPHP &gt;=7.3

Since Jun 11Pushed 2y ago41 watchersCompare

[ Source](https://github.com/kenjis/ci-phpunit-test)[ Packagist](https://packagist.org/packages/kenjis/ci-phpunit-test)[ Docs](http://kenjis.github.io/ci-phpunit-test/)[ RSS](/packages/kenjis-ci-phpunit-test/feed)WikiDiscussions 3.x Synced 1mo ago

READMEChangelog (10)Dependencies (1)Versions (49)Used By (3)

ci-phpunit-test for CodeIgniter 3.x
===================================

[](#ci-phpunit-test-for-codeigniter-3x)

[![Latest Stable Version](https://camo.githubusercontent.com/a6d6c99d10f5acb3bcb2ddc81d9ad1f456455546ad8bca9879ffdd6bade8faaf/68747470733a2f2f706f7365722e707567782e6f72672f6b656e6a69732f63692d706870756e69742d746573742f762f737461626c65)](https://packagist.org/packages/kenjis/ci-phpunit-test) [![Total Downloads](https://camo.githubusercontent.com/e78c0b9a5cde73d960e94356e10d56c299c2e490bae3494062df66e9b2c9e3e0/68747470733a2f2f706f7365722e707567782e6f72672f6b656e6a69732f63692d706870756e69742d746573742f646f776e6c6f616473)](https://packagist.org/packages/kenjis/ci-phpunit-test) [![Latest Unstable Version](https://camo.githubusercontent.com/d824bb1ecb5db26b60d25c571d73074dc62b76bb0c14d762d2e5400ff8560856/68747470733a2f2f706f7365722e707567782e6f72672f6b656e6a69732f63692d706870756e69742d746573742f762f756e737461626c65)](https://packagist.org/packages/kenjis/ci-phpunit-test) [![License](https://camo.githubusercontent.com/d32a491bea0bcdfae5da4b3e9c37a839a96ceaa943a65d101bee0852ed0c9f15/68747470733a2f2f706f7365722e707567782e6f72672f6b656e6a69732f63692d706870756e69742d746573742f6c6963656e7365)](https://packagist.org/packages/kenjis/ci-phpunit-test)

[![Scrutinizer Code Quality](https://camo.githubusercontent.com/46276c60d808908d54282b8209df2a1bad7af7dd6a57f1035ce016914bf566ff/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6b656e6a69732f63692d706870756e69742d746573742f6261646765732f7175616c6974792d73636f72652e706e673f623d332e78)](https://scrutinizer-ci.com/g/kenjis/ci-phpunit-test/?branch=3.x)[![Coverage Status](https://camo.githubusercontent.com/96ba47b20189067a6f47fbd03a1f0805b982455ca1c8bc8ac156a592ee8e8b55/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6b656e6a69732f63692d706870756e69742d746573742f62616467652e7376673f6272616e63683d332e78)](https://coveralls.io/r/kenjis/ci-phpunit-test?branch=3.x)[![Build Status](https://camo.githubusercontent.com/3fb74b893370f5c359d5d37cf5f3370254a740777d54a459053effd985087a5a/68747470733a2f2f7472617669732d63692e636f6d2f6b656e6a69732f63692d706870756e69742d746573742e7376673f6272616e63683d332e78)](https://travis-ci.com/kenjis/ci-phpunit-test)

---

Warning

[Upgrading to CodeIgniter4 is strongly recommended](https://github.com/kenjis/ci-phpunit-test/issues/409).

---

An easier way to use PHPUnit with [CodeIgniter](https://github.com/bcit-ci/CodeIgniter) 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.

[![Screenshot: Running tests on NetBeans 8.1](docs/imgs/netbeans-8.1.png)](docs/imgs/netbeans-8.1.png)

Requirements
------------

[](#requirements)

- 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](https://github.com/kenjis/ci-phpunit-test/issues/361).
- PHPUnit 9.3 or later
    - If you want to use PHPUnit 9.2 or earlier, please use ci-phpunit-test [2.x](https://github.com/kenjis/ci-phpunit-test/tree/2.x).

Optional
--------

[](#optional)

- NetBeans
    - Go to *Project Properties &gt; Testing &gt; PHPUnit*, check *Use Custom Test Suite* checkbox, and select `application/tests/_ci_phpunit_test/TestSuiteProvider.php`.

Change Log
----------

[](#change-log)

See [Change Log](https://github.com/kenjis/ci-phpunit-test/blob/3.x/application/tests/_ci_phpunit_test/ChangeLog.md).

Folder Structure
----------------

[](#folder-structure)

```
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/

```

Installation
------------

[](#installation)

### Manual Installation

[](#manual-installation)

1. Download latest `ci-phpunit-test` from .
2. Unzip and copy `application/tests` folder into your `application` folder in CodeIgniter project.

That's it.

### Composer Installation

[](#composer-installation)

```
$ cd /path/to/codeigniter/
$ composer require kenjis/ci-phpunit-test:^3.0 --dev
```

And run `install.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` and `public` folder with option arguments, if you use custom folder paths.

```
$ php vendor/kenjis/ci-phpunit-test/install.php -a  -p  -t
```

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 in [tests/Bootstrap.php](https://github.com/kenjis/ci-phpunit-test/blob/3.x/application/tests/Bootstrap.php#L98).

Upgrading
---------

[](#upgrading)

### Manual Upgrading

[](#manual-upgrading)

1. Download latest `ci-phpunit-test` from .
2. Unzip and replace `application/tests/_ci_phpunit_test` folder.
3. Read [Change Log](https://github.com/kenjis/ci-phpunit-test/blob/3.x/application/tests/_ci_phpunit_test/ChangeLog.md).

### Composer Upgrading

[](#composer-upgrading)

```
$ cd /path/to/codeigniter/
$ composer update kenjis/ci-phpunit-test
```

Read [Change Log](https://github.com/kenjis/ci-phpunit-test/blob/3.x/application/tests/_ci_phpunit_test/ChangeLog.md).

#### If you want to remove application/test/\_ci\_phpunit\_test/

[](#if-you-want-to-remove-applicationtest_ci_phpunit_test)

If you're upgrading from a previous version of `ci-phpunit-test` that created an `application/test/_ci_phpunit_test` directory and now want to directly use `ci-phpunit-test` from Composer, you have a couple of additional steps:

1. Delete the old test library directory: ```
    $ rm -rf /path/to/codeigniter/application/tests/_ci_phpunit_test
    ```
2. Edit the `application/tests/Bootstrap.php` file. At the bottom of the "set the main path constants" section, add the following: ```
    define('CI_PHPUNIT_TESTPATH', implode(
        DIRECTORY_SEPARATOR,
        [dirname(APPPATH), 'vendor', 'kenjis', 'ci-phpunit-test', 'application', 'tests', '_ci_phpunit_test']
    ).DIRECTORY_SEPARATOR);
    ```

    And replace any references to `__DIR__ . '/_ci_phpunit_test/` or `TESTPATH . '_ci_phpunit_test` with `CI_PHPUNIT_TESTPATH . '`. (So, for example, `__DIR__ . '/_ci_phpunit_test/CIPHPUnitTest.php'`would become `CI_PHPUNIT_TESTPATH . '/CIPHPUnitTest.php'`.)

How to Run Tests
----------------

[](#how-to-run-tests)

You need to install PHPUnit before running tests.

If you use Composer:

```
$ composer require phpunit/phpunit --dev
```

### Run All Tests

[](#run-all-tests)

```
$ 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 MB

OK (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.

### Run a Single Test

[](#run-a-single-test)

If you want to run a single test case file:

```
$ vendor/bin/phpunit -c application/tests/ application/tests/models/Category_model_test.php
```

How to Write Tests
------------------

[](#how-to-write-tests)

As an example, a test case class for `Inventory_model` would be as follows:

```
