CIUnit is simple and light-weight PHPUnit/JUnit like unit testing framework for CodeIgniter. The framework runs on top of CodeIgniter and provides a web interface for test execution. CIUnit is a good alternative for small projects where the full potential of PHPUnit is not used and a good start for unit testing newbies.
We are still in Beta version, so if you notice any bugs please report them here.
Any suggestions on how to improve our code are wellcome :)
- Getting Started
- Writing Tests for CIUnit
- Testing CodeIgniter
- Testing Exceptions
- Fixtures
- Organizing Tests
- CIUnit API
- аssertArrayHasKey
- аssertCount
- аssertEmpty
- аssertFalse
- аssertinstanceOf
- аssertinternalType
- аssertNull
- аssertSameSize
- аssertTrue
- аssertGreaterThan
- аssertGreaterThanOrEqual
- аssertLessThan
- аssertLessThanOrEqual
- аssertStringStartsWith
- аssertStringEndsWith
- аssertStringMatchesRegex
- assertClassHasAttribute
- assertClassHasStaticAttribute
- assertObjectHasAttribute
- assertObjectHasStaticAttribute
- Change Log
- Issues
- Credits
CIUnit requires CodeIgniter >= 2.0
PHP >= 5.3
- Download CIUnit Framework from here or just clone the repo
git clone [email protected]:agop/ciunit-framework.git
- Unzip the
resources/
folder in the root of your application - Unzip the
controllers/ciunit_controller.php
to yourcontrollers/
folder - Unzip the
third_party/ciunit/
folder to yourthird_party/
folder - Create folder
tests/
under yourapplication/
folder - Add the following routes to your
routes.php
$route['ciunit'] = "ciunit_controller/index";
$route['ciunit/(:any)'] = "ciunit_controller/index/$1";
- CIUnit makes use of the
base_url()
feature in CodeIgniter. Please, make sure it is properly configured inside yourconfig/config.php
$config['base_url'] = 'http://example.com/';
- Now you must be good to go.
http://example.com/index.php/ciunit
orhttp://example.com/ciunit
depending on your url configuration
NB! CIUnit used CodeIgniter's ENVIRONMENT
constant, and will only works only in testing
or development
. Please make sure your are not running in production
.
Change your working environment from your index.php
located in the root
of your project.
define('ENVIRONMENT', 'testing');
CIUnit allows you to change location for resources and tests. The CIUnit config file is located under ciunit/config/config.php
Default configuration is as follows
$config['tests_path'] = APPPATH .'tests/';
$config['resources_path'] = 'resources/';
If you have any issues installing CIUnit, please feel free to contact me.
The example introduces the basic conventions and steps for writing tests with CIUnit
- The tests for the class
User
go into a classUserTest
in UserTest.php
NB! It is important to follow this naming convention for tests so they can be discovered by CIUnit e.g.class MyTest
->MyTest.php
UserTest
inherits fromCIUnit_Framework_TestCase
- Tests are public methods whose name starts with
test*
- Inside your test methods you would use assertions like
$this->assertTrue(TRUE)
- Your test classes go under
application/tests
by default. Nested folders are not supported in this release!
StackTest.php
<?php
class StackTest extends CIUnit_Framework_TestCase
{
public function testPushAndPop()
{
$stack = array();
$this->assertEquals(0, count($stack));
array_push($stack, 'foo');
$this->assertEquals('foo', $stack[count($stack)-1]);
$this->assertEquals(1, count($stack));
$this->assertEquals('foo', array_pop($stack));
$this->assertEquals(0, count($stack));
}
}
The CIUnit_Framework_TestCase
class holds an instance to CodeIgniter that can be accessed from your test class using $this->get_instance()
Example test class performing operations with a CodeIgniter module class.
CITest.php
<?php
class CITest extends CIUnit_Framework_TestCase
{
public function testAccessCIModel()
{
$model = $this->get_instance()->model_name->get_all();
$this->assertEquals(10, count($model));
}
}
To test whether an exception is thrown inside the tested code use the setExpectedException (string $name, string $message, integer $code)
method.
The $message
and $code
parameters are optional. If present framework will try to match them with the corresponding
values from the exception thrown.
The example introduces the basic steps for testing for expected exception
ExceptionTest.php
<?php
class ExceptionTest extends CIUnit_Framework_TestCase
{
public function testException()
{
$this->setExpectedException('Exception');
throw new Exception("Error Processing Request", 1)
}
}
Alternative approach to testing exceptions
<?php
class ExceptionTest extends CIUnit_Framework_TestCase
{
public function testException()
{
try {
// Code throwing exception goes here...
}
catch(Exception $e) {
return;
}
$this->fail('No exception has been raised');
}
}
NB! With this approach your test will be mark as Incomplete
, because it is not performing any assertions.
I assume we all know what fixtures are and how they work.
The setUp()
and tearDown()
template methods are run once for each test method of the test case class.
FixtureTest.php
<?php
class FixtureTest extends CIUnit_Framework_TestCase
{
protected function setUp()
{
print __METHOD__ . "\n";
}
public function testOne()
{
print __METHOD__ . "\n";
$this->assertTrue(TRUE);
}
public function testTwo()
{
print __METHOD__ . "\n";
$this->assertTrue(TRUE);
}
protected function tearDown()
{
print __METHOD__ . "\n";
}
}
The result from running the code will be
FixtureTest::setUp
FixtureTest::testOne
FixtureTest::tearDown
FixtureTest::setUp
FixtureTest::testTwo
FixtureTest::tearDown
The CIUnit framework allows us to organize tests into a hierarchy of test suite objects. Suites are created by extending the CIUnit_Framework_TestSuite
class and using the suite()
method.
The CIUnit_Framework_TestSuite
class class offers two template methods, setUp()
and tearDown()
, that are called before the first test of the test suite and after the last test of the test suite, respectively.
Here is a basic examplе how to organize your test suites into one test suite.
MySuite.php
<?php
require_once 'MyTest.php';
class MySuite extends CIUnit_Framework_TestSuite
{
public static function suite()
{
$suite = new MySuite('Project Suite');
$suite->addTestSuite('MyTest');
return $suite;
}
protected function setUp()
{
print __METHOD__ . "\n";
}
protected function tearDown()
{
print __METHOD__ . "\n";
}
}
The implementation for MyTest.php can be found below
<?php
class MyTest extends CIUnit_Framework_TestCase
{
protected function setUp()
{
print __METHOD__ . "\n";
}
protected function tearDown()
{
print __METHOD__ . "\n";
}
public function testOne()
{
print __METHOD__ . "\n";
}
public function testTwo()
{
print __METHOD__ . "\n";
}
public function testThree()
{
print __METHOD__ . "\n";
}
}
Output
MySuite::setUp
MyTest::setUp
MyTest::testOne
MyTest::tearDown
MyTest::setUp
MyTest::testTwo
MyTest::tearDown
MyTest::setUp
MyTest::testThree
MyTest::tearDown
MySuite::tearDown
Method asserts that array has a specified key
assertArrayHasKey(mixed $needle, array $haystack, string $message = '')
Reports an error identified by $message
if $array
does not contain the specified $key
.
assertArrayNotHasKey()
is the inverse of this assertion and takes the same arguments.
<?php
class ArrayHasKeyTest extends CIUnit_Framework_TestCase
{
public function testFailure()
{
$this->assertArrayHasKey('foo', array('bar' => 'bar'));
}
}
Failure description
Failed asserting that an array has the key "bar".
Method asserts the number of elements of an array, Countable or Iterator
assertCount($expectedCount, array $haystack, string $messge = '')
Reports an error identified by $message
if the number of elements in $haystack
is not equal to $expectedCount
.
assertNotCount()
is the inverse of this assertion and takes the same arguments.
<?php
class CountTest extends CIUnit_Framework_TestCase
{
public function testFailure()
{
$this->assertCount(0, array('foo'));
}
}
Failure description
Failed asserting that actual size 1 matches expected size 0.
Method asserts that a variable is empty
assertEmpty(mixed $actual, string $message = '')
Reports an error identified by $message
if $actual
is not empty.
assertNotEmpty()
is the inverse of this assertion and takes the same arguments.
<?php
class EmptyTest extends CIUnit_Framework_TestCase
{
public function testFailure()
{
$this->assertEmpty(array('foo'));
}
}
Failure description
Failed asserting that an array is empty.
Method asserts that two variables are equal
assertEquals(mixed $expected, mixed $actual, string $message = '')
Reports an error identified by $message
if $expected
is not equal to $actual
.
assertNotEquals()
is the inverse of this assertion and takes the same arguments.
<?php
class EqualsTest extends CIUnit_Framework_TestCase
{
public function testFailure()
{
$this->assertEquals(12, 13);
}
public function testFailure2()
{
$this->assertEquals('Integer', 'String');
}
}
Failure description
Failed asserting that 12 matches expected 13.
Failed asserting that two strings are equal.
- Expected
+ Actual
-'Integer'
+'String'
assertEquals(float $expected, float $actual, float $delta = 0, string $message = '')
Reports an error identified by $message
if the two floats $expected
and $actual
are not within $delta
of each other.
<?php
class EqualsTest extends CIUnit_Framework_TestCase
{
public function testSuccess()
{
$this->assertEquals(12.0, 12.1, 0.2, '');
}
public function testFailure()
{
$this->assertEquals(12.0, 12.1);
}
}
Failure description
Failed asserting that 12.1 matches expected 12.0.
assertEquals(array $expected, array $actual, string $message = '')
<?php
class EqualsTest extends CIUnit_Framework_TestCase
{
public function testFailure()
{
$actual = array('a', 'b', 'c');
$expected = array('a', 'b', 'd');
$this->assertEquals($actual, $expected);
}
}
Failure description
Failed asserting that two arrays are equal.
- Expected
+ Actual
Array (
0 => 'a'
1 => 'b'
- 2 => 'c'
+ 2 => 'd'
)
assertEquals(object $expected, object $actual, string $message = '')
<?php
class EqualsTest extends CIUnit_Framework_TestCase
{
public function testFailure()
{
$expected = new stdClass();
$expected->name = "John";
$expected->age = 11;
$actual = new stdClass();
$actual->name = "John";
$actual->age = 19;
$this->assertEquals($actual, $expected);
}
}
Failure description
Failed asserting that two objects are equal.
- Expected
+ Actual
stdClass Object (
'name' => 'John'
- 'age' => 11
+ 'age' => '19'
)
Method asserts that a condition is false
assertFalse(bool $condition, string $message = '')
Reports an error identified by $message
if $condition
is TRUE
.
<?php
class FalseTest extends CIUnit_Framework_TestCase
{
public function testFailure()
{
$this->assertFalse(TRUE);
}
}
Failure description
Failed asserting that true is false.
Method asserts that a variable is of a given type
assertInstanceOf(object $expected, object $actual, string $message = '')
Reports an error identified by $message
if $expected
is not an instance of $actual
.
assertNotInstanceOf()
is the inverse of this assertion and takes the same arguments.
<?php
class InstanceOfTest extends CIUnit_Framework_TestCase
{
public function testFailure()
{
$this->assertInstanceOf('RunTimeException', new Exception());
}
}
Failure description
Failed asserting that Exception Object (...) is an instance of RunTimeException.
Method assert that variable is of a given type
assertInternalType(object $expected, object $actual, string $message = '')
Reports an error identified by $message
if $expected
is not of type $actual
.
assertNotInternalType()
is the inverse of this assertion and takes the same arguments.
<?php
class InternalTypefTest extends CIUnit_Framework_TestCase
{
public function testFailure()
{
$this->assertInternalType('string', 1);
}
}
Failure description
Failed asserting that 1 is of type "string".
Method asserts that a variable is NULL
assertNull(mixed $var, string $message = '')
Reports an error identified by $message
if $var
is not NULL
.
assertNotNull()
is the inverse of this assertion and takes the same arguments.
<?php
class NullTest extends CIUnit_Framework_TestCase
{
public function testFailure()
{
$this->assertNull('string');
}
}
Failure description
Failed asserting that 'string' is null.
Method assert that the size of two arrays (or Countable
or Iterator
objects) is the same
assertSameSize(mixed $expected, mixed $actual string $message = '')
Reports an error identified by $message
if the number of elements in $expected
is not the same as in $actual
.
assertNotSameSize()
is the inverse of this assertion and takes the same arguments.
<?php
class SameSizeTest extends CIUnit_Framework_TestCase
{
public function testFailure()
{
$this->assertSameSize(array(1, 2), array());
}
}
Failure description
Failed asserting that actual size 0 matches expected size 2.
Method asserts that a condition is true
assertTrue(bool $condition, string $message = '')
Reports an error identified by $message
if $condition
is FALSE
.
<?php
class TrueTest extends CIUnit_Framework_TestCase
{
public function testFailure()
{
$this->assertTrue(FALSE);
}
}
Failure description
Failed asserting that false is true.
Method asserts that a value is greater than another value
assertGreaterThan(mixed $expected, mixed $actual, string $message = '')
Reports an error identified by $message
if $expected
is not greater than $actual
.
For various types, comparison is done according to this table.
<?php
class GreaterThanTest extends CIUnit_Framework_TestCase
{
public function testFailure()
{
$this->assertGreaterThan(12, 2);
}
}
Failure description
Failed asserting that 2 is greater than 12.
Method asserts that a value is greater than or equal to another value
AssertGreaterThanOrEqual(mixed $expected, mixed $actual, string $message = '')
Reports an error identified by $message
if $expected
is not greater than or equal to $actual
.
For various types, comparison is done according to this table.
<?php
class GreaterThanOrEqualTest extends CIUnit_Framework_TestCase
{
public function testFailure()
{
$this->AssertGreaterThanOrEqual(13, 12);
}
}
Failure description
Failed asserting that 12 is greater or equal to 13.
Method asserts that a value is less than another value
AssertLessThan(mixed $expected, mixed $actual, string $message = '')
Reports an error identified by $message
if $expected
is not less than $actual
.
For various types, comparison is done according to this table.
<?php
class LessThanTest extends CIUnit_Framework_TestCase
{
public function testFailure()
{
$this->assertLessThan(13, 21);
}
}
Failure description
Failed asserting that 21 is less than 13.
Method asserts that a value is less than or equal to another value
AssertLessThanOrEqual(mixed $expected, mixed $actual, string $message = '')
Reports an error identified by $message
if $expected
is not less than or equal to $actual
.
For various types, comparison is done according to this table.
<?php
class LessThanOrEqualTest extends CIUnit_Framework_TestCase
{
public function testFailure()
{
$this->assertLessThanOrEqual(13, 15);
}
}
Failure description
Failed asserting that 15 is less or equal to 13.
Method assert that string starts with a give prefix
assertStringStartsWith(string $prefix, string $string, string $message = '')
Reports an error identified by $message
if $string
does not start with $prefix
.
assertStringNotStartsWith()
is the inverse of this assertion and takes the same arguments.
<?php
class StringStartsWithTest extends CIUnit_Framework_TestCase
{
public function testFailure()
{
$this->assertStringStartsWith('prse', 'presentation');
}
}
Failure description
Failed asserting that 'presentation' string starts with 'prse'.
Method assert that string starts with a give prefix
assertStringEndsWith(string $suffix, string $string, string $message = '')
Reports an error identified by $message
if $string
does not end with $suffix
.
assertStringNotEndsWith()
is the inverse of this assertion and takes the same arguments.
<?php
class StringEndsWithTest extends CIUnit_Framework_TestCase
{
public function testFailure()
{
$this->assertStringEndsWith('tatiodn', 'presentation');
}
}
Failure description
Failed asserting that 'presentation' string ends with 'tatiodn'.
Method asserts that a string matches a given regular expression
assertStringMatchesRegex(string $regex, string $string, string $message = '')
Reports an error identified by $message
if $string
does not match $regex
.
assertStringNotMatchesRegex()
is the inverse of this assertion and takes the same arguments.
<?php
class StringMatchesRegexTest extends CIUnit_Framework_TestCase
{
public function testFailure()
{
$this->assertStringMatchesRegex('/^prez/', 'presentation');
}
}
Failure description
Failed asserting that 'presentation' string matches the regular expression '/^prez/'.
Method asserts that a class has a specified attribute
assertClassHasAttribute(string $attribute, string $className, string $message = '')
Reports an error identified by $message
if class $className
does not have attribute $attribute
.
assertClassNotHasAttribute()
is the inverse of this assertion and takes the same arguments.
<?php
class ClassHasAttributeTest extends CIUnit_Framework_TestCase
{
public function testFailure()
{
$this->assertClassHasAttribute('notExistingAttribute', 'ClassWithAttributes');
}
}
Failure description
Failed asserting that 'ClassWithAttributes' class has attribute 'notExistingAttribute'.
Method asserts that a class has a specified static attribute
assertClassHasStaticAttribute(string $attribute, string $className, string $message = '')
Reports an error identified by $message
if class $className
does not have static attribute $attribute
.
assertClassNotHasStaticAttribute()
is the inverse of this assertion and takes the same arguments.
<?php
class ClassHasStaticAttributeTest extends CIUnit_Framework_TestCase
{
public function testFailure()
{
$this->assertClassHasStaticAttribute('nonStaticAttribute', 'ClassWithAttributes');
}
}
Failure description
Failed asserting that 'ClassWithAttributes' class has static attribute 'nonStaticAttribute'.
Method asserts that an object has a specified attribute
assertObjectHasAttribute(string $attribute, object $object, string $message = '')
Reports an error identified by $message
if $object
does not have attribute $attribute
.
assertObjectNotHasAttribute()
is the inverse of this assertion and takes the same arguments.
<?php
class ObjectHasAttributeTest extends CIUnit_Framework_TestCase
{
public function testFailure()
{
$this->assertObjectHasAttribute('publicAttribute', new ClassWithAttributes());
}
}
Failure description
Failed asserting that ClassWithAttributes Object(...) has attribute 'publicAttribute'.
Method asserts that an object has a specified static attribute
assertObjectHasStaticAttribute(string $attribute, object $object, string $message = '')
Reports an error identified by $message
if $object
does not have static attribute $attribute
.
assertObjectNotHasStaticAttribute()
is the inverse of this assertion and takes the same arguments.
<?php
class ObjectHasStaticAttributeTest extends CIUnit_Framework_TestCase
{
public function testFailure()
{
$this->assertObjectHasStaticAttribute('publicAttribute', new ClassWithAttributes());
}
}
Failure description
Failed asserting that ClassWithAttributes Object(...) has static attribute 'publicAttribute'.
Release date: March 6, 2013
- Added assertGreaterThan
- Added assertGreaterThanOrEqual
- Added assertLessThan
- Added assertLessThanOrEqual
- Added assertStringStartsWith
- Added assertStringEndsWith
- Added assertStringMatchesRegex
Release date: March 14, 2013
- Added assertClassHasAttribute
- Added assertClassHasStaticAttribute
- Added assertObjectHasAttribute
- Added assertObjectHasStaticAttribute
- Fixed "Allowed memory exhausted" bug when processing exceptions
Have a bug? Please create an issue here on GitHub!
https://github.com/agop/ciunit-framework/issues
Copyright © 2013
Licensed under the The BSD 3-Clause License
The front-end is developed using the Bootstrap framework by Twitter.
I took my inspiration from JUnit and PHPUnit, thank you guys for being there for us.