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

Writing Functional Tests for WP-CLI Packages
Jan 05, 2017 @ 12:57:25

On the Delicious Brains blog there's a post sharing some of their knowledge about building tests for WP-CLI packages, a set of command line tools for administering a WordPress installation. Their testing makes use of the Behat testing tool (already in use on WP-CLI's own tests).

My last article was part of a short series on automating local WordPress site setup. In that series, we created a WP-CLI package that helps with installing and uninstalling WordPress development environments, and we even got it submitted to the WP-CLI Package Index.

[...] In this post we’re going to take a bit of a break from automating WordPress installs and start writing some functional tests to make sure that everything works as expected. While I’ll be writing the tests for the wp installer command, the same concepts should apply for any WP-CLI package.

They start by clarifying the difference between functional and unit tests and how to get your environment all set up and ready to use for testing. They help you get the wp_scaffold_package installed and how to confirm that everything is working as expected. From there it's all about the tests: ensuring that a package is active, creating a custom step to use in testing and an example of what the output should look like.

tagged: functional test wordpress wpcli package behat tutorial

Link: https://deliciousbrains.com/writing-functional-tests-wp-cli-packages/

Laravel News:
Run PHPUnit Tests From Sublime Text
Dec 29, 2016 @ 09:30:48

On the Laravel News site they've posted a guide showing you how to setup and run PHPUnit tests from Sublime Text, one of the more popular editors for code development.

Sublime Text is a great editor. It’s lightweight, fast, and extremely customizable. However, one downside to it compared to a full blown IDE is it doesn’t come with support for running your PHPUnit tests directly from the test class you are working with.

To solve this problem, Adam Wathan created and released a free package named Sublime PHPUnit that allows you to run your tests from a keyboard shortcut. Let’s take a look at how to add this package to your arsenal.

The post walks you through the installation of the tool (manually cloning the repository) and how to then use it via Sublime's command palette. There's also some instruction on customizing the plugin's setup and allowing for shortcut keystrokes bound to events the plugin provides. The final tip helps you change the tool used to run the tests (the Terminal app by default) over to something like ITerm.

tagged: phpunit test unittest sublimetext editor plugin keystroke run

Link: https://laravel-news.com/sublime-phpunit

Joe Ferguson:
Mocking Swift Mailer – 3 Steps to better code
Dec 22, 2016 @ 10:46:43

If you're a user of the SwiftMailer package in your application, you may have hit on issues with your unit tests with mocking the library to isolate it from the actual email sending. Joe Ferguson is here to help with some advice in his latest post on mocking the library and how he used it to solve a problem in his own code.

A few days ago an issue came across my Jira queue that mentioned odd characters showing up in the subject line of our notification system. We use SwiftMailer library which is a fantastic library for sending mail.

[...] Something was taking our twig template that we use for our email subject and changing the encoding. This is normally done when you want to convert special characters so they’re not interpreted as code blocks. The example above is showing where an apostrophe is converted to “'”, the ASCII code equivalent.

He breaks the rest of the post into 3 steps (well, really 4 but there's a 2.5 in there) that he followed to mock the library out appropriately and be able to test the message sending without actually having to send. Code examples are included of both the code doing the sending and the test doing the mocking and verifying the subject lines on their emails (the original bug reported).

tagged: mocking unittest swiftmailer package test tutorial

Link: https://www.joeferguson.me/mocking-swift-mailer-3-steps-to-better-code/

PHP 7.1.0 Release Candidate 5 Released
Oct 27, 2016 @ 10:25:04

The main PHP.net page has posted an announcement about the latest Release Candidate in the PHP 7.1.x series being tagged and released: PHP 7.1.0 Release Candidate 5:

The PHP development team announces the immediate availability of PHP 7.1.0 Release Candidate 5. This release is the fifth release candidate for 7.1.0. All users of PHP are encouraged to test this version carefully, and report any bugs and incompatibilities in the bug tracking system.

For more information on the new features and other changes, you can read the NEWS file, or the UPGRADING file for a complete list of upgrading notes. These files can also be found in the release archive.

As a reminder, this is a release candidate and is not to be used in production. You can download and test out this latest release from the PHP.net source QA site or the Windows QA site for the binaries. The next release candidate for this version will be released on November 10th with a goal of a final release following that.

tagged: php71 release candidate release preview test php71rc5

Link: http://php.net/index.php#id2016-10-27-1

PHP 7.1.0 Release Candidate 3 Released
Sep 30, 2016 @ 11:47:01

The php.net site has posted an announcement about the latest Release Candidate for the PHP 7.1.x series: PHP 7.1.0 RC 3:

The PHP development team announces the immediate availability of PHP 7.1.0 Release Candidate 3. This release is the third release candidate for 7.1.0. All users of PHP are encouraged to test this version carefully, and report any bugs and incompatibilities in the bug tracking system.

[...] For more information on the new features and other changes, you can read the NEWS file, or the UPGRADING file for a complete list of upgrading notes. These files can also be found in the release archive.

As a reminder, this is a preview release and is NOT designed to be used in production . The release schedule is provided on the wiki, the next Release Candidate will be coming in early October (the 10th) with three more following. The target release date for PHP 7.1.0 is sometime in the later part of November.

tagged: php71 release candidate release preview test

Link: http://php.net/index.php#id2016-09-29-1

Master Zend Framework:
How to Build a Docker Test Environment
Sep 28, 2016 @ 11:20:40

The Master Zend Framework site continues their series covering the creation of a Docker-based testing environment in this second part highlighting the addition of testing support.

In the first part in this series on developing web applications using Docker, we saw how to create a local development environment using Docker; one ideally suited to creating Zend Expressive (or any other kind of PHP-based web application). But, what we didn’t cover was how to handle testing in a Docker-based environment.

[...] How do you run tests when working with Docker containers? After a bit of searching, I found that it’s not that difficult. But you have to use the right combination of commands.

Since unit tests can be run locally if need be (they shouldn't need any resources from the service if they're true unit tests) he focuses on acceptance testing. For his examples he uses the Codeception testing tool. He walks you through the setup of some simple tests based on the "home" page functionality of the Zend Expressive skeleton application. With that in place, he shows the updates that will need to be made to execute the tests from outside the instance via a "docker exec" call. The post finishes with a look at adding two other tools to the mix as well: Make and Phing.

tagged: docker test environment series part2 testing acceptance codeception make phing

Link: http://www.masterzendframework.com/how-to-build-a-docker-test-environment/

SitePoint PHP Blog:
Quick Intro: PhpCompatibility for PHPCS – Are You PHP7 Ready?
Sep 27, 2016 @ 11:13:17

The SitePoint PHP blog has a quick tutorial posted helping you get your application PHP 7 ready with the help of the PhpCompatibility "sniffs" for the widely used PHP_CodeSniffer tool.

Sooner or later, there will come a time when you will need to migrate your projects to different PHP versions. How will you check if you’re compatible with a PHP version different to the one you’ve been developing on?

One possibility is always to install the version of PHP we want to migrate to, run php -l or something like PHPSA to check for syntax errors, check the PHP documentation for known issues with the migration and hope for the best. Or, we can use some available third party tools to check for PHP version compatibility in our projects.

The article then introduces the PHPCompatibility set of sniffs for PHP_CodeSniffer and installing them with a "git clone" in the right Standards directory. Also included are some basics for using PHP_CodeSniffer (like the command line options) and an example of some of the output from the compatibility check. The post wraps up with a real-life example using the PHPMailer codebase and testing it for PHP 5.6 readiness.

tagged: php7 codesniffer compatibility test codebase sniff

Link: https://www.sitepoint.com/quick-intro-phpcompatibility-standard-for-phpcs-are-you-php7-ready/

QaFoo Blog:
Introduction To Page Objects
Sep 06, 2016 @ 11:03:17

The QaFoo blog has a post to their blog introducing page objects and how they're useful in functional testing to help provide a "decoupling" from the actual frontend.

A while ago we wrote about writing acceptance tests (end-to-end tests) with Mink and PHPUnit. While this is a great set of tools for various applications such tests tend be susceptible to changes in the frontend. And the way they break is often hard to debug, too. Today I will introduce you to Page Objects which can solve some of these problems.

The basic idea behind a Page Object is that you get an object oriented representation of your website. The Page Objects maps the HTML (or JSON) to an object oriented structure you can interact with and assert on. This is more initial work then than writing tests with PHPUnit and Mink directly, but it can be worth the effort.

They use the Mnk testing tool to simulate a browser and some previously shared functionality to lay the foundation. From there they write up a first test using a "Login" page object and processing the username/password handling of the page. They show how to implement a custom page object with a bit of additional logic and put it to use in processing the request. They also include an update when, for example, a site is switched from Twig templates to a React.js component and how the Page object would need to be refactored for the update.

tagged: page object functional test mink behat example tutorial

Link: https://qafoo.com/blog/089_introduction_to_page_objects.html

Dries Vints:
Two tips to speedup your Laravel tests
Aug 25, 2016 @ 09:15:48

In this recent post to his site Dries Vints shares two quick tips you can use to help speed up the execution of the tests for your Laravel application.

I've seen two different tips for speeding up your tests in Laravel in the past week and thought I'd share them with you. For me, they made a significant impact on the speed of my tests.

His two tips involve lowering the "cost" factor on the number of "rounds" the user password is hashed and the use of a pre-computed hash in your testing factories. These both help reduce the overhead needed, especially when working with tests that need to create the user every time. He includes code and reference links for more information about these two tips and applying them in your testing.

tagged: speed performance laravel test hashing rounds precomputed

Link: https://driesvints.com/blog/two-tips-to-speedup-your-laravel-tests/

Chema Garrido:
Speed test PHP vs Lumen vs Laravel
Aug 18, 2016 @ 12:08:53

Chema Garrido has written up a post sharing some results of a performance test (speed) between Lumen and Laravel also comparing it against Kohana and straight PHP.

I am working in the new EmailValidator!, and after developing the EU VAT API, I feel confident to develop it on Laravel Framework. But before we start… let’s test the speed of the stack.

I used my local computer a 8 cores i7 2ghz 8GB ram 512SSD. Apache2, PHP 7.0.8. Tested this test with siege 5 times for each and retrieved the highest.

The first part of the post shows the results in a tabular format but following this is the more detailed version, complete with the siege command executed and the code used. The results are interesting but seem to mostly fall into the real of micro-optimization as there's really not that much difference between the results (though the "Longest transaction" on the plain PHP code is an oddity).

tagged: laravel lumen performance speed test results framework

Link: https://chema.ga/speed-test-php-vs-lumen-vs-laravel/