Jan 7, 2015 - CLI Unit testing on CodeIgniter

Comments

CodeIgniter (v2.x) has a simple unit testing library, but it’s way too simple.
As a default, all you can do is write simple comparisons of expected values, as such:

<?php
$test = 1 + 1;

$expected_result = 2;

$test_name = 'Adds one plus one';

$this->unit->run($test, $expected_result, $test_name);

and the result can either be displayed as an HTML, or can be returned as an array.

<?php
echo $this->unit->report();
$result = $this->unit->result();

That means that there is no CLI solution for unit testing in CodeIgniter. I’m sure that everybody who is familiar with CodeIgniter knows that it has pretty much no CLI feature whatsoever.
Of course the speed = simplicity = lack of features was always CodeIgniter’s trait, and I still respect that. Still, I want to be able to have a CLI unit testing capability for Continuous Integration and what not.

Here’s what I came up with: cli_unit_test

It’s an extension to the Unit_test library, adding one extra function which is cli_report(). This outputs the test results in a CLI friendly manner, with colors and stuff.

Since we can determine whether the request is a HTTP or a CLI request, here’s a typical implementation I write:

<?php
/* some test code above... */

if ($this->input->is_cli_request()) {
	$this->unit->cli_report();
} else {
	$this->unit->report();
}

And just call your unit test controller as such:

php /path/to/ci/index.php test_controller index

It’s a small and simple, but nice solution and works pretty well for me.

P.S.
I should start looking into BDD testing frameworks for PHP; Unit testing isn’t enough these days. I want something as intuitive and reliable as Rspec. Behat would be the best option maybe?

Jan 6, 2015 - My First Post

This is my first post.
I will be writing about code and technology in general.

I’m a co-founder/CTO of a startup based in Tokyo, Japan. My interests in tech include:

  • PHP
  • DevOps
  • AWS
  • Mobile Application
  • Security
  • Agile/Scrum
  • Business models

I’ve tried blogging a few times before but didn’t work;
Primarily because I worry too much about what readers think, and how to write “catchy” blogs. Hope I don’t have to worry about that kinda stuff this time.