Testing untestable code - IPC12
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

Testing untestable code - IPC12

on

  • 791 views

 

Statistics

Views

Total Views
791
Views on SlideShare
782
Embed Views
9

Actions

Likes
1
Downloads
4
Comments
0

1 Embed 9

http://www.php-talks.com 9

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Testing untestable code - IPC12 Presentation Transcript

  • 1. Testing untestable codeStephan Hochdörfer, bitExpert AG
  • 2. Testing untestable code About me  Stephan Hochdörfer, bitExpert AG  Department Manager Research Labs  enjoying PHP since 1999  S.Hochdoerfer@bitExpert.de  @shochdoerfer
  • 3. Testing untestable code No excuse for writing bad code!
  • 4. Testing untestable code "Hang the rules. Theyre more like guidelines anyway." Elizabeth Swann, Pirates of the Caribbean
  • 5. Testing untestable code "There is no secret to writing tests, there are only secrets to write testable code!" Miško Hevery
  • 6. Testing untestable code What is „untestable code“?
  • 7. Testing untestable code "...our test strategy requires us to have more control [...] of the sut." Gerard Meszaros, xUnit Test Patterns: Refactoring Test Code
  • 8. Testing untestable code In a perfect world... Unittest Unittest SUT SUT
  • 9. Testing untestable code Legacy code is not perfect... Dependency Dependency Unittest Unittest SUT SUT Dependency Dependency
  • 10. Testing untestable code ... Legacy code is not perfect... Dependency Dependency Unittest Unittest SUT SUT Dependency Dependency ...
  • 11. Testing untestable code ... Legacy code is not perfect... Dependency Dependency Unittest Unittest SUT SUT Dependency Dependency ...
  • 12. Testing untestable code How to get „testable“ code?
  • 13. Testing untestable code How to get „testable“ code? Refactoring
  • 14. Testing untestable code "Before you start refactoring, check that you have a solid suite of tests." Martin Fowler, Refactoring
  • 15. Testing untestable code Which path to take?
  • 16. Testing untestable code Which path to take? Do not change existing code!
  • 17. Testing untestable code Examples Object Construction External resources Language issues
  • 18. Testing untestable code Object construction <?php class Car { private $Engine; public function __construct($sEngine) { $this­>Engine = Engine::getByType($sEngine); } }
  • 19. Testing untestable code Object construction - Autoload <?php function run_autoload($psClass) { $sFileToInclude = strtolower($psClass)..php; if(strtolower($psClass) == engine) { $sFileToInclude = /custom/mocks/.       $sFileToInclude; } include($sFileToInclude); } // Testcase spl_autoload_register(run_autoload); $oCar = new Car(Diesel); echo $oCar­>run();
  • 20. Testing untestable code Object construction <?php include(Engine.php); class Car { private $Engine; public function __construct($sEngine) { $this­>Engine = Engine::getByType($sEngine); } }
  • 21. Testing untestable code Object construction - include_path <?php ini_set(include_path, /custom/mocks/.PATH_SEPARATOR. ini_get(include_path)); // Testcase include(car.php); $oCar = new Car(Diesel); echo $oCar­>run();
  • 22. Testing untestable code Object construction – Stream Wrapper <?php class CustomWrapper {   private $_handler;   function stream_open($path, $mode, $options,  &$opened_path) {     stream_wrapper_restore(file);  // @TODO: modify $path before fopen     $this­>_handler = fopen($path, $mode);     stream_wrapper_unregister(file);     stream_wrapper_register(file, CustomWrapper);     return true;   } }
  • 23. Testing untestable code Object construction – Stream Wrapper stream_wrapper_unregister(file); stream_wrapper_register(file, CustomWrapper);
  • 24. Testing untestable code Object construction – Stream Wrapper <?php class CustomWrapper { private $_handler; function stream_read($count) { $content = fread($this­>_handler, $count); $content = str_replace(Engine::getByType,        AbstractEngine::get, $content); return $content; } }
  • 25. Testing untestable code External resources
  • 26. Testing untestable code External resources Database Webservice Filesystem Mailserver
  • 27. Testing untestable code External resources – Mock database
  • 28. Testing untestable code External resources – Mock database Provide own implementation
  • 29. Testing untestable code External resources – Mock database ZF1 example: $db = new Custom_Db_Adapter(array()); Zend_Db_Table::setDefaultAdapter($db);
  • 30. Testing untestable code External resources – Mock databasePHPUnit_Extensions_Database_TestCase
  • 31. Testing untestable code External resources – Mock database require_once "PHPUnit/Extensions/Database/TestCase.php" class MySampleTest extends  PHPUnit_Extensions_Database_TestCase {     public function getConnection() {         $pdo = new PDO(sqlite::memory:);         return $this­>createDefaultDBConnection(         $pdo,          :memory:     );     }     public function getDataSet() {         return $this­>createFlatXMLDataSet(         dirname(__FILE__)./_files/data.xml     );     } }
  • 32. Testing untestable code External resources – Mock database Proxy for your SQL Server
  • 33. Testing untestable code External resources – Mock webservice
  • 34. Testing untestable code External resources – Mock webservice Provide own implementation
  • 35. Testing untestable code External resources – Mock webservice Host redirect via /etc/hosts
  • 36. Testing untestable code External resources – Mock filesystem
  • 37. Testing untestable code External resources – Mock filesystem <?php // set up test environmemt vfsStream::setup(exampleDir); // create directory in test enviroment mkdir(vfsStream::url(exampleDir)./sample/); // check if directory was created echo vfsStreamWrapper::getRoot()­>hasChild(sample);
  • 38. Testing untestable code External resources – Mock Mailserver
  • 39. Testing untestable code External resources – Mock Mailserver Use fake mail server
  • 40. Testing untestable code External resources – Mock Mailserver $ cat /etc/php5/php.ini | grep sendmail_path sendmail_path=/usr/local/bin/logmail $ cat /usr/local/bin/logmail cat >> /tmp/logmail.log
  • 41. Testing untestable code Dealing with language issues
  • 42. Testing untestable code Dealing with language issues Testing your privates?
  • 43. Testing untestable code Dealing with language issues <?php class CustomWrapper { private $_handler; function stream_read($count) { $content = fread($this­>_handler, $count); $content = str_replace(          private function,          public function,           $content       ); return $content; } }
  • 44. Testing untestable code Dealing with language issues $myClass = new MyClass(); $reflectionClass  = new ReflectionClass(MyClass); $reflectionMethod = $reflectionClass­> getMethod(mydemo); $reflectionMethod­>setAccessible(true); $reflectionMethod­>invoke($myClass);
  • 45. Testing untestable code Dealing with language issues Overwrite internal functions?
  • 46. Testing untestable code Dealing with language issues pecl install runkit-0.9
  • 47. Testing untestable code Dealing with language issues - Runkit <?php ini_set(runkit.internal_override, 1); runkit_function_redefine(mail,,return  true;); ?>
  • 48. Testing untestable code Dealing with language issues pecl install funcall-0.3.0alpha
  • 49. Testing untestable code Dealing with language issues - Funcall <?php function my_func($arg1, $arg2) {     return $arg1.$arg2; } function post_cb($args,$result, $process_time) {   // return custom result based on  $args } fc_add_post(my_func,post_cb); var_dump(my_func(php, c));
  • 50. Testing untestable code Dealing with language issues funcall for methods?
  • 51. Testing untestable code Dealing with language issues git clone https://github/juliens/AOP
  • 52. Testing untestable code Dealing with language issues - AOP <?php aop_add_after(Car::drive*,  adviceForDrive);
  • 53. Testing untestable code Dealing with language issues - AOP <?php $advice = function(AopTriggeredJoinpoint $jp) {   $returnValue =       $jp­>getReturnedValue();   // modify the return value   $returnValue = 1234;   $jp­>setReturnedValue($returnValue); }; aop_add_after(Car­>drive(), $advice);
  • 54. Testing untestable code And now? Spaghetti mess... <?php $all_tables_query  =  SELECT table_name, MAX(version) as version FROM ...; $all_tables_result =  PMA_query_as_controluser($all_tables_query); // If a HEAD version exists if (PMA_DBI_num_rows($all_tables_result) > 0) { ?>     <div id="tracked_tables">     <h3><?php echo __(Tracked tables);?></h3> <?php }
  • 55. Testing untestable code What else? Generative Programming
  • 56. Testing untestable code Generative Programming Configuration Configuration (DSL) (DSL) 1..n Implementation- Implementation- components Generator Generator Product components Product
  • 57. Testing untestable code Generative Programming Configuration Configuration (DSL) (DSL) Customer 22 Customer Implementation- Implementation- components Generator Generator Customer 11 components Customer
  • 58. Testing untestable code Generative Programming Configuration Configuration (DSL) (DSL) Test Test Enviroment Enviroment Implementation- Implementation- components Generator Generator Prod. Prod. components Enviroment Enviroment
  • 59. Testing untestable code Generative Programming A frame is a data structure for representing knowledge.
  • 60. Testing untestable code Frame <?php class Car { private $Engine; public function __construct($sEngine) { $this­>Engine = <!{Factory}!>:: getByType($sEngine); } }
  • 61. Testing untestable code ContentProvider for the Frame public class MyContentProvider extends     AbstractContentProvider {     public SlotConfiguration computeSlots(         FeatureConfiguration config) {         SlotConfiguration sl = new SlotConfiguration();         if(config.hasFeature("unittest")) {             sl.put("Factory", "FactoryMock");         } else {             sl.put("Factory", "EngineFactory");         }         return sl;     } }
  • 62. Testing untestable code Generated result – Test Enviroment <?php class Car { private $Engine; public function __construct($sEngine) { $this­>Engine = FactoryMock:: getByType($sEngine); } }
  • 63. Testing untestable code Generated result – Prod. Enviroment <?php class Car { private $Engine; public function __construct($sEngine) { $this­>Engine = EngineFactory:: getByType($sEngine); } }
  • 64. Testing untestable code Curious for more? http://replicatorframework.org
  • 65. Thank you!
  • 66. http://joind.in/7314
  • 67. Testing untestable code Flickr Credits http://www.flickr.com/photos/andresrueda/3452940751/ http://www.flickr.com/photos/andresrueda/3455410635/