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

Sammy Kaye Powers:
Writing tests for PHP source (Part 5 & 6)
Jul 25, 2017 @ 09:56:56

Sammy Kaye Powers has posted the latest parts in his series looking at testing the PHP language with phpt tests. So far he's helped you compile PHP from source, run the test suite, learn about the phpt files and debug failing tests. He continues the series with two new posts:

In the 5th part of his series he shows how to use the PHP gcov site to locate lines of code in the PHP language core that aren't tested yet, how to create a new test to cover it and generating a code coverage report to see how much you've tested. In Part 6 he shows you how to take what you've created and submit it back to the PHP project on GitHub as a Pull Request (no RFC needed) based on changes from your own forked repository.

tagged: series testing language phpt untested gcov source pullrequest

Link: https://www.sammyk.me/finding-untested-code-in-php-source-writing-tests-for-php-source

ThePHP.cc:
Why Developers Should Not Code
Jul 19, 2017 @ 11:16:01

On thePHP.cc blog Stefan Priebsch offers up an interesting opinion about code, developers and understanding - developers shouldn't code.

The ultimate problem with program code seems to be that no human really understands it. Sure, we can look at a short piece of code and be relatively clear on what it does, but can we still do the same thing with programs that span tens or even hundreds of thousands of lines?

[...] Well, sometimes I get a strong feeling that there is a shortage of good programmers, because I often find myself looking at legacy code, being unable to tell what it does, at least with reasonable certainty. [...] Personally, I already consider code to be problematic when there is a reasonable amount of doubt as to what it does (and why it exists). To me, uncertainty and discussions are a sure sign of bad code. Call me picky, but years of experience have taught me that this level of strictness makes sense.

He suggests that the fact a developer cannot recognize what current code is doing doesn't make you a poor developer, but the opposite. He talks some about the meaning of the word "code" and how it is written for a machine to understand, not a human. He ends the post talking about testing your code to provide an "executable specification" and, despite having this, a human-readable spec is still a requirement (like it or not).

tagged: developer code opinion specification testing

Link: https://thephp.cc/news/2017/07/why-developers-should-not-code

Ben Ramsey:
Announcing Testfest 2017
Jul 17, 2017 @ 11:14:53

In a new post to his site long time PHP community member Ben Ramsey has officially announced PHP Testfest 2017, an event that gathers developers to write tests for the PHP language itself to help improve its quality and show them how to contribute back to the project.

For those who’ve been around the PHP community for a while, you’ll recall the successful PHP TestFest events that began after a discussion at PHP Quebec in 2008. Many user groups and mentors signed on to host and help with events, and a lot of folks became first-time contributors to the PHP project, helping improve our code coverage. It ran strong in a global sense from 2008 to 2010. After that, various groups (particularly the Brazilian groups) have continued the tradition.

A few months ago, at php[tek] in Atlanta, I mentioned to Michelangelo that I’d love to bring back PHP TestFest. Sammy had given an excellent talk on writing PHPT tests, and <a href="https://twitter.com/ellotheth/status/868583446498734084>Gemma tweeted a link to the old PHP TestFest wiki page. From there, things snowballed.

Ben the provides the details of the planned Testfest that will run for four months and can be worked on from anywhere, not just one single event. The plan is to have it run from September through December of 2017 and there's plenty of support to help out. Organizers can email for more help getting started. There's also a website, Google Group and IRC channel as well as tools and resources to help bring you up to speed on testing the PHP language with phpt tests.

tagged: testfest17 testing language phpt event quality testfest community

Link: https://benramsey.com/blog/2017/07/phptestfest/

Laravel News:
A Roundup of Laravel Testing Resources and Packages
Jul 12, 2017 @ 09:50:29

On the Laravel News site there's a new post sharing a list of Laravel testing resources and package that you can use for your Laravel-based applications to make testing easier and less work overall.

Testing code is one of the more challenging parts of programming, but luckily Laravel and some other tools are making it easier than ever. Research has even shown implementing a Test-Driven Development (TDD) approach can significantly reduce the number of bugs that make it to production. Testing provides many other benefits, like the freedom to refactor large parts of a system without (as much) fear of breaking things.

With all of the benefits of testing, it’s still challenging to continually test applications. If you are looking to start learning how to test Laravel applications or you want to expand your skills here is a list of great resources to help you.

Included in their list are resources like the official documentation, Laravel Dusk, the Laracasts Laravel Behat extension, the phpspec Laravel extension and Spatie Laravel packages. Each item on the list includes a screenshot, a brief description of the resource/tool and links to get more information about it.

tagged: laravel testing package tool list resource link

Link: https://laravel-news.com/laravel-testing-resources

QaFoo Blog:
Refactoring Singleton Usage to get Testable Code
Jul 11, 2017 @ 12:22:07

The QaFoo.com blog has a new post sharing a helpful hint on refactoring singletons to make them more testable. Singletons and notoriously difficult to test due to how it can potentially return an unexpected version of an object.

So your code base is littered with singletons and using them? Don't worry, you can start refactoring them out of your code base class by class and introduce increased testability at every step. This strategy is very simple to implement and the probability of breaking your code is very low, especially when you are becoming more experienced with this technique.

They give an example of a service class that uses a singleton to get an instance of the Solarium_Client class via a static method call. They show how to refactor this out into a separate method and then use the "lazy initialization" pattern to only use the singleton if the property isn't already defined. This then allows you to use a setter to inject your own client during testing (a mock most likely).

tagged: refactor testing unittest mock singleton property lazy initialization

Link: https://qafoo.com/blog/107_refactoring_singletons_testability.html

Colin O'Dell:
Testing PHP 7.2 With Docker
Jul 07, 2017 @ 11:58:07

Colin O'Dell has posted a guide that you can follow to help you test your applications with the latest version of the PHP 7.2 pre-releases using a simple Docker setup.

PHP 7.2 is slated for release in November 2017, but you don't have to wait until then to start testing your applications! We can easily use Docker to test against the latest pre-release versions of 7.2.

He's even created a Docker image that's posted on DockerHub to make the process even simpler. He shows how to reference specific types (php-fpm, apache, etc) and specific tags for the PHP pre-releases on each. He encourages you to log bugs in the PHP if found and report issues to him if there's a problem with the Docker setup. He also mentions that, since the mcrypt extension was removed by default from PHP 7.2, you'll need some special commands to get it set up and working (via a PECL install).

tagged: testing php72 prerelease version docker image dockerhub

Link: https://www.colinodell.com/blog/201707/testing-php-72-docker

SitePoint PHP Blog:
What Is Snapshot Testing, and Is It Viable in PHP?
Jun 21, 2017 @ 12:28:01

On the SitePoint PHP blog they've posted another article from author Christopher Pitt. This time he talks about snapshot testing and if it's a viable way to evaluate code in PHP applications.

Ah-ha moments are beautiful and rare in programming. Every so often, we’re fortunate enough to discover some trick or facet of a system that forever changes how we think of it. For me, that’s what snapshot testing is.

You probably write a lot of PHP code, but today I want to talk about something I learned in JavaScript. We’ll learn about what snapshot testing is and then see how it can help us write better PHP applications.

He starts with a brief introduction to the technology to be used (React in Javascript) and how it could be recreated in PHP. He then covers testing interfaces, evaluating the correct functionality of frontends via various testing tools. He includes some examples of this kind of testing and how it can help a developer mentally break up functionality into components. From there he switches over to the PHP side, introducing the spatie/phpunit-snapshot-assertions package and showing how to use it to check the make up of a snapshot and test several assertions that should exist.

tagged: snapshot testing viable javascript frontend assertion tutorial

Link: https://www.sitepoint.com/snapshot-testing-viable-php/

SitePoint PHP Blog:
Web App Performance Testing with Siege – Plan, Test, Learn
May 30, 2017 @ 12:27:36

The SitePoint PHP blog has a tutorial posted that wants to help you test you application and get the best performance from it. The tutorial introduces you to Siege, a performance testing tool that includes benchmarking functionality.

Almost anyone can build an app locally, deploy it to a server, and proudly show it to your friends. I hope you’ve already done all of this, and your project went viral, so you’re obviously here because you want to learn how to make sure your app is ready for some high traffic.

[...] In this article, designed to speed this [learning] process up, I will cover the basic concepts of testing the app (regression, load, and stress testing) with Siege</a and some tips and tricks I like to use when I’m testing my own web apps.

The tutorial starts with an overview of the types of testing and a few tips for making your testing successful and effective. Then, using a sample Symfony demo application, they show how to make basic requests using Siege and what kind of data the results report. With the basics out of the way, they then cover more advanced topics like concurrency and doing the actual performance testing. Based on the results, they see that caching content could definitely help and, after enabling that, see the numbers go down dramatically. The post ends with a look at other testing tools and some of the things you'd need to plan out when using Siege against your application to get the most meaningful results.

tagged: application performance testing siege tool tutorial

Link: https://www.sitepoint.com/web-app-performance-testing-siege-plan-test-learn/

thePHP.cc:
Testing Keeps Me From Getting Things Done
May 25, 2017 @ 09:52:29

On thePHP.cc site they have a new post that tries to refute a common claim from developers when it comes to testing: testing keeps me from getting things done. The post is a response to an email to the group about testing asking where the real value is in applications versus libraries/tools.

To successfully develop software means to work target-oriented. These targets should be derived from acceptance criteria that are reconciled with the business. Without clear targets – we mean at a task level, not project or annual targets – the developer runs the risk of getting lost in work. Most importantly, he does not know when he is done with a task.

It is prudent to document and verify acceptance criteria through automated tests. One way or another, the targets have to be defined before production code gets written. This is test-driven development, whether you want to call it that or not.

The response goes on to talk about how, with tests written after the code has already been written (legacy code), it's not always clear what the original intent was resulting in lost context. It also compares two of the main types of testing - integration and unit - and the place each has in an overall testing strategy.

tagged: testing unittest reply integration opinion application

Link: https://thephp.cc/news/2017/05/testing-keeps-me-from-getting-things-done

Medium.com:
Expressive Code & Real Time Facades
May 10, 2017 @ 11:13:54

On his Medium.com blog Laravel project lead Taylor Otwell shares some of his thoughts on expressive code and real-time facades and how they make things simpler, event for testing/mocking.

Recently, I worked on some code that surfaced my most common use-case for Laravel 5.4’s “real-time” facades. If you’re not familiar with this feature, it allows you to use any of your application’s classes as a Laravel “facade” on-demand by prefixingFacades to the namespace when importing the class. This is not a feature that is littered throughout my code, but I find it occasionally provides a clean, testable approach to writing expressive object APIs.

To illustrate he uses the code from the Laravel Forge service talking about service providers (like DigitalOcean, Linode, etc) and "service" classes to contain API methods. He then shifts over to the controller to see how he'd like to access it, making a generic Provider class with a make method to create the instance. This has an issue, however, with testing making it very difficult. Instead he shifts over to the real-time facades and a factory where the test can more easily manually mock the method into a stub provider (example included).

tagged: expressive code realtime facade testing factory tutorial

Link: https://medium.com/@taylorotwell/expressive-code-real-time-facades-41c442914291