unit tests in_symfony
TRANSCRIPT
UNIT TESTS IN SYMFONY
Sayed Ahmed
B.Sc. Eng. in Computer Science & EngineeringM. Sc. in Computer Science
Exploring Computing for 14+ years
[email protected]://sayed.justetc.net
TESTS IN SYMFONY Tests in symfony
unit tests|Unit Testing functional tests
Unit Tests verify that each method and function is working properly Each test must be as independent as possible from the
others Corresponding Directory (to keep tests)
(test/unit/) Function Tests
verify that the resulting application behaves correctly as a whole
Corresponding Directory (to keep tests) (test/functional/)
TESTING LIBRARY Lime
http://trac.symfony-project.org/wiki/LimeTestingFramework
PHPUnit http://
www.phpunit.de/manual/current/en/index.html
THE LIME TESTING FRAMEWORK All lime tests can be started with the same
code require_once dirname(__FILE__).'/../bootstrap/unit.php'; $t = new lime_test(1);
How testing works by calling a method or a function with a set of
predefined inputs and then comparing the results with the
expected output This comparison determines whether a test
passes or fails
COMPARISON METHODS Lime provides several methods to ease the
comparison ok($test) Tests a condition and passes if it is true is($value1, $value2) Compares two values and
passes if they are equal (==) Isnt($value1, $value2) Compares two values and
passes if they are not equal like($string, $regexp) Tests a string against a
regular expression unlike($string, $regexp) Checks that a string
doesn't match a regular expression is_deeply($array1, $array2) Checks that two
arrays have the same values
TESTING METHODS Other Convenient Testing Methods
fail() Always fails--useful for testing exceptions pass() Always passes--useful for testing
exceptions skip($msg, $nb_tests) Counts as $nb_tests
tests--useful for conditional tests todo() Counts as a test--useful for tests yet to be
written
RUNNING UNIT TESTS Run the PHP file that includes the tests Or use the symfony task: test:unit Example unit test file
// test/unit/JobeetTest.php require_once dirname(__FILE__).'/../bootstrap/unit.php'; $t = new lime_test(1); $t->pass('This test always passes.');
Run Tests $ php test/unit/JobeetTest.php $ php symfony test:unit Jobeet
TESTING SLUGIFY Example
each line only tests one thing All lime test methods take a string as their last
argument that serves as the description for the test. // test/unit/JobeetTest.php require_once dirname(__FILE__).'/../bootstrap/unit.php'; $t = new lime_test(6); $t->is(Jobeet::slugify('Sensio'), 'sensio'); $t->is(Jobeet::slugify('sensio labs'), 'sensio-labs'); $t->is(Jobeet::slugify('sensio labs'), 'sensio-labs'); $t->is(Jobeet::slugify('paris,france'), 'paris-france'); $t->is(Jobeet::slugify(' sensio'), 'sensio'); $t->is(Jobeet::slugify('sensio '), 'sensio');
CODE COVERAGE To help you check
that all your code is well tested Symfony provides the test:coverage task
Check: what are not covered in a lib file or directory $ php symfony test:coverage test/unit/JobeetTest.php
lib/Jobeet.class.php Check: which lines are not covered
$ php symfony test:coverage --detailed test/unit/JobeetTest.php lib/Jobeet.class.php
when the task indicates that your code is fully unit tested it just means that each line has been executed, not that all
the edge cases have been tested test:coverage relies on XDebug
you need to install it and enable it first
ADDING TESTS FOR NEW FEATURES
ADDING TESTS BECAUSE OF A BUG After you found the bug
First write a test To confirm the bug
Then update code to fix Run the tests again
DOCTRINE UNIT TESTS Use separate test database for testing
CONFIGURATION PRINCIPLES IN SYMFONY
TESTING CONFIGURATION: EXAMPLE
EXAMPLE: UNIT TEST FILE // test/unit/model/JobeetJobTest.php
include(dirname(__FILE__).'/../../bootstrap/Doctrine.php');
$t = new lime_test(1); $t->comment('->getCompanySlug()'); $job = Doctrine_Core::getTable('JobeetJob')-
>createQuery()->fetchOne(); $t->is($job->getCompanySlug(),
Jobeet::slugify($job->getCompany()), '->getCompanySlug() return the slug for the company');
ANOTHER EXAMPLE UNIT TEST FILE
TEST OTHER DOCTRINE CLASSES You can add unit tests for other classes and
methods as well
UNIT TESTING IS IMPORTANT The symfony framework itself has more than
9000 tests
REFERENCES http://www.symfony-project.org/jobeet/1_4/Doctrine/
en/08