Unittests für Dummies

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

0 comments

Post a comment

    Post a comment
    Embed Video
    Edit your comment Cancel

    Favorites, Groups & Events

    Unittests für Dummies - Presentation Transcript

    1. Unit Testing für Dummies 15.11.2009 Lars Jankowfsky, swoodoo AG Thorsten Rinne, Mayflower GmbH
    2. About me: PHP, C++, Developer, Software Architect since 1992 PHP since 1998 Many successful projects from 2 to 20 developers Running right now three projects using eXtreme Programming CTO and (Co-)Founder swoodoo AG (Co-)Founder OXID eSales AG
    3. About me: Diplom-Informatiker (FH) Certified Scrum Master Zend Certified Engineer PHP since 1999 Many successful projects from 2 to 6 developers using Scrum, eXtreme Programming, Crystal Clear Senior Developer / Team Lead at Mayflower GmbH Master of phpMyFAQ
    4. (c) spackletoe http://www.flickr.com/photos/spackletoe/90811910/) (c) aboutpixel.de
    5. Unit Tests? PHPUnit Fixtures Stubs Pitfalls
    6. PHPUnit http://www.phpunit.de/
    7. PHPUnit PHPUnit phpunit myTest user@workshop:/var/www/thorsten-zfguestbook/tests/ application$ phpunit controllers_PostsControllerTest
    8. Fixtures
    9. Fixtures Fixtures Make sure that tests don‘t alter fixture Fixture is FIXture if you feel creating fixtures is too much work - refactor more! Do never let tests leave altered tests
    10. Fixtures the Ruby way... Fixtures Ruby uses YAML www.yaml.org PHP YAML support done by using Syck Syck = YAML + fast http://whytheluckystiff.net/syck/ http://www.frontalaufprall.com/2008/05/05/php-unit- database-fixtures-the-ruby-way/
    11. YAML loading Fixtures public static function create($fileName) { $fileName = 'Fixtures'.DIRECTORY_SEPARATOR.$fileName; ob_start(); include $fileName; $fileContents = ob_get_contents(); ob_clean(); $yamlData = syck_load($fileContents); return $yamlData; }
    12. YAML storing Fixtures public static function load($fixtures, $tableName) { if (is_array($fixtures) && count($fixtures)) { foreach ($fixtures as $fixture) { if (is_array($fixture) && is_array(current($fixture))) { Fixtures::load($fixture, $tableName); } $fields = array_keys($fixture); $statement = "INSERT INTO $tableName (" . implode(', ', $fields) . ") VALUES (:" . implode(", :", $fields) . ")"; $stmt = self::$_db->prepare($statement); if (count($fixture)) { foreach ($fixture as $key => $value ) { $stmt->bindValue(':'.$key, $value); } } $stmt->execute(); self::$_usedTables[$tableName] = $tableName; } } }
    13. YAML - cleanup Fixtures if (!empty(self::$_usedTables)) { foreach (array_reverse(self::$_usedTables) as $tableName) { self::$_db->execute("TRUNCATE TABLE $tableName"); } }
    14. Fixtures the other side ... Fixtures manual fixtures are too much work use a test database think about automatic creation of YAML files
    15. Stubs
    16. Mocking stubs? Stubs Unittesting is about testing a unit of work, not a complete workflow isolates your code from external dependencies can be done with PHPUnit, but you don‘t need to
    17. Mocking stubs The PHPUnit way Stubs /** * A simple stub providing a simple result directly instead of using the database */ class UserModelStub extends UserModel { public getUserCount() { return 10; } } UserModelStub extends PHPUnit_Framework_Testcase { public function testGetUserCount() { $stub = $this->getMock(‘UserModel‘); $stub->expects($this->any())->method(‘getUserCount‘)->will($this- >returnValue(10)); } }
    18. Pitfalls
    19. Code the unit test first. Pitfalls OOP, public, private Globals Superglobals Sessions Cookies
    20. Dependencies ... Pitfalls Separate logic from view create accessors, add all parameters in calls
    21. Dependency Example Pitfalls class displayUserDetails() { /** * Processes input and sends user first name, last name to display; */ function show() { global $dbLink; global $templateEngine; $itemId = (int) $_REQUEST['user_id']; $firstName = $dbLink->getOne("select first_name from users where id = $itemId"); $lastName = $dbLink->getOne("select last_name from users where id = $itemId"); $templateEngine->addTemplateVar('firstName', $firstName); $templateEngine->addTemplateVar('lastName', $lastName); $templateEngine->display(); } }
    22. Dependency Example Pitfalls /** * A view class responsible for displaying user details. */ class userView() { /** * Loads user object and sends first name, last name to display */ public function show() { $userId = $this->_inputProcessor->getParameter("user_id"); $this->templateEngine->addTemplateVar('user', $this->model->loadUser(userId)); $this->templateEngine->display(); } } /** * And the corresponding model */ class userModel() { public function loadUser($userId) { $user = new User( $userId ); return array('firstName' => $user->getFirstName(), 'lastName' => $user->getLastName()); } }
    23. STOP
    24. Layer Example Pitfalls class someOtherClass { var $setting; function calculateSomething($a, $b) { return $a+$b; } } class myOldNastyClass { function needToTestThisFunction() { $class = new someOtherClass(); $z = $_GET['input']; // .... return $class->calculateSomething( $class->setting, $z); } }
    25. Layer Example Pitfalls class someOtherClass { private $setting; public function calculateSomething($a, $b) { return $a+$b; } public function setSetting($set) { $this->setting = $set; } public function getSetting() { return $this->setting; } } class myInput { public function getParameter($name) { return $_GET[$name]; } } class myOldNastyClass { private $input; // set e.g. in constructor public function needToTestThisFunction(someOtherClass &$class, $z) { $z = $input->getParameter('input'); // .... return $class->calculateSomething( $class->getSetting(), $z); } }
    26. (c) istockphoto
    27. „Questions?“
    SlideShare Zeitgeist 2009

    + dodgerisdodgeris Nominate

    custom

    104 views, 0 favs, 1 embeds more stats

    Refactoring, Agile Entwicklung, Continuous Integrat more

    More info about this document

    © All Rights Reserved

    Go to text version

    • Total Views 104
      • 103 on SlideShare
      • 1 from embeds
    • Comments 0
    • Favorites 0
    • Downloads 8
    Most viewed embeds
    • 1 views on http://www.insidephp.com

    more

    All embeds
    • 1 views on http://www.insidephp.com

    less

    Flagged as inappropriate Flag as inappropriate
    Flag as inappropriate

    Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

    Cancel
    File a copyright complaint
    Having problems? Go to our helpdesk?

    Categories

    Tags