19
UNIT TESTS IN SYMFONY Sayed Ahmed B.Sc. Eng. in Computer Science & Engineering M. Sc. in Computer Science Exploring Computing for 14+ years [email protected] http://sayed.justetc.net

Unit tests in_symfony

Embed Size (px)

Citation preview

Page 1: Unit tests in_symfony

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

Page 2: Unit tests in_symfony

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/)

Page 3: Unit tests in_symfony

TESTING LIBRARY Lime

http://trac.symfony-project.org/wiki/LimeTestingFramework

PHPUnit http://

www.phpunit.de/manual/current/en/index.html

Page 4: Unit tests in_symfony

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

Page 5: Unit tests in_symfony

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

Page 6: Unit tests in_symfony

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

Page 7: Unit tests in_symfony

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

Page 8: Unit tests in_symfony

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');

Page 9: Unit tests in_symfony

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

Page 10: Unit tests in_symfony

ADDING TESTS FOR NEW FEATURES

Page 11: Unit tests in_symfony

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

Page 12: Unit tests in_symfony

DOCTRINE UNIT TESTS Use separate test database for testing

Page 13: Unit tests in_symfony

CONFIGURATION PRINCIPLES IN SYMFONY

Page 14: Unit tests in_symfony

TESTING CONFIGURATION: EXAMPLE

Page 15: Unit tests in_symfony

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');

Page 16: Unit tests in_symfony

ANOTHER EXAMPLE UNIT TEST FILE

Page 17: Unit tests in_symfony

TEST OTHER DOCTRINE CLASSES You can add unit tests for other classes and

methods as well

Page 18: Unit tests in_symfony

UNIT TESTING IS IMPORTANT The symfony framework itself has more than

9000 tests

Page 19: Unit tests in_symfony

REFERENCES http://www.symfony-project.org/jobeet/1_4/Doctrine/

en/08