Kahlan – Implementing the Goodness of RSpec/Jasmine in PHP Testing
In PHP, there are several testing frameworks you can use to test your applications. Popular ones including PHPUnit, Codeception, Behat and PHPSpec.
PHPUnit helps you to unit test your code, Behat, PHPSpec gives you the ability to do a form of behavioural testing.
Today, I introduce to you Kahlan, A very awesome full-featured Unit and BDD test framework which brings the goodness of RSpec and Jasmine to testing.
I do a lot of Javascripting currently and also worked on Rails app before now. Test tools like Jasmine and Rspec provide me the ability to use the describe-it
syntax which makes the testing code very simple and legible to write and understand easily.
Example code
Jasmine
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
require('jasmine-expect'); var statesAndCapital = require('../src/app'); describe('states and Capital', function() { describe('all', function() { it('should be a collection of strings in an object', function() { expect(statesAndCapital.all).toBeObject(); }); }); describe('Each City', function() { it('should return `Ikeja` as the Capital of `Lagos`', function() { expect(statesAndCapital.all.Lagos).toEqual('Ikeja'); }); }); }); |
Meanwhile, In PHPUnit, Similar code would be like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
<?php namespace BusayoTests; use BusayoLibStatesAndCapital; class StatesAndCapitalTest extends PHPUnit_Framework_TestCase { /** * Test that the returned result is an array */ public function testAll() { $result = StatesAndCapital->all(); $this->assertInternalType('array', $result); } /** * Test that the returned result is the Capital of Lagos */ public function testCity() { $result = StatesAndCapital->all(); $this->assertEquals('Ikeja', $result['Lagos']); } } ?> |
Now, I feel this block of code is longer and the results are not so descriptive like that of Jasmine and Rspec when the code is run from the terminal.
Kahlan takes care of all of that. It’s very descriptive. Similar code written with Kahlan:
Kahlan
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<?php namespace BusayoSpec; use BusayoStateAndCapital; describe('states and Capital', function() { describe('all', function() { it('should be a collection of strings in an array', function() { $result = StateAndCapital::all(); expect($result)->toBeA('array'); }); }); describe('Each City', function() { it('should return `Ikeja` as the Capital of `Lagos`', function() { $result = StateAndCapital::getCity('Lagos'); expect($result)->toEqual('Ikeja'); }); }); }); ?> |
When we run this spec file using ./bin/kahlan
If successful, results look like this:
If one assertion fails, we will have the result like so:
Awesome!!, See how detailed and descriptive the error message is,…taking into consideration all the description we had written in our test suite.
Note: Kahlan provides monkey patching out of the box also. Is that not awesome? You should definitely try Kahlan out. It’s Great!.
The source code for this test experiment can be found here. You can go through it, clone it, run composer install
and then run the test using ./bin/kahlan --coverage=4
Please if you have any questions or observations, feel free to drop it in the comments section below.

- How to build your own Youtube – Part 10 - August 1, 2016
- How to build your own Youtube – Part 9 - July 25, 2016
- How to build your own Youtube – Part 8 - July 23, 2016
- How to build your own Youtube – Part 6 - July 6, 2016
- Introducing Laravel Password v1.0 - July 3, 2016
- How to build your own Youtube – Part 5 - June 28, 2016
- How to build your own Youtube – Part 4 - June 23, 2016
- How to build your own Youtube – Part 3 - June 15, 2016
- How to build your own Youtube – Part 2 - June 8, 2016
- How to build your own Youtube – Part 1 - June 1, 2016