Skip to content

jeffcao/ci-phpunit-test

 
 

Repository files navigation

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.

  • You don't have to modify CodeIgniter core files at all.
  • You can write controller tests easily.
  • Well documented.

Screenshot: Running tests on NetBeans

Requirements

  • PHP 5.4.0 or later
  • CodeIgniter 3.0.*
  • PHPUnit (version 4.7 is recommended)

Change Log

See Change Log.

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
  • Above command always overwrites exisiting files.
  • You must run it at CodeIgniter project root folder.

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

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

<?php

class Inventory_model_test extends TestCase
{
    public function setUp()
    {
        $this->CI =& get_instance();
        $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:

<?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.

Function/Class Reference

See Function and Class Reference.

Related Projects for CodeIgniter 3.0

About

An easier way to use PHPUnit with CodeIgniter 3.0.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 99.8%
  • Shell 0.2%