Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Kevin Ennis:
On Unit Testing
Jul 27, 2015 @ 12:48:31

On Medium.com Kevin Ennis has shared some thoughts on unit testing and how he's "done a 180%" on what kind of value he feels they bring.

There are a lot of really easy ways to rationalize not testing your code, and I’m probably guilty of saying each of them at one point or another. For some engineers, I think the reluctance to embrace unit testing is basically just FUD. Like so many other things, testing seems scary if you haven’t done it before.

But it’s also really difficult to fully understand the benefits of testing unless you’ve worked on a project that has good tests. So it’s easy to see why?—?without fully understanding the upside?—?many developers regard unit testing as an unnecessary step.

He goes through several of the common excuses for not writing unit tests and debunks them one at a time. He also includes a brief section at the end of the post with a recommendation on how to get started testing...essentially "just do it".

tagged: unittest opinion common rationalization fud

Link: https://medium.com/@kevincennis/on-unit-testing-1cc6798f81ee

Giorgio Sironi:
Property-based testing primer
Jun 19, 2015 @ 13:15:29

Giorgio Sironi has a new post to his site today talking about a method (and tool) around a different sort of testing practice: property-based testing. The difference is that, instead of hard-coding values and evaluating them post-processing, you're generating random values and ensuring they match against a set of properties, not values.

I'm a great advocate of automated testing and of finding out your code does not work on your machine, 30 seconds after having written it, instead of in production after it has caused a monetary loss and some repair work to be performed. [...] However, classic test suites written with xUnit and BDD styles have some scaling problems they hit when you want to exercise more than some happy paths. [...] Property-based testing is an approach to testing coming from the functional programming world.

He helps to make the point a bit more clear with an example of testing the "sort" function and its results. He talks about how to test it using normal data, empty data, etc. but notes that this kind of testing can become "boring and error-prone". Instead he proposes the property-based testing of the results. He generates random values to feed into the method and checks to ensure the results are sorting by comparing them to each other. He expands this with a bit more complex example, showing how to test some DateTime handling and evaluating the result with logic in a closure. To help make this kind of testing easier, he's created a library (Eris) that extends PHPUnit and provides the methods seen in his examples.

tagged: property testing unittest phpunit extension random datetime sort eris

Link: http://www.giorgiosironi.com/2015/06/property-based-testing-primer.html

PHPUnit 4.7 and Three Shades of Green
Jun 08, 2015 @ 13:57:25

Sebastian Bergmann has posted a guide to PHPUnit 4.7 and some of the changes/new features it introduces.

PHPUnit 4.7 introduces a couple of small improvements. For instance, PHPUnit's PHPT test runner now supports --INI-- sections, information about the PHP runtime used is now printed in verbose mode, and a warning is now printed when code coverage data is collected but no whitelist is configured.

He also talks about the support that's been added improving the output of the HTML version of the code coverage reports, showing different colors based on how well covered the lines are. He also briefly looks ahead to PHPUnit 5, the versions it will support and the plans for release.

tagged: phpunit unittest v47 small medium large coverage shade phpunit5

Link: https://thephp.cc/news/2015/06/phpunit-4-7-and-three-shades-of-green

Nate Krantz:
How I'm Writing Unit / Functional Tests
May 22, 2015 @ 11:50:42

In a recent post Nate Krantz has shared some of his own methods around writing functional and unit tests.

So...testing. That thing that everyone says is so important but you don't really learn about it in school. I've had some trials and tribulations with testing so I'm going to just dump out some thoughts here.

He starts with a bit of background on his own experiences in development and how he finally decided that testing would "solve everything". He started with unit tests (for a CodeIgniter application) and how he got them up and running. He talks about issues he found around dependencies (and static methods) and how he made use of mocks to reduce some of the issues with dynamic loading, at least how CodeIgniter does it. Unfortunately, this didn't work out as planned so he fell back to a test database and create more effective and simpler functional tests. Code examples are sprinkled through out the post to show how he was trying to solve the problem at different points in the process.

tagged: unittest functionaltest opinion experience codeigniter

Link: http://www.natekrantz.com/why-test-driven-development-rocks-sucks/

Theo Kouzelis:
Improving Readability of PHPUnit Data Providers
Apr 10, 2015 @ 13:01:28

Theo Kouzelis has a recent tutorial posted showing you how to make your PHPUnit tests a bit cleaner with the help of data providers, a built-in tool for PHPUnit that allows for easier validation of larger datasets.

When writing tests I try to describe the test in the function name. I use the format testDoesSomethingWhenPassedSomething to first describe the assertion and then the context. [...] When I have many data sets running against the same test I will use the frameworks data providers to make the code less verbose.

He includes code examples to show both the difference between the single data tests and one using a set of email addresses to validate their correctness. He notes that the error message using data providers can be confusing (and maybe hide the real problem) so he offers a solution to make it more readable: associative arrays. The trick is that PHPUnit uses the key to display the error and by making this unique you can make the error output more informative.

tagged: dataprovider tutorial readability phpunit unittest associative array

Link: http://theo.codes/php/improving-readability-of-phpunit-data-providers.html

Cody Kennedy-Darby:
Testing Your Current Code Against PHP7 Or HHVM
Apr 01, 2015 @ 09:49:11

On Medium.com Cory Kennedy-Darby has a quick post showing you how you can test your current code against the latest versions of PHP 7 with the help of DUnit.

DUnit (dee-unit) makes your life easier by allowing you to run your unit tests on different versions of PHP or HHVM. Different versions are possible by using Docker containers. Thanks to @danbruce each of the Docker containers are only, ~35 MB in size. [...] PHP7 isn’t that far away. In fact, it is scheduled for release in ~8 months in November. Now is the perfect time to start testing your code against PHP7 nightly.

He starts with a few reasons you might want to test your code and things you can do to start "thinking forward" to when it is released. He then shows you how to install DUnit (more detail here) and use it to test on both PHP 7 and HHVM builds.

tagged: nightly test unittest build dunit docker container php7 hhvm

Link: https://medium.com/@ckdarby/testing-your-current-code-against-php7-or-hhvm-2f0ab059af78

Freek Lijten:
Testing and improving PHP extensions for PHP 7
Mar 13, 2015 @ 11:02:47

In his latest post Freek Lijten talks about PHP extensions, the upcoming PHP version - well, PHP7 - and the things that can be (and are being) done to help improve and prepare the extension ecosystem. In his post he walks you through the process of getting a PHP7 install set up, a sample extension set up and writing some tests to help improve it.

PHP7 is coming. And it is coming to a neighbourhood near you :) A couple of people started an initiative to ensure extensions will be running out of the box once PHP7 hits the shelves. The fun part: You can help too! No C knowledge is necessary (although it is fun to dive into PHP's internals!). This piece is a short intro to help you help PHP! Help triaging extensions, write tests, add documentation and who knows when you'll be diving into C code.

He's encouraging this work as a part of the recently launched GoPHP7 - Extensions initiative launched a while back. He starts by helping you get PHP7 installed (from source, compiled). Once that's installed and working, he helps you get an extension up and running, in this case the enchant extension. He shows you how to run the tests for the extension and how to write some tests to contribute back to the project. He includes instructions for generating code coverage reports, walks you through some sample code and a link to a page with more information if you get stuck.

tagged: testing improving extension php7 version phpt unittest coverage gophp7

Link: http://www.freeklijten.nl/home/2015/03/12/Testing-and-improving-PHP-extensions-for-PHP-7

Jani Hartikainen:
What’s the difference between Unit Testing, TDD and BDD?
Mar 02, 2015 @ 10:16:28

On his site today Jani Hartikainen has a new post helping to demystify some potential confusion around unit testing, test-driven development and behavior-driven development. He's talking about them in the context of Javascript in this case, but the fundamentals transfer to just about any other language, including PHP.

When you’re just getting started with automating your JavaScript testing, there’s a lot of questions. You’ll probably see people talk about unit testing, TDD or Test-Driven Development, and BDD or Behavior-Driven Development. But which one of them is the best approach? Can you use all of them? I’ve talked to a number of JavaScript developers, and there seems to be some confusion about all this. So, let’s take a look at Unit testing, TDD and BDD, and fix some of the common misconceptions about them out there.

For each he provides an overview of this basic concepts and a bit of sample code showing it in action. For TDD (test-driven development) there's not really a way to show it specifically in code as it's more of a practice. Instead he gives a "checklist" to follow when practicing it.

Unit Testing gives you the what. Test-Driven Development gives you the when. Behavior Driven-Development gives you the how. Although you can use each individually, you should combine them for best results as they complement each other very nicely.
tagged: bdd unittest tdd testdrive behaviordriven development introduction

Link: http://codeutopia.net/blog/2015/03/01/unit-testing-tdd-and-bdd/

Snack Overflow:
Unit testing static calls without refactoring the world in php
Feb 27, 2015 @ 12:55:06

The "Snack Overflow" blog (from tech.graze.com) has a recent post sharing some suggestions to help unit test static calls without having to "refactor the world" away from them.

Imagine you have a situation [using a static method call] in some legacy code. Currently we can't unit test this as we can't mock out the doSomethingElse() call. So what do we do? Well we have two options really [...] neither of which is very appealing. [...] There is, however, a third option that gains us the ability to unit test Foo without having to touch Bar at all.

This option involves creating a "proxy" object of the "Bar" class that's non-static and only returns the result of the previous class' static method. You can then correctly mock that class and return the result in a more self-contained way. He lists a few caveats with this method including the fact that it could lead to a lot of proxy objects if there are a lot of static methods to replicate.

tagged: unittest static method refactor proxy object mock tutorial

Link: http://tech.graze.com/2015/02/26/unit-testing-static-calls-without-refactoring-the-world-in-php/

Stephan Hochdörfer:
Running PHPUnit via Phing on HHVM
Feb 26, 2015 @ 10:16:58

Stephan Hochdörfer has a quick post showing how he has PHPunit up and working on an HHVM instance. His problem was that the tests were actually executing using the "php" binary, not the HHVM one.

For quite some time we run the unit tests for our libs and tools against PHP and HHVM, at least that is what I thought up to now. As it turns out I missed a minor detail. [...] What happens now is that Phing is executed via HHVM but PHPUnit will still be executed via the PHP binary because the PHPUnit shell script will look for the php binary in the PATH configuration. Since we run HHVM side-by-side with PHP on our Jenkins build nodes I was not able to point /usr/bin/php to /usr/bin/hhvm - which would be the easiest and cleanest solution. I

He shares the workaround he created, creating a symbolic link between the hhvm and php binaries and then executing the Phing task to run the tests. This is being run via Jenkins and uses it's "WORKSPACE" as a container so the main "php" binary isn't overwritten.

tagged: hhvm phpunit test unittest execute binary path jenkins phing task

Link: https://blog.bitexpert.de/blog/running-phpunit-via-phing-on-hhvm/