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

Remi Collet:
PHPUnit code coverage benchmark
Nov 09, 2015 @ 11:57:33

Remi Collet has a quick review of some of the performance results from running the Composer PHPUnit tests on PHP 5 versus PHP 7.

As already said numerous time, PHP 7 is faster than PHP 5. Since PHPUnit 4.8 you can choose between XDebug and phpdbg as driver to retrieve code coverage data, see PHPUnit 4.8: Code Coverage Support.

Here is some benchmark results. All the tests are run using PHPUnit 5.0.8, PHP 5.6.15 as SCL or PHP 7.0.0RC6 as SCL and XDebug 2.4.0beta1 (freshly released, with some additional patches) for the composer test suite.

He shows the results in execution time and memory used for PHP 5 versus PHP 7 versions, both with and without code coverage being generated. He also includes two examples of running the tests with PHP 7, once using the XDebug debugger and one using phpdbg.

tagged: remicollet phpunit composer coverage benchmark php5 php7

Link: http://blog.remirepo.net/post/2015/11/09/PHPUnit-code-coverage-benchmark

Ken Guest:
Scan your code for old-style constructors using PHPUnit
Nov 06, 2015 @ 11:53:26

Ken Guest has a quick post on his site with a helpful hint for those updating older codebases. You can use PHPUnit & PHP_CodeSniffer to locate old constructors in the PHP4 format (constructors named after the classes).

There are less than seven days left until PHP 7 is released, which drops support for old-style constructors – the ones where a method is a constructor if it shares the same name as the class. You don’t want to spend too much time scrolling through codebases for that though do you? Better things to do, like watch videos of conference talks you’ve missed and such. Well, you’re in luck. If you use php_codesniffer (and if you don’t, well shame on you), you’ll be able to get a report of old-style constructors fairly quickly.

He includes examples of the commands you'll need to use to sniff out these older constructors, making use of the built-in "Squiz" coding standard and the "Generic.NamingConventions.ConstructorName" sniff but only on PHP files. He also shows how to alias it to a bash command and export the results to a CSV file.

tagged: scan code legacy constructor php4 php7 phpunit phpcodesniffer

Link: https://kenguest.wordpress.com/2015/11/06/scan-your-code-for-old-style-constructors-using-phpunit/

Matt Stauffer:
Creating custom @requires annotations for PHPUnit
Oct 28, 2015 @ 10:06:46

In this post to his site Matt Stauffer walks you through how he created a custom @requires annotation to use in his PHPUnit testing. He needed a way to tell a test to only run if it wasn't being executed on the Travis CI service.

I was working on a project this weekend that required skipping certain tests in a particular environment (Travis CI). [...] I remembered that there was a @requires annotation in PHPUnit that works natively to allow you to skip a test under a certain version of PHP or with certain extensions disabled, so I set out to write my own custom @requires block.

He links to an article that helped him get most of the functionality in place but decided to restructure it a bit to make the override of the checkRequirements method a bit clearer. He ends up using the Laravel Collection functionality instead of a basic foreach reducing it down to a closure that looks for an environment variable called TRAVIS and automatically mark the test as skipped.

tagged: requires annotation custom phpunit travisci skip environment variable closure

Link: https://mattstauffer.co/blog/creating-custom-requires-annotations-for-phpunit

Matthew Turland:
PHPUnit + XHProf = BOOM!
Oct 14, 2015 @ 09:38:22

Matthew Turland has a post to his site sharing his experience with the PHPUnit and XHProf combination...and the unfortunate result that made every test fail.

I ran into an issue recently while trying to run PHPUnit tests in an environment using XHProf. Google didn’t prove to be much help, so I thought I’d document the problem and solution here for posterity.

When I ran my tests, each failed with the same cryptic error and no backtrace: "Attempted to serialize unserializable builtin class PDO" The cause was the culmination of two rather unfortunate circumstances.

He shares the two problems that causes this issue - one being XHProf's use of globals (where its PDO connection is stored) and the other is the @backupGlobals setting in PHPUnit that's enabled by default. This makes PHPUnit to try to backup that PDO connection by serializing it but can't, hence the failure. He points out a pull request that aims to fix the issue but recommends disabling the globals backup for the time being if you don't have a need for it.

tagged: phpunit xhprof combine globals pdo error backup

Link: http://matthewturland.com/2015/10/13/phpunit-xhprof-boom/

Scott Keck-Warren:
Making dataProviders More Maintainable
Sep 30, 2015 @ 09:44:18

Scott Keck-Warren has a quick post to his site sharing a method for keeping data providers maintainable in your unit tests. Data providers are a quick way to retest the same logic with several different types of data and not have an individual test for each.

I’m a big fan of using PHPUnit’s data providers feature because it allows you to easily run a lot of data through the same kinds of tests over and over again without having a bunch of duplicate code sitting around. But they aren’t always the easiest thing to come back to an understand.

He briefly introduces how data providers are used in PHPUnit testing, including a brief code example. The errors that can come up with this common setup can be cryptic to debug. He recommends a slight alteration to the data provider return structure to use an associative array instead of a single-level array. This way, if there's an error the resulting message refers to the index, not just a number making a bit more sense and aids in debugging.

tagged: dataprovider maintainable phpunit tip associative array

Link: http://www.thisprogrammingthing.com/2015/making-dataproviders-more-maintainable/

SitePoint PHP Blog:
Using the Selenium Web Driver API with PHPUnit
Aug 24, 2015 @ 12:54:56

The SitePoint PHP blog has posted a tutorial showing you how to use the Selenium web driver API from inside of your PHPUnit tests. Selenium is an automation tool that makes testing frontends of applications simpler.

Previously, we demonstrated using Selenium with PHPUnit and used a user subscription form example throughout the article. In this one, we are going to explore Facebook’s webdriver package for emulating a browser. PHPUnit partially supports the Selenium WebDriver API and the work is still in progress. One of the most popular WebDriver API implementations is the Facebook/webdriver package. We will try to accomplish the same validation tests from the previous article using this package.

They help you get the Facebook package installed (via Composer) and create a first simple test class. They create an instance of the RemoteWebDriver object and point it at their local application. A test is then created to navigate to a form, fill in a bit of data and submit it. The results are then checked for a string ("Everything is Good!") to pass the test. He also shows how to have the driver wait for an element to load, possibly one that uses an AJAX request. The post finishes off with a look at some of the other interaction methods (drag and drop, handling popups) and how to run the tests in a "headless" mode not requiring a browser to execute through your system.

tagged: selenium phpunit webdriver api tutorial frontend testing

Link: http://www.sitepoint.com/using-the-selenium-web-driver-api-with-phpunit/

PHPUnit 4.8: Code Coverage Support
Aug 10, 2015 @ 08:48:52

Sebastian Bergmann has posted about some updates in the latest version of the PHPUnit PHP unit testing tool (v4.8) and changes in code coverage handling.

PHPUnit 4.8 introduces a couple of small improvements. For instance, the @testWith annotation was added as "syntactic sugar" for defining data providers and the --no-coverage commandline option was added for ignoring any code coverage configuration from the configuration file.

PHP_CodeCoverage, the library used by PHPUnit to collect, process, and report code coverage information, has been updated for PHPUnit 4.8. It can now collect code coverage information on PHP 7 without the need for a third-party extension such as Xdebug. And PHP_CodeCoverage's HHVM driver has been updated to reflect changes to how HHVM provides code coverage information.

He gets into a bit more detail about the updates to both the PHP 7 handling and changes for HHVM, including how to call it for PHP 7 (phpdbg vs just the normal CLI) and how HHVM no exposes its coverage information publicly. This makes it simpler for PHPUnit to grab the data without special handling code. He also looks ahead to PHPUnit 5, scheduled for release in October 2015, and that some functionality will be deprecated when the version is changed.

tagged: phpunit codecoverage support php7 hhvm changes phpdbg public

Link: https://thephp.cc/news/2015/08/phpunit-4-8-code-coverage-support

Semaphore CI Blog:
Getting Started with BDD in Laravel
Aug 05, 2015 @ 09:17:43

Bruno Skvorc has written up a tutorial on the Semaphore-CI blog showing you how to get started with behavior-driven development in Laravel applications. He makes use of the Behat and PHPSpec libraries to write and execute the tests.

For many developers BDD is a complicated subject, and getting started with it the right way often does not come easy - especially when you need to implement it into an existing framework. This tutorial aims to help you get a BDD-powered Laravel project up and running in very little time, introducing you to the basic concepts and workflow you'll need to proceed on your own. We'll be installing and using Behat and PhpSpec.

He walks you through the process of getting everything you need installed: a simple Laravel application and Behat (also requiring a bit of setup to make it "play nice" with Laravel). He initializes the Behat directory and explains the concept of "context" and how to configure your Behat installation. He then gets into writing the features, creating a basic test that checks the main page of the Laravel application for the phrase "Laravel 5". A bit of additional PHP code is required to make the tests work (included) and the result is a passing test, executed with just a behat command.

The second half of the article is about PHPSpec, showing how it can be used as a sort of replacement for PHPUnit with a bit more readable syntax. He shows how to write a simple test against an object. Finally, he shows how to combine the powers of Behat and PHPSpec into a single method of testing, using PHPSpec behind the scenes in the Behat context to help with testing assertions.

tagged: phpunit phpspec testing behaviordriven behat bdd introduction tutorial

Link: https://semaphoreci.com/community/tutorials/getting-started-with-bdd-in-laravel

Younes Rafie:
Using Selenium with PHPUnit
Jul 30, 2015 @ 09:51:20

In this tutorial posted to the SitePoint PHP blog Younes Rafie shows you how to combine Selenium with PHPUnit to do acceptance testing on your application. Where PHPUnit and unit testing is more about testing the "pieces" of your application, acceptance testing it more about checking the interface and functionality for correctness according to requirements.

Testing is a really wide subject, whether it be unit testing, functional testing, acceptance testing, etc. In this article, we’re going to see how you can do acceptance testing using Selenium. I will use a practical example to illustrate a real use case. I will assume that you already know how to do unit testing using PHPUnit, or that you at least have a grasp of what it’s all about.

He starts with a few definitions around what acceptance testing is and how Selenium can help in performing these evaluations. He uses a simple user registration page for his tests and includes commands to get PHPUnit+Selenium installed as well as an alias to start the Selenium server. He then creates a first test case, setting up the Selenium "browser" to use in later requests. He then gets to testing the page itself, setting up some data providers for the content to inject into the form both valid and invalid. He shows how to work with the DOM inside of your tests and making both a valid and invalid form submission. He also shows how to use a different browser (he defaults to Firefox) and how to check of the current document is ready for evaluation, that is if all Javascript has fired and finished.

tagged: selenium phpunit acceptance testing unittest tutorial

Link: http://www.sitepoint.com/using-selenium-with-phpunit/

Giorgio Sironi:
Property-based testing primer
Jun 19, 2015 @ 12: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