How (Not) to Write Testable Code
This repository contains example code for the according talk at WordCamp Nuremberg, 2016.
Unit Test Examples
The first part of the talk is about unit testing. After a short summary, you can see several unit test examples. This repository includes the complete and fully documented versions of the presented PHP unit tests as well as the respective JavaScript unit tests.
Requirements
The PHP unit tests require PHP 5.4+, and PHPUnit, Mockery, Brain Monkey and WP_Mock. If you have Composer installed, just execute $ composer install
, and you're good to go.
The JavaScript unit tests require tape and Sinon.JS. With npm installed, $ npm install
gets you started already.
Bad Practices
The second and main part of the talk then is about code examples that are either hard to test, or not testable at all. As with the unit test examples, this repository also includes the bad practice PHP code and JavaScript code examples.
Since only presenting bad (i.e., hard-to-test) code doesn't do any good, this repository also includes improved versions of the code. For each of the code examples, you can find an according pull request. By means of the diff view, you can easily compare the original and the improved code.
Feel free to fork this repository, and work on possible tests, if you like.