Your SlideShare is downloading. ×
0
Testing untestable code
Testing untestable code About me  Stephan Hochdörfer, bitExpert AG  Department Manager Research Labs  enjoying PHP sinc...
Testing untestable code No excuse for writing bad code!
Testing untestable code Not to freak out!
Testing untestable code Creativity matters!
Testing untestable code     "There is no secret to writing tests,       there are only secrets to write               test...
Testing untestable code What is „untestable Code“?
Testing untestable code 1. Wrong object construction         “new” is evil!                          s
Testing untestable code 2. Tight coupling
Testing untestable code No code reuse!
Testing untestable code No isolation → not testable!
Testing untestable code 3. Uncertainty
Testing untestable code       "...our test strategy requires us to       have more control [...] of the sut."      Gerard ...
Testing untestable code In a perfect world...                  Unittest                   Unittest   SUT                  ...
Testing untestable code Legacy code is not perfect...                                 Dependency                          ...
Testing untestable code                                 ... Legacy code is not perfect...                                 ...
Testing untestable code                                 ... Legacy code is not perfect...                                 ...
Testing untestable code How to get „testable“ code?
Testing untestable code How to get „testable“ code?                   Refactoring
Testing untestable code     "Before you start refactoring, check     that you have a solid suite of tests."               ...
Testing untestable code Hope? - Nope...
Testing untestable code Which path to take?
Testing untestable code Which path to take?       Do not change existing code!
Testing untestable code Examples Object Construction   External resources   Language issues
Testing untestable code Object construction <?php class Car {    private $Engine;     public function __construct($sEngine...
Testing untestable code Object construction - Autoload <?php function run_autoload($psClass) {    $sFileToInclude = strtol...
Testing untestable code Object construction <?php include(Engine.php); class Car {    private $Engine;     public function...
Testing untestable code Object construction - include_path <?php ini_set(include_path,    /custom/mocks/.PATH_SEPARATOR.  ...
Testing untestable code Object construction – Stream Wrapper <?php class CustomWrapper {   private $_handler;   function s...
Testing untestable code Object construction – Stream Wrapper stream_wrapper_unregister(file); stream_wrapper_register(file...
Testing untestable code Object construction – Stream Wrapper <?php class CustomWrapper {    private $_handler ;     functi...
Testing untestable code Object construction – Namespaces <?php class Car {    private $Engine;     public function __const...
Testing untestable code External resources
Testing untestable code External resources             Database     Webservice            Filesystem    Mailserver
Testing untestable code External resources – Mock database
Testing untestable code External resources – Mock database          Provide own implementation
Testing untestable code External resources – Mock database                          ZF example:          $db = new Custom_...
Testing untestable code External resources – Mock database PHPUnit_Extensions_Database_TestCase
Testing untestable code External resources – Mock database            Proxy for your SQL Server
Testing untestable code External resources – Mock webservice
Testing untestable code External resources – Mock webservice          Provide own implementation
Testing untestable code External resources – Mock webservice            Host redirect via /etc/hosts
Testing untestable code External resources – Mock filesystem
Testing untestable code External resources – Mock filesystem <?php // set up test environmemt vfsStream::setup(exampleDir)...
Testing untestable code External resources – Mock Mailserver
Testing untestable code External resources – Mock Mailserver                Use fake mail server
Testing untestable code External resources – Mock Mailserver $ cat /etc/php5/php.ini | grep sendmail_path sendmail_path=/u...
Testing untestable code Dealing with language issues
Testing untestable code Dealing with language issues             Testing your privates?
Testing untestable code Dealing with language issues <?php class CustomWrapper {    private $_handler;     function stream...
Testing untestable code Dealing with language issues $myClass = new MyClass(); $reflectionClass = new ReflectionClass(MyCl...
Testing untestable code Dealing with language issues       Overwrite internal functions?
Testing untestable code Dealing with language issues              pecl install runkit-0.9
Testing untestable code Dealing with language issues - Runkit <?php ini_set(runkit.internal_override, 1); runkit_function_...
Testing untestable code Dealing with language issues       pecl install funcall-0.3.0alpha
Testing untestable code Dealing with language issues - Funcall <?php function my_func($arg1, $arg2) {     return $arg1.$ar...
Testing untestable code What else?
Testing untestable code What else?           Generative Programming
Testing untestable code Generative Programming                 Configuration                  Configuration               ...
Testing untestable code Generative Programming                 Configuration                  Configuration               ...
Testing untestable code Generative Programming        A frame is a data structure       for representing knowledge.
Testing untestable code A Frame instance <?php class Car {    private $Engine;     public function __construct($sEngine) {...
Testing untestable code The Frame controller public class MyFrameController extends SimpleFrameController {    public void...
Testing untestable code Generated result - Testcase <?php class Car {    private $Engine;     public function __construct(...
Testing untestable code Generated result - Application <?php class Car {    private $Engine;     public function __constru...
Testing untestable code What is possible?
Testing untestable code What is possible?          Show / hide parts of the code
Testing untestable code What is possible?         Change content of global vars!
Testing untestable code What is possible?              Define pre- or postfixes!
Testing untestable code Is it worth it?
Testing untestable code Conclusions         Change your mindset to write                testable code!
Testing untestable code Conclusions            PHP is a swiss-army knife.                 Use it that way!
http://joind.in/3922
Testing untestable code Bildquellen http://www.flickr.com/photos/andresrueda/3452940751/ http://www.flickr.com/photos/andr...
Upcoming SlideShare
Loading in...5
×

Testing untestable code - phpconpl11

1,220

Published on

Published in: Technology, Business
2 Comments
0 Likes
Statistics
Notes
  • Thank you. Was a pleasure to attend phpconpl11.
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Awesome speech, it was a pleasure to listen you.
    The example code that you shou us, was nice and give me many ideas, and help me in my work.
    I never heard of Runkit. Function runkit_function_redefine() is amazing! This knowledge will help me in many difficult situations.
    I hope one day I will have possibility to listen you again.
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Be the first to like this

No Downloads
Views
Total Views
1,220
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
6
Comments
2
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "Testing untestable code - phpconpl11"

  1. 1. Testing untestable code
  2. 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. 3. Testing untestable code No excuse for writing bad code!
  4. 4. Testing untestable code Not to freak out!
  5. 5. Testing untestable code Creativity matters!
  6. 6. Testing untestable code "There is no secret to writing tests, there are only secrets to write testable code!" Miško Hevery
  7. 7. Testing untestable code What is „untestable Code“?
  8. 8. Testing untestable code 1. Wrong object construction “new” is evil! s
  9. 9. Testing untestable code 2. Tight coupling
  10. 10. Testing untestable code No code reuse!
  11. 11. Testing untestable code No isolation → not testable!
  12. 12. Testing untestable code 3. Uncertainty
  13. 13. Testing untestable code "...our test strategy requires us to have more control [...] of the sut." Gerard Meszaros, xUnit Test Patterns: Refactoring Test Code
  14. 14. Testing untestable code In a perfect world... Unittest Unittest SUT SUT
  15. 15. Testing untestable code Legacy code is not perfect... Dependency Dependency Unittest Unittest SUT SUT Dependency Dependency
  16. 16. Testing untestable code ... Legacy code is not perfect... Dependency Dependency Unittest Unittest SUT SUT Dependency Dependency ...
  17. 17. Testing untestable code ... Legacy code is not perfect... Dependency Dependency Unittest Unittest SUT SUT Dependency Dependency ...
  18. 18. Testing untestable code How to get „testable“ code?
  19. 19. Testing untestable code How to get „testable“ code? Refactoring
  20. 20. Testing untestable code "Before you start refactoring, check that you have a solid suite of tests." Martin Fowler, Refactoring
  21. 21. Testing untestable code Hope? - Nope...
  22. 22. Testing untestable code Which path to take?
  23. 23. Testing untestable code Which path to take? Do not change existing code!
  24. 24. Testing untestable code Examples Object Construction External resources Language issues
  25. 25. Testing untestable code Object construction <?php class Car { private $Engine; public function __construct($sEngine) { $this->Engine = Engine::getByType($sEngine); } }
  26. 26. 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);
  27. 27. Testing untestable code Object construction <?php include(Engine.php); class Car { private $Engine; public function __construct($sEngine) { $this->Engine = Engine::getByType($sEngine); } }
  28. 28. 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();
  29. 29. 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; } }
  30. 30. Testing untestable code Object construction – Stream Wrapper stream_wrapper_unregister(file); stream_wrapper_register(file, CustomWrapper);
  31. 31. 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 , Abstract Engine::get , $content ); return $content ; } }
  32. 32. Testing untestable code Object construction – Namespaces <?php class Car { private $Engine; public function __construct($sEngine) { $this->Engine = CarEngine:: getByType($sEngine); } }
  33. 33. Testing untestable code External resources
  34. 34. Testing untestable code External resources Database Webservice Filesystem Mailserver
  35. 35. Testing untestable code External resources – Mock database
  36. 36. Testing untestable code External resources – Mock database Provide own implementation
  37. 37. Testing untestable code External resources – Mock database ZF example: $db = new Custom_Db_Adapter(array()); Zend_Db_Table::setDefaultAdapter($db);
  38. 38. Testing untestable code External resources – Mock database PHPUnit_Extensions_Database_TestCase
  39. 39. Testing untestable code External resources – Mock database Proxy for your SQL Server
  40. 40. Testing untestable code External resources – Mock webservice
  41. 41. Testing untestable code External resources – Mock webservice Provide own implementation
  42. 42. Testing untestable code External resources – Mock webservice Host redirect via /etc/hosts
  43. 43. Testing untestable code External resources – Mock filesystem
  44. 44. 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);
  45. 45. Testing untestable code External resources – Mock Mailserver
  46. 46. Testing untestable code External resources – Mock Mailserver Use fake mail server
  47. 47. 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
  48. 48. Testing untestable code Dealing with language issues
  49. 49. Testing untestable code Dealing with language issues Testing your privates?
  50. 50. 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; }
  51. 51. Testing untestable code Dealing with language issues $myClass = new MyClass(); $reflectionClass = new ReflectionClass(MyClass); $reflectionMethod = $reflectionClass-> getMethod(mydemo); $reflectionMethod->setAccessible(true); $reflectionMethod->invoke($myClass);
  52. 52. Testing untestable code Dealing with language issues Overwrite internal functions?
  53. 53. Testing untestable code Dealing with language issues pecl install runkit-0.9
  54. 54. Testing untestable code Dealing with language issues - Runkit <?php ini_set(runkit.internal_override, 1); runkit_function_redefine(mail,,return true;); ?>
  55. 55. Testing untestable code Dealing with language issues pecl install funcall-0.3.0alpha
  56. 56. 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));
  57. 57. Testing untestable code What else?
  58. 58. Testing untestable code What else? Generative Programming
  59. 59. Testing untestable code Generative Programming Configuration Configuration 1 ... n Implementation Implementation Generator Generator Product components components Product Generator Generator application application
  60. 60. Testing untestable code Generative Programming Configuration Configuration Application Application Implementation Implementation Generator Generator components components Testcases Testcases Generator Generator application application
  61. 61. Testing untestable code Generative Programming A frame is a data structure for representing knowledge.
  62. 62. Testing untestable code A Frame instance <?php class Car { private $Engine; public function __construct($sEngine) { $this->Engine = <!{Factory}!>:: getByType($sEngine); } }
  63. 63. Testing untestable code The Frame controller public class MyFrameController extends SimpleFrameController { public void execute(Frame frame, FeatureConfig config) { if(config.hasFeature("unittest")) { frame.setSlot("Factory", "FactoryMock"); } else { frame.setSlot("Factory", "EngineFactory"); } } }
  64. 64. Testing untestable code Generated result - Testcase <?php class Car { private $Engine; public function __construct($sEngine) { $this->Engine = FactoryMock:: getByType($sEngine); } }
  65. 65. Testing untestable code Generated result - Application <?php class Car { private $Engine; public function __construct($sEngine) { $this->Engine = EngineFactory:: getByType($sEngine); } }
  66. 66. Testing untestable code What is possible?
  67. 67. Testing untestable code What is possible? Show / hide parts of the code
  68. 68. Testing untestable code What is possible? Change content of global vars!
  69. 69. Testing untestable code What is possible? Define pre- or postfixes!
  70. 70. Testing untestable code Is it worth it?
  71. 71. Testing untestable code Conclusions Change your mindset to write testable code!
  72. 72. Testing untestable code Conclusions PHP is a swiss-army knife. Use it that way!
  73. 73. http://joind.in/3922
  74. 74. Testing untestable code Bildquellen http://www.flickr.com/photos/andresrueda/3452940751/ http://www.flickr.com/photos/andresrueda/3455410635/
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×