RSS
 

Posts Tagged ‘Symfony’

Unit testing in Symfony

26 May

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.

 

Symfony Live 2010

08 Jan

Sensio Labs is proud to announce that the second symfony live conference will be held February 16-17th in Paris (in English). The symfony live conference is a unique opportunity to meet the symfony community, talk with the symfony core team, and share your experience with the framework.

And I am happy to announce that I will attend the 2 day conference.

 
 

Interview: Stefan Koopmanschap

22 Sep

3317597132_6be12c93c7

A few weeks ago I thought it might be cool to get some interesting guys, which are occupied with projects in PHP, interviewed. And guess what, we got on contact with Mr. Stefan Koopmanschap and he was kind enough to answer all our questions.

Stefan Koopmanschap (‘left’) is a PHP developer, consultant and trainer with an eye for best practices. He works at Unet as (symfony) developer and development team leader. He is a community person and is active in the european PHP community as secretary of the phpBenelux Usergroup as well as in the Symfony community by advocating symfony and as the Community Manager.

Stefan has a wide history in Open Source, having been Support Team Leader for phpBB, documentation translator for Zend Framework and community manager, plugin developer and maintainer plus various other things for symfony.

Stefan is also a best practices advocate. He prefers easy and useful explanations of best practices over the academic and theoretical stuff found in most literature.

Hope you enjoy this interview!

Hello Stefan, first of all, thx for taking the time to answer all our questions.

Can you tell us what projects you are currently working on?

At work I am involved in a big project to build an application that will handle all the administration, provisioning and handling of user accounts etc. for the whole VOiP and connectivity of the services we offer. Aside from that, my main projects are being the Community Manager for symfony and also preparing some new talks for the upcoming conferences.

Read the rest of this entry »

 

Basic forms with Symfony

30 Apr

SymfonyOur goal is to create a little crud that contains 4 fields

id (auto increment integer)
name (varchar 50)
value (double)
type (0 or 1)

What I will explain is how to change your fields in the form after creation with generation of a module. So we start with a clean module that contains 1 action class and 3 templates (new, edit and index) and 1 partial (form) that is included on the new and edit template.
We created this module not by hand but with the very easy command: symfony propel:generate-module.

Read the rest of this entry »

 

Interesting People – part 1: Fabien Potencier

04 Apr

Fabien PotencierNothing better to get inspiration from then a nice cup of Nespresso in the evening. We are working with Symfony for a few months (years) now and I was wondering earlier this day: Who are the guys that are able to produce such a nice piece of software? You got to admit it that the framework these guys created is a masterpiece, must be a shitload of work and it is still growing on a daily basis.

Off course there is more then one person creating this software, but 1 man in particular is leading this project: Fabien Potencier.

Read the rest of this entry »

 

symfony 1.3

26 Jan

symfony announced a new release in November 2009 and will be going from 1.2 to 1.3.

It’s wonderful that these guys are thinking ahead and keeping us up to date. Why is this important, you ask? Well would you invest in software that isn’t trying to evolve to a better version, to a more reliable version? I don’t think so. Like in our case: We started developing software in October 2007 and needed a part of the project to be PHP-based. The choice of framework wasn’t that easy but the great documentation and the future developments convinced us to use symphony. And we didn’t regret our choice for one minute. Fabien Potencier and his crew keep their promises to the developers and users.

I can quote him as they give good reasons why they should keep us up to date:

  • the users will have time to learn all the great symfony 1.2 features compared to symfony 1.0
  • the core team will have plenty of time to make symfony 1.3 rock solid
  • the documentation team will have time to write even more tutorials and blog posts
  • the plugin developers will have plenty of time to upgrade their plugins to 1.2 with confidence that their work will still be relevant for 1.3.

Great work guys, keep us up to date, in the mean time we’ll do just fine with the 1.2 version.

 

Jobeet

03 Dec

Symfony, ah we just love the framework… A year ago we started using this php framework and we wrote a post on it a few months ago (read). In 2005 the creators of Symfony wrote a step by step tutorial on how to build a website with their framework. For me and my colleagues, this was the ideal guide to learn it and get to know the best way to use it.

Now, 3 years later, they are publishing a new 24 hour tutorial called Jobeet. If you want to know more and follow this tutorial, click on the link in the sidebar or here.