I hope I don’t have to convince you of the importance of unit testing… and if I do, just read this post from Adil Akhter on the importance of unit testing.
In Symfony, A lime test library has been included and makes it easy for us to do these kinds of tests.
You can do this by requiring the unit.php file that is included in the Symfony package.
And create an instance of the lime_test class
require_once dirname(__FILE__).'/../bootstrap/unit.php';
$t = new lime_test(3, new lime_output_color());
In this case with initialize $t as an instance of lime_test and in the constructor we set 3 as variable, being the number of tests we want to run.
If we run this test in a command prompt, it will execute our test and display the results line by line.
To make it more readable you could set extra text like this:
$t->diag(‘this is the start of the test’);
From the moment the class was initialized, you can create any test you want by simply using the predefined functions in the lime_test class.
Diag generates text
is compares two values
like compares two strings
and so on…
In our little test we simply use ‘is’ as we only want to check if the values that have been calculated are correct if we run it trough a function.
What we want to do is do a unit test of 2 functions, one called sum and one called multiply. The best way to test them is to split them up into 2 unit tests, but in my example, I’ll test them both at once.
The data we can use for this test is set up in an YML file called fixtures.yml and is placed in the same folder as the file that contains the unit test.
I like using this YML file for this kind of tests for several reasons:
- You can set a huge amount of test data (almost equal to xml)
- The test data is separated from the code, and code is therefore more readable
We save our file in the same folder as the yml file and call it: SumTest.php, the yml file, containing all the test data is called fixtures.yml.
Here is all code that I wrote in the SumTest.php file, and under the code, you can find the complete explanation.
require_once(dirname(__FILE__).'/../../bootstrap/unit.php');
/**
* Sum of set of values
* @param array $inputData
* @return integer $sum
*/
function sum($inputData){
$sum = 0;
$sum = $inputData['a'] + $inputData['b'] + $inputData['c'];
return $sum;
}
/**
* Multiply set of values
* @param array $inputData
* @return integer $multiply
*/
function multiply($inputData){
$multiply = 0;
$multiply = $inputData['a'] * $inputData['b'] * $inputData['c'];
return $multiply;
}
// Initialize the test engine.
$t = new lime_test(6, new lime_output_color());
$t->diag('Our small function test');
// retrieve test data
$dataProvider = sfYaml::load(dirname(__FILE__) . "/fixtures.yml");
//loop tests
foreach($dataProvider as $testId => $testData) {
//for each test run the test data trough the functions, retrieving a result
$sum = sum($testData['input']);
$multiply = multiply($testData['input']);
//get the expected data from the yml file
$expectedData = $testData['expected'];
//do the actual unit test for the sum and multiply
$t->is($sum, $expectedData['sum'], " Result of the sum is: " . $sum);
$t->is($multiply, $expectedData['multiply'], " Result of the multiplication is: " . $multiply);
}
On top of the file I wrote 2 functions that I wanted to test, sum and multiply, a better way would be to include them from the file where they were originally placed and used. If you copy your method in your test, and later change the original method, your test will be totally useless, so be sure to include that in stead of copying it.
The both simply loop the data and add or multiply it and finally return the result.
After initializing our lime_test and displaying a line on the output, we get all test data from the YML file.
We can execute all tests by looping them and using the test data by sending it trough our 2 functions we wrote on top of the page.
Get the expected results out of that same YML file and run them both trough the unit tests.
In our case we simply compare the result of the function with the expected result giving a third parameter as output result on success, or giving an error on failure.
In our YML file data is set as followed.
Test1:
input: { a: 17, b: 26, c: 35}
expected: { sum: 78, multiply: 15470 }
Test2:
input: { a: 2, b: 13, c: 18}
expected: { sum: 33, multiply: 468 }
Test3:
input: { a: 3, b: 0, c: 1500}
expected: { sum: 1503, multiply: 0 }
If we run our test in dos prompt, we see that all results went just fine, even when trying to multiply by 0 as this was an expected output.
You can run your test by calling Symfony test:unit and then the name of your test without Test.php on the end(Remember our file was called SumTest.php).

Hope this was a bit clear for y’all.
greetings.
K.