Testing Codeigniter 3.0 applications with PHPUnit
Writing tests for CodeIgniter application is a bit difficult.
But I introduce an easier way to use PHPUnit with CodeIgniter 3.0.
(2016/02/28 Added) If you are not familiar with Testing, I recommend our Ebook. CodeIgniter Testing Guide. It is Beginners' Guide to Automated Testing in PHP.
Requirements
- CodeIgniter 3.0
- PHP 5.4 or later
- PHPUnit
Setup
Step 1
Install CodeIgniter 3.0.0.
If you like Composer, I recommend to use CodeIgniter Composer Installer.
Step 2
Install CI PHPUnit Test. Download latest version from https://github.com/kenjis/ci-phpunit-test/releases and unzip it. Copy application/tests
folder into application
folder.
CodeIgniter-3.0.0/
└── application/
└── tests/
If you like Composer:
$ cd CodeIgniter-3.0.0/
$ composer require kenjis/ci-phpunit-test --dev
$ php vendor/kenjis/ci-phpunit-test/install.php
Step 3
(2015/06/15 Added) You don't need this step on CI PHPUnit Test v0.1.1 and later. Go to Step 4.
Fix the three paths in tests/Bootstrap.php
if you need. If you used CodeIgniter Composer Installer, you don't have to change.
$system_path = '../../vendor/codeigniter/framework/system';
$application_folder = '../../application';
define('FCPATH', realpath(dirname(__FILE__).'/../../public').'/');
If you use CodeIgniter 3.0 default folder structure. You have to change like below:
--- a/application/tests/Bootstrap.php
+++ b/application/tests/Bootstrap.php
@@ -102,7 +102,7 @@ switch (ENVIRONMENT)
* Include the path if the folder is not in the same directory
* as this file.
*/
- $system_path = '../../vendor/codeigniter/framework/system';
+ $system_path = '../../system';
/*
*---------------------------------------------------------------
@@ -230,7 +230,7 @@ switch (ENVIRONMENT)
define('BASEPATH', str_replace('\\', '/', $system_path));
// Path to the front controller (this file)
- define('FCPATH', realpath(dirname(__FILE__).'/../../public').'/');
+ define('FCPATH', realpath(dirname(__FILE__).'/../..').'/');
// Name of the "system folder"
define('SYSDIR', trim(strrchr(trim(BASEPATH, '/'), '/'), '/'));
Step 4
Install PHPUnit. See https://phpunit.de/manual/4.7/en/installation.html.
Now you're ready to write and run tests.
How to Write Tests
Controllers
tests/controllers/Welcome_test.php
<?php
class Welcome_test extends TestCase
{
public function test_index()
{
$output = $this->request('GET', ['Welcome', 'index']);
$this->assertContains('<title>Welcome to CodeIgniter</title>', $output);
}
}
Models
(2016/01/11 Updated)
Updated code in setUp()
.
tests/models/Inventory_model_test.php
<?php
class Inventory_model_test extends TestCase
{
public function setUp()
{
$this->resetInstance();
$this->CI->load->model('shop/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);
}
}
If you want to know how to write tests more, see https://github.com/kenjis/ci-phpunit-test/blob/master/docs/HowToWriteTests.md.
How to Run Tests
$ cd CodeIgniter-3.0.0/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.
Summary
- Installing CI PHPUnit Test is very easy. You don't have to modify CodeIgniter core files.
- You can write controller tests easily using CI PHPUnit Test.
- You could probably write tests for your application with 100% coverage using CI PHPUnit Test.
Related
References
Date: 2015/06/12