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

Gundars Meness:
Unit Testing - The Big Picture
Apr 24, 2017 @ 11:56:50

If you're still new to the world of testing in your applications, you should give this new guide from Gundars Meness a read. He's done a great job of providing "the big picture" of testing - why to do it and what it is.

This is not a crash course of what characters one needs to type in his code editor to produce unit tests. This is fuel the brain requires before attempting such actions.

The subject of Unit Testing is not as simple as one might think. Many of us, developers, go into unit testing based on pressure from clients, peers, colleagues, our heroes and so on. We quickly learn the value of it, and, once the tech setup is done, there is a tendency to forget the big picture, if it was ever learnt. This article will provide a short insight into what is and isn’t unit testing in PHP and in general, and unit test place in the quality assurance realm.

He then breaks up the article into a few different sections, each with a paragraph or two, covering different testing related topics:

  • What is testing?
  • What really is testing?
  • What is Unit Testing?
  • How to write a Unit test?

There's some great suggestions in here, for both those new to testing and those still trying to figure out some good practices.

tagged: unittest introduction bigpicture testing

Link: http://gundars.me/php/unit-testing-php-big-picture/

North Meets South Podcast:
WordPress, UUIDs, and testing revelations
Apr 11, 2017 @ 13:47:15

The North Meet South podcast, with hosts Jacob Bennett and Michael Dyrynda, has posted their latest episode - Episode #25: WordPress, UUIDs, and testing revelations.

This episode, Jake and Michael speak about working with UUIDs in Laravel, Wordpress, and testing mindsets.

Other topics mentioned in this episode include the book "Building APIs You Won't Hate" (by Phil Sturgeon), and article about Advanced Custom Fields and Australia's Science Channel. You can listen to this latest episode either using the in-page audio player or by downloading the mp3 directly. If you enjoy the show, be sure to subscribe to their feed and follow them on Twitter for updates when new shows are released.

tagged: northmeetssouth podcast ep25 wordpress uuid testing jacobbennett michaeldyrynda

Link: http://www.northmeetssouth.audio/25

Laravel News:
Introduction to Seeding Data in Testing
Apr 10, 2017 @ 10:42:26

On the Laravel News site they've posted an introduction to seeding data in testing to help make your Laravel application testing easier and see "more correct" results.

Since seeding was released in Laravel 5.1, testing has become easier and quicker.

You can have ten users with each having a post or 1000 users with one or more posts inserted before the testing begins. In this tutorial, you will create a test case to test the user model and a seeder to seed ten users, and each is following one user into the database.

The tutorial starts with a migration to create a "users" table including a "follow user ID" field that tracks which user another is following. Next up is the creation of the User model with the methods to create the "follow" links between users. The make:seeder command is then used with this model to generate the seeder class and make 10 users with faked information. The db:seed command is used to execute the seeder and populate the data. Finally an example test case is created, first just testing that 10 users were created then refactored to test links between the users and the follow/unfollow functionality.

tagged: laravel seed data testing migration tutorial unittest model

Link: https://laravel-news.com/seeding-data-testing

A Package for Snapshot Testing in PHPUnit
Mar 29, 2017 @ 12:05:03

In this post on Medium.com Sebastian De Deyne shares a package from Spatie that makes it simpler to perform "snapshot testing" in your PHP applications.

The gist of snapshot testing is asserting that a set of data hasn’t changed compared to a previous version, which is a snapshot of the data, to prevent regressions. The difference between a classic assertEquals and an assertMatchesSnapshot is that you don't write the expectation yourself when snapshot testing. When a snapshot assertion happens for the first time, it creates a snapshot file with the actual output, and marks the test as incomplete. Every subsequent run will compare the output with the existing snapshot file to check for regressions.

Snapshot testing is most useful larger datasets that can change over time, like serializing an object for an XML export or a JSON API endpoint.

Our package, which exposes a trait to add snapshot testing capabilities to your tests, can be installed via composer and is available on GitHub.

He starts with a basic example, checking to verify that a string hasn't changed between test runs. The first run creates the snapshot (marking the test as incomplete) and then following runs verify - one passing (no change) and one failing (changing of the return string). He also includes examples of tests on objects, mentions where the snapshot files are stored and how to add drivers for additional snapshot storage types.

tagged: snapshot testing phpunit package tutorial

Link: https://medium.com/@sebdedeyne/a-package-for-snapshot-testing-in-phpunit-2e4558c07fe3

Raphael Stolt:
Keeping your CLI integration tests green on Windows
Mar 27, 2017 @ 10:55:08

Raphael Stolt has a new post to his site for the PHP on Windows users out there helping you to keep your integration tests green when little things (like line ending differences) can cause issues between the Windows and unix-based platforms.

Lately on a Windows system, some failing integration tests for CLI commands utilising the Symfony Console component caused me some blip headaches by PHPUnit insisting that two strings are not identical due to different line endings. The following post documents the small steps I took to overcome these headaches.

He starts with the error message that the failing test was giving, a slightly unhelpful response with the "actual" and "expected" values looking basically the same. He suspected it to be something different with the encoding or line endings (a common issue between Windows and Linux platforms). To confirm his suspicions he looked deeper into the Symfony Console code and, on locating the difference, extended the "CommandTester" class to automatically disable the "decorated" setting across all tests.

tagged: phpunit testing integration green windows platform decorate tutorial

Link: http://raphaelstolt.blogspot.com/2017/03/keeping-your-cli-integration-tests.html

Mark Baker:
Closures, Anonymous Classes and an alternative approach to Test Mocking (Part 1)
Mar 06, 2017 @ 11:12:04

On his site Mark Baker has posted the first part of a series of articles covering the use of closures and anonymous classes in testing and mocking. In this first part of the series he focuses on introducing some of the basics of the topics to be covered and what the closures/anonymous classes can replace.

Since their first introduction with PHP 5.3, Closures have proven an incredibly useful feature for writing simple callback code, making it cleaner and more intuitive. Anonymous Functions can be used inline for many of the array functions or assigned to a variable as a Lambda that can be referenced many times in different places in your code.

[...] But this isn’t an article about the differences between Anonymous and Lambda Functions and Closures [...] Instead, I want to take a look at binding Closures to objects as a first step to demonstrating an alternative approach to test mocking.

He goes on to talk about the mocking the PHPUnit already includes, other libraries that help with mocking/stubs but then pushing those off for the focus of the article - the use of the closures/anonymous classes. He gets into some details about how PHP handles closures internally and how to bind a closure to a class or object instance (via the bindTo function). He then attaches this to an object and shows how to create a "snooper" to work with an object, perform some processing and return some values from it.

tagged: closure anonymous function tutorial testing unittest phpunit snooper

Link: https://markbakeruk.net/2017/03/05/closures-anonymous-classes-test-mocking-1/

SitePoint PHP Blog:
Laravel Dusk – Intuitive and Easy Browser Testing for All!
Feb 23, 2017 @ 12:54:06

On the SitePoint PHP blog there's a tutorial posted that introduces you to Laravel Dusk, a browser-based testing tool, and how it can be used to test a Laravel-based application.

End to end testing for JavaScript applications, particularly single-page-apps, has always been a challenge. To that end, Laravel released its 5.4 version recently with a new testing library: Dusk.

With the release of Dusk, Laravel hopes to give its users a common API for browser testing. It ships with the default ChromeDriver, and if we need support for other browsers, we can use Selenium. It will still have this common testing API to cater to our needs.

The tutorial then walks you through the installation process and two approaches to getting it integrated into your application. They then create a first test, checking to see if a user can log in successfully. They also include how it looks when a test fails and the screenshot that's taken just before the failure. It also covers the testing of Ajax-related calls, inserting a delay when a button is clicked to wait for the response. Finally, the tutorial shows a more advanced example involving a popup modal, a form and multiple interactions.

tagged: laravel dusk browser testing tutorial introduction ajax example

Link: https://www.sitepoint.com/laravel-dusk-intuitive-and-easy-browser-testing-for-all/

Laravel News:
Tips For Building Your First Laravel Package
Feb 23, 2017 @ 09:42:08

On the Laravel News site there's a tutorial posted from Dmitry G. Ivanov giving you some helpful tips on building your first Laravel package.

Laravel is a powerful and modern framework. It has tons of different features, which make our work faster and easier. But you can’t push everything into the single box. At one time or another, we’ve all been in need of something not implemented in the framework out of the box.

[...] A package can be a solution. Write your code once and use it in any number of projects. Maybe you found a bug, or want to make some changes? Do it just once in your package code and then pull required changes in all of your projects. Sounds good?

The article then breaks down the information into a few different categories:

  • The First Step (checking Packagist for something pre-existing)
  • Development
  • Testing
  • Documentation
  • Release

He ends the post by pointing out that there's several other things to consider when creating your package but they're a bit more in-depth than a short post like this could tackle.

tagged: laravel package tips development documentation testing release tutorial

Link: https://laravel-news.com/first-laravel-package-tips

Nikola Poša:
Testing conventions
Feb 17, 2017 @ 10:31:32

In a new post to his site Nikola Poša has suggested some testing conventions he's worked up over his time in development across projects.

Testing is an essential aspect of development, and test code should be treated the same way with regard to defining and using coding conventions and standards.

This time I would like to share few conventions that I follow when writing unit tests in particular, some of which I adopted only recently.

He breaks it down into three main sections:

  • Structure (file locations and namespacing)
  • Naming (files and testing methods)
  • Arrange-Act-Assert with exceptions and test doubles

Example code is included showing the concepts and implementation of the suggested convention, just to name a few.

tagged: testing convention tutorial structure naming arrange act assert

Link: http://blog.nikolaposa.in.rs/2017/02/13/testing-conventions/

Giorgio Sironi:
Book review: Fifty quick ideas to improve your tests
Feb 13, 2017 @ 11:15:03

Giorgio Sironi has posted a review of a book to his site that shares fifty quick ideas to improve your tests that can help with things like run time, ease of maintenance and following best testing practices.

Fifty quick ideas to improve your tests is, well, a series of fifty quick ideas that you can implement on some of your automated test suites to improve their value or lower their creation or maintenance costs.

These ideas are pattern-like in which they are mostly self-contained and often independent from each other. They are distilled from real world scenarios that the authors (David Evans, Tom Roden and Gojko Adzic) have encountered in their work.

He's included a few quotes from the book he found particularly interesting including mentions of:

  • a pyramid of software quality levels
  • misunderstanding that testing can somehow be completely replaced by a set of carefully chosen examples
  • difficult testing is a symptom, not a problem
  • the problem with time-based "sleeping" in tests

You can find the book itself over on Leanpub and can pick up your own copy for around $10 USD.

tagged: book review fifty ideas improve testing ebook leanpub

Link: http://www.giorgiosironi.com/2017/02/book-review-fifty-quick-ideas-to.html