edgcarmu/codeigniter-phpunit

Official adaptation of yidas/codeigniter-phpunit for CodeIgniter 3.1.11 and PHP 7.1+

v1.0.0 2025-06-08 23:03 UTC

This package is auto-updated.

Last update: 2025-06-08 23:06:51 UTC


README

CodeIgniter PHPUnit Test


CodeIgniter 3 PHPUnit Test extension library

Latest Stable Version License

Official adaptation of the yidas/codeigniter-phpunit repository for compatibility with CodeIgniter 3.1.13 and PHP 7.1+.

FEATURES

  • PHPUnit Test in Codeigniter 3 Framework

  • Easy to install into your Codeigniter project by Composer

OUTLINE

REQUIREMENTS

This library requires the following:

  • PHP 7.1.0+
  • CodeIgniter 3.1.13+

INSTALLATION

Run Composer from the project root:

composer require edgcarmu/codeigniter-phpunit

DIRECTORY STRUCTURE

codeigniter/
├── application/
├── system/
├── tests/
│   ├── bootstrap.php        ← Initializes the test environment; create only if needed to customize, then delegate to vendor/edgcarmu/codeigniter-phpunit/bootstrap.php
│   └── MyTest.php           ← Your test cases go here
├── vendor/
└── phpunit.xml              ← PHPUnit configuration; can point directly to the library's bootstrap if no customization is needed

CONFIGURATION

According to Directory Structure, create and configure phpunit.xml in the project root:

<?xml version="1.0" encoding="UTF-8" ?>
<phpunit bootstrap="vendor/edgcarmu/codeigniter-phpunit/bootstrap.php">
  <testsuites>
    <testsuite name="TestSuite">
      <directory>tests</directory>
    </testsuite>
  </testsuites>
</phpunit>

For this phpunit.xml template, the test cases directory is tests/, located at the project root. Make sure to place all your test cases inside this directory.

USAGE

From the project root, run PHPUnit using the Composer-installed binary:

./vendor/bin/phpunit

Or using absolute path commands like:

$ /var/www/html/codeigniter3/vendor/bin/phpunit -c /var/www/html/codeigniter3/phpunit.xml
$ phpunit -c /var/www/html/codeigniter3/phpunit.xml

Then the result would like:

PHPUnit 5.7.27 by Sebastian Bergmann and contributors.



Time: 40 ms, Memory: 2.75MB

No tests executed!

TEST CASE

With this extension library, you can write test cases while loading the CodeIgniter framework.

For example, create a test case at tests/CodeigniterTest.php to test a CodeIgniter component like the config loader:

<?php

use PHPUnit\Framework\TestCase;

final class CodeigniterTest extends TestCase
{
    function __construct() 
    {
        parent::__construct();
        // Load CI application
        $this->CI = & get_instance();
    }
    
    public function testConfigItem()
    {
        $indexPage = $this->CI->config->item('index_page');
        
        $this->assertSame('index.php', $indexPage);
    }
}

Then you would get the result OK (1 test, 1 assertion) by running PHPUnit test.