2017 © Pedro Peláez
 

library ci-phpunit-test

An easier way to use PHPUnit with CodeIgniter 3.x

image

kenjis/ci-phpunit-test

An easier way to use PHPUnit with CodeIgniter 3.x

  • Sunday, June 3, 2018
  • by kenjis
  • Repository
  • 46 Watchers
  • 379 Stars
  • 128,870 Installations
  • PHP
  • 4 Dependents
  • 1 Suggesters
  • 134 Forks
  • 46 Open issues
  • 36 Versions
  • 10 % Grown

The README.md

ci-phpunit-test for CodeIgniter 3.x

Latest Stable Version Total Downloads Latest Unstable Version License, (*1)

Scrutinizer Code Quality Coverage Status Build Status, (*2)

An easier way to use PHPUnit with CodeIgniter 3.x., (*3)

  • 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, (*4)

Requirements

  • PHP 5.4.0 or later (5.6 or later is recommended)
    • If you use PHP 7.3 or later and Monkey Patching, you must use PHP-Parser 4.2 or later as a Composer dependency.
  • CodeIgniter 3.x
  • PHPUnit 4.3 to 7.5 (4.8 or later is recommended)
    • If you want to use PHPUnit 8 or later, please use ci-phpunit-test 2.x.
    • If you use PHPUnit 6.0, please use ci-phpunit-test v0.14.0 or later.
    • You can download an old version of phpunit.phar from https://phar.phpunit.de/.

Optional

  • NetBeans
    • Go to Project Properties > Testing > PHPUnit, check Use Custom Test Suite checkbox, and select application/tests/_ci_phpunit_test/TestSuiteProvider.php.

Change Log

See Change Log., (*5)

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

  1. Download latest ci-phpunit-test from https://github.com/kenjis/ci-phpunit-test/releases.
  2. Unzip and copy application/tests folder into your application folder in CodeIgniter project.

That's it., (*6)

Installation via Composer

If you like Composer:, (*7)

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

And run install.php:, (*8)

$ 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.
  • Please remove the line <exclude>./_ci_phpunit_test/</exclude> in tests/phpunit.xml.
  • 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 <application_dir> -p <public_dir> -t <unittest_dir>

So the default would be:, (*9)

$ php vendor/kenjis/ci-phpunit-test/install.php -a application -p public -t application/tests

Upgrading

  1. Download latest ci-phpunit-test from https://github.com/kenjis/ci-phpunit-test/releases.
  2. Unzip and replace application/tests/_ci_phpunit_test folder.
  3. Read Change Log.

Upgrading via Composer

If you like Composer:, (*10)

$ cd /path/to/codeigniter/
$ composer update kenjis/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:, (*11)

  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'.)

Read Change Log., (*12)

How to Run Tests

You have to install PHPUnit before running tests., (*13)

Note: You must run phpunit command in application/tests folder., (*14)

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

...

Time: 341 ms, Memory: 5.50Mb

OK (3 tests, 3 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., (*15)

If you want to run a single test case file:, (*16)

$ phpunit models/Category_model_test.php

How to Write Tests

As an example, a test case class for Inventory_model would be as follows:, (*17)

<?php

class Inventory_model_test extends TestCase
{
    public function setUp()
    {
        $this->resetInstance();
        $this->CI->load->model('Inventory_model');
        $this->obj = $this->CI->Inventory_model;
    }

    public function test_get_category_list()
    {
        $expected = [
            1 => 'Book',
            2 => 'CD',
            3 => 'DVD',
        ];
        $list = $this->obj->get_category_list();
        foreach ($list as $category) {
            $this->assertEquals($expected[$category->id], $category->name);
        }
    }

    public function test_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:, (*18)

<?php

class Welcome_test extends TestCase
{
    public function test_index()
    {
        $output = $this->request('GET', 'welcome/index');
        $this->assertContains(
            '<title>Welcome to CodeIgniter</title>', $output
        );
    }
}

See How to Write Tests for details., (*19)

Function/Class Reference

See Function and Class Reference., (*20)

Tips

See Tips., (*21)

The Versions

03/06 2018

dev-master

9999999-dev http://kenjis.github.io/ci-phpunit-test/

An easier way to use PHPUnit with CodeIgniter 3.x

  Sources   Download

MIT

The Requires

 

test phpunit unit testing codeigniter monkey patch

22/04 2018

v0.16.1

0.16.1.0 http://kenjis.github.io/ci-phpunit-test/

An easier way to use PHPUnit with CodeIgniter 3.x

  Sources   Download

MIT

The Requires

 

test phpunit unit testing codeigniter monkey patch

15/04 2018

dev-fix-strict-mode

dev-fix-strict-mode http://kenjis.github.io/ci-phpunit-test/

An easier way to use PHPUnit with CodeIgniter 3.x

  Sources   Download

MIT

The Requires

 

test phpunit unit testing codeigniter monkey patch

15/04 2018

dev-fix-installer

dev-fix-installer http://kenjis.github.io/ci-phpunit-test/

An easier way to use PHPUnit with CodeIgniter 3.x

  Sources   Download

MIT

The Requires

 

test phpunit unit testing codeigniter monkey patch

25/03 2018

dev-update-ci

dev-update-ci http://kenjis.github.io/ci-phpunit-test/

An easier way to use PHPUnit with CodeIgniter 3.x

  Sources   Download

MIT

The Requires

 

test phpunit unit testing codeigniter monkey patch

25/03 2018

dev-fix-scripts

dev-fix-scripts http://kenjis.github.io/ci-phpunit-test/

An easier way to use PHPUnit with CodeIgniter 3.x

  Sources   Download

MIT

The Requires

 

test phpunit unit testing codeigniter monkey patch

21/03 2018

v0.16.0

0.16.0.0 http://kenjis.github.io/ci-phpunit-test/

An easier way to use PHPUnit with CodeIgniter 3.x

  Sources   Download

MIT

The Requires

 

test phpunit unit testing codeigniter monkey patch

14/01 2018

dev-fix-for-php72

dev-fix-for-php72 http://kenjis.github.io/ci-phpunit-test/

An easier way to use PHPUnit with CodeIgniter 3.x

  Sources   Download

MIT

The Requires

 

test phpunit unit testing codeigniter monkey patch

27/08 2017

dev-add-to-check-cookie-is-present-or-not

dev-add-to-check-cookie-is-present-or-not http://kenjis.github.io/ci-phpunit-test/

An easier way to use PHPUnit with CodeIgniter 3.x

  Sources   Download

MIT

The Requires

 

test phpunit unit testing codeigniter monkey patch

23/04 2017

v0.15.0

0.15.0.0 http://kenjis.github.io/ci-phpunit-test/

An easier way to use PHPUnit with CodeIgniter 3.x

  Sources   Download

MIT

The Requires

 

test phpunit unit testing codeigniter monkey patch

09/02 2017

v0.14.0

0.14.0.0 http://kenjis.github.io/ci-phpunit-test/

An easier way to use PHPUnit with CodeIgniter 3.x

  Sources   Download

MIT

The Requires

 

test phpunit unit testing codeigniter monkey patch

20/11 2016

v0.13.0

0.13.0.0 http://kenjis.github.io/ci-phpunit-test/

An easier way to use PHPUnit with CodeIgniter 3.x

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

test phpunit unit testing codeigniter monkey patch

23/07 2016

v0.12.2

0.12.2.0 http://kenjis.github.io/ci-phpunit-test/

An easier way to use PHPUnit with CodeIgniter 3.0

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

test phpunit unit testing codeigniter monkey patch

11/06 2016

v0.12.1

0.12.1.0 http://kenjis.github.io/ci-phpunit-test/

An easier way to use PHPUnit with CodeIgniter 3.0

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

test phpunit unit testing codeigniter monkey patch

17/04 2016

v0.12.0

0.12.0.0 http://kenjis.github.io/ci-phpunit-test/

An easier way to use PHPUnit with CodeIgniter 3.0

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

test phpunit unit testing codeigniter monkey patch

25/03 2016

v0.11.3

0.11.3.0 http://kenjis.github.io/ci-phpunit-test/

An easier way to use PHPUnit with CodeIgniter 3.0

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

test phpunit unit testing codeigniter monkey patch

17/03 2016

v0.11.2

0.11.2.0 http://kenjis.github.io/ci-phpunit-test/

An easier way to use PHPUnit with CodeIgniter 3.0

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

test phpunit unit testing codeigniter monkey patch

22/02 2016

v0.11.1

0.11.1.0 http://kenjis.github.io/ci-phpunit-test/

An easier way to use PHPUnit with CodeIgniter 3.0

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

test phpunit unit testing codeigniter monkey patch

20/01 2016

v0.11.0

0.11.0.0 http://kenjis.github.io/ci-phpunit-test/

An easier way to use PHPUnit with CodeIgniter 3.0

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

test phpunit unit testing codeigniter monkey patch

31/12 2015

v0.10.1

0.10.1.0 http://kenjis.github.io/ci-phpunit-test/

An easier way to use PHPUnit with CodeIgniter 3.0

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

test phpunit unit testing codeigniter monkey patch

27/11 2015

v0.10.0

0.10.0.0 http://kenjis.github.io/ci-phpunit-test/

An easier way to use PHPUnit with CodeIgniter 3.0

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

test phpunit unit testing codeigniter monkey patch

22/11 2015

v0.9.1

0.9.1.0 http://kenjis.github.io/ci-phpunit-test/

An easier way to use PHPUnit with CodeIgniter 3.0

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

test phpunit unit testing codeigniter

18/11 2015

v0.9.0

0.9.0.0 http://kenjis.github.io/ci-phpunit-test/

An easier way to use PHPUnit with CodeIgniter 3.0

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

test phpunit unit testing codeigniter

09/10 2015

v0.8.2

0.8.2.0 http://kenjis.github.io/ci-phpunit-test/

An easier way to use PHPUnit with CodeIgniter 3.0

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

test phpunit unit testing codeigniter

01/10 2015

v0.8.1

0.8.1.0 http://kenjis.github.io/ci-phpunit-test/

An easier way to use PHPUnit with CodeIgniter 3.0

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

test phpunit unit testing codeigniter

28/09 2015

v0.8.0

0.8.0.0 http://kenjis.github.io/ci-phpunit-test/

An easier way to use PHPUnit with CodeIgniter 3.0

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

test phpunit unit testing codeigniter

09/09 2015

v0.7.0

0.7.0.0 http://kenjis.github.io/ci-phpunit-test/

An easier way to use PHPUnit with CodeIgniter 3.0

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

test phpunit unit testing codeigniter

13/08 2015

v0.6.2

0.6.2.0 http://kenjis.github.io/ci-phpunit-test/

An easier way to use PHPUnit with CodeIgniter 3.0

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

test phpunit unit testing codeigniter

12/08 2015

v0.6.1

0.6.1.0 http://kenjis.github.io/ci-phpunit-test/

An easier way to use PHPUnit with CodeIgniter 3.0

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

test phpunit unit testing codeigniter

12/08 2015

v0.6.0

0.6.0.0 http://kenjis.github.io/ci-phpunit-test/

An easier way to use PHPUnit with CodeIgniter 3.0

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

test phpunit unit testing codeigniter

27/07 2015

v0.5.0

0.5.0.0 http://kenjis.github.io/ci-phpunit-test/

An easier way to use PHPUnit with CodeIgniter 3.0

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

test phpunit unit testing codeigniter

21/07 2015

v0.4.0

0.4.0.0 http://kenjis.github.io/ci-phpunit-test/

An easier way to use PHPUnit with CodeIgniter 3.0

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

test phpunit unit testing codeigniter

14/07 2015

v0.3.0

0.3.0.0

An easier way to use PHPUnit with CodeIgniter 3.0

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

test phpunit unit testing codeigniter

19/06 2015

v0.2.0

0.2.0.0

An easier way to use PHPUnit with CodeIgniter 3.0

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

test phpunit unit testing codeigniter

15/06 2015

v0.1.1

0.1.1.0

An easier way to use PHPUnit with CodeIgniter 3.0

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

test phpunit unit testing codeigniter

11/06 2015

v0.1.0

0.1.0.0

An easier way to use PHPUnit with CodeIgniter 3.0

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

test phpunit unit testing codeigniter