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

Matthias Noback:
Testing actual behavior
Jun 26, 2018 @ 13:48:02

Matthias Noback has posted another article covering domain-driven development practices, this time focusing on testing actual behavior and some of the downsides that can come with it and domain-driven development.

He breaks the article up into three main sections:

  • The downsides of starting with the domain model
  • The downsides of starting with the smallest bricks
  • The downsides of your test suite as the major client of your production code

Each includes a description of the downsides related to the topic and, where it helps, code to illustrate the issue. The post also includes three experiments to try to help prevent some of these downfalls from happening (including not testing constructors and not adding getters).

Tests are not the main use case of an object, they should guide the development process and make sure you write exactly the code that you need. This means not sacrificing the object's encapsulation just to make it testable.
tagged: domaindriven development tutorial testing behavior downsides

Link: https://matthiasnoback.nl/2018/06/testing-actual-behavior/

Sammy Kaye Powers:
Testing Unreleased Features of PHP
Jun 26, 2018 @ 09:31:52

In a new post to his site Sammy Kaye Powers shows you how to test unreleased features of PHP using a pull request related to a RFC that's officially been proposed.

We'll be discovering and testing a completely unreleased feature of php-src from an RFC that's still under discussion.

If you've ever wanted to be ahead of the curve of PHP features or you've just wanted to contribute back to PHP internals, testing an unreleased feature from an RFC is a fun and educational way to do so.

He uses the typed properties RFC in his example (allowing the addition of types to class properties). He then walks through the checkout of the PHP source, grabbing the pull request for the RFC and compiling PHP with the new code in place. He shows an example of a script that makes use of this new feature and tries it out. Finally, he covers how to run tests on this new version and some suggestions on adding typed properties to real-world projects.

tagged: language rfc pullrequest tutorial testing unreleased feature

Link: https://www.sammyk.me/how-to-compile-an-unreleased-rfc-feature-for-php-source-php-internals

Michaelangelo van Dam:
Testing your e-commerce PHP application
May 02, 2018 @ 10:10:45

Michelangelo van Dam has a new post on his site sharing some helpful hints you can use to test your e-commerce application, be it WooCommerce or other popular PHP-based e-commerce packages.

I'm participating (as much as possible) in the #30daysoftesting challenge organised by Ministry of Testing and SauceLabs. If you're interested, read the full 30 Days of E-Commerce Testing article and join this fun and educational challenge.

The 2nd challenge on the list was to read and share interesting blog articles about E-commerce testing. Since I'm working as a PHP professional I thought it would be great if I oriented my focus on testing PHP based E-commerce platforms. I picked WooCommerce as it's an easy to install and use E-commerce solution. For Magento, PrestaShop and others I've added useful links at the bottom of this article.

He then gets into the details of WooCommerce and what it has available from a testing perspective. He also includes links to a few resources for more general WordPress testing too. The remainder of the post consists of links to more information about testing the other popular e-commerce packages: Magento, PrestaShop, OpenCart, Sylius and Laravel-based packages.

tagged: testing ecommerce application woocommerce magento prestashop

Link: https://www.dragonbe.com/2018/05/testing-your-e-commerce-php-application.html

php[architect]:
Testing Strategy With the Help of Static Analysis
Apr 30, 2018 @ 12:49:41

php[architect] magazine has shared another article from their April 2018 issue on their site. In this article Ondrej Mirtes covers the use of static analysis to help testing and inform you of type safety issues.

When developing an application, our aim as software developers is to make sure it does what it ought to do and to keep the number of defects as low as possible. [...] In this article, I’d like to introduce you to the concept of type safety and how it can improve the reliability and stability of your code. Once your code is more type-safe, and that fact is verified by automated tools, you can cherry-pick which parts of your application need extensive unit tests and where you can rely just on well-defined types.

He starts off by talking about types (strict and dynamic) in PHP and the current state of the typing system for variable values. He covers the role of good type hinting in method and function definitions and how it can help IDEs like PhpStorm locate issues. He also talks about how type hints can provide you feedback on the design of the application and some of the tools that can help you find issues.

He wraps up the article with some tips for making your code more strongly typed and a look at what kind of tests are needed to help ensure these types remain enforced.

tagged: testing staticanalysis help article tutorial tools

Link: https://www.phparch.com/2018/04/testing-strategy-with-the-help-of-static-analysis/

Junior Grossi:
Solitary or Sociable? Testing Events and Listeners using Laravel
Apr 30, 2018 @ 09:38:58

In a new post to his site Junior Grossi covers two main methods for testing event handling in Laravel-based applications: either solitary or sociable.

Testing with Laravel is very easy, but it can be a nightmare when the tests depend on Events and Listeners. In this post I’m gonna show you how you can simplify and improve those tests.

[...] When testing events in Laravel, you can fake() events in a very simple way, like the documentation says. The problem is not with the events, but with the listeners, because usually a listener does a single action, but with more than one listener you start writing repeatable tests.

He starts by looking at some of the background behind the ideas of "solitary" and "sociable" as it relates to events. He then gets into the code, providing some examples of event tests (and how it could evolve into larger, unwieldy tests). He provides some recommendations on refactoring tests to help get around these issues. As a bonus at the end of the post he shows how to create sociable tests, complete with example code.

tagged: testing event handling laravel tutorial solitary sociable

Link: https://blog.jgrossi.com/2018/solitary-or-sociable-testing-events-and-listeners-using-laravel/

Nikola Posa:
Testing web API clients using Guzzle Mock Handler
Apr 09, 2018 @ 09:43:06

Nikola Posa has a quick post to his site showing how to use a mocked up HTTP request handler - in this case Guzzle - for testing web API clients. These scripts make live HTTP requests to remote APIs as a part of their functionality but this presents a dilemma for testers. Unit tests should not reach out to external sources...and that's where the mock comes in.

Whether you're writing a client for your own web API to offer it to users or you're simply implementing integration for a 3rd-party API in your system, it is important to test it to make sure your client is capable of handling actual API responses correctly.

Testing web API clients is mostly about checking how they deal with responses received after sending requests to API endpoints, and for your unit tests, you introduce test doubles to simulate API calls instead of executing real HTTP requests.

He starts with a quick note about the usual way he's seen this issue worked around, creating a separate mocked class instead of using the actual tool. Fortunately, if you're a Guzzle user, there's a tool that comes with the HTTP client that can be used in your unit tests in much the same way as a normal Guzzle instance: the Guzzle MockHandler. He includes some example code showing how to use this class and inject it into your client and use it in much the same way as a normal client instance.

tagged: guzzle http testing mock unittest handler tutorial

Link: https://blog.nikolaposa.in.rs/2018/04/07/testing-web-api-clients-using-guzzle-mock-handler/

Mark Baker:
Discharging Static #2
Apr 05, 2018 @ 10:22:09

Mark Baker continues his series looking at the use of static properties and methods in applications and how to test them. In this second part of the series he focuses more on some of the unintentional side-effects that could happen when you're trying to refactor them out.

In the first article in this series, I wrote about the problems with testing static methods in classes, and showed a few approaches that allow us to write mocks for statics. Testing classes where we have static properties is a lot harder to manage, because any changes made in any test to those properties are global, across the entire test suite, not simply the test that we are running. Static properties are part of the global state, and we can’t simply tearDown() those changes in the way that we do with instances — at least we cannot easily do so if the property has a visibility of private or protected.

He goes through an example of a refactor from a static property (essentially in the global scope) to a private property. He points out the issue of setting a static value in what seem to be separate child classes, the fact that it actually changes the base value, not the individual ones, leading to potentially unintended consequences.

His main recommendation is to avoid the use of static properties all together. Where that's no possible (like in a legacy project) he offers two potential solutions: either replace them with constants if they're never changed or changing them to instance properties.

tagged: static property series part2 refactor consequences testing

Link: https://markbakeruk.net/2018/04/03/discharging-static-2/

Matthias Noback:
Mocking the network
Apr 04, 2018 @ 11:19:06

Matthias Noback has continued his series about "mocking the edges" in your unit testing with this new post. In it, he talks about mocking "the network", those places where your application reaches out to external services to access data or perform other operations.

In this series, we've discussed several topics already. We talked about persistence and time, the filesystem and randomness. The conclusion for all these areas: whenever you want to "mock" these things, you may look for a solution at the level of programming tools used (use database or filesystem abstraction library, replace built-in PHP functions, etc.). But the better solution is always: add your own abstraction.

[...] The same really is true for the network. You don't want your unit tests to rely on a network connection, or a specific web service to be up and running. [...] The smarter solution again is to introduce your own interface, with its own implementation, and its own integration test. [...] Though this solution would be quite far from traditional mocking I thought it would be interesting to write a bit more about it, since there's also a lot to say.

In his example he shows the use of a file_get_contents call to fetch stock information. He introduces a ExchangeRateService interface with a getFor method to provide structure for the "wrapper" around the network call. He then covers the idea of an "anti-corruption layer" to change up the interface to use models instead of just a string value (code included). He ends the post talking about the inversion of the dependency - the option to have a job pull the value out-of-band and then have the application use that value.

tagged: mock testing unittest network connection interface inversion

Link: https://matthiasnoback.nl/2018/04/mocking-the-network/

Christoph Rumpel:
Build a newsletter chatbot in PHP - Part 3
Mar 27, 2018 @ 10:57:24

Christoph Rumpel has continued his series covering the creation of a chatbot using the Botman package in a new post to his site. The latest post, part three in the series, builds on the base created in parts one and two and integrates the bot with his project website.

In part one and two we created a Facebook Messenger chatbot that let your users subscribe to your newsletter. We stored that information in the database and sent out our first newsletter. In the last third part, we integrate this bot to a website and write our first tests.

He then walks you through the process of using the Facebook Customer Chat Plugin to integrate it into the site (using the Facebook JavaScript SDK). He includes instructions on whitelisting your domain and including the plugin into the site's source using a few custom configuration options. With the integration complete he then takes a step back and shows the creation of several tests evaluating the fallback handling and subscription conversation responses.

tagged: chatbot tutorial series part3 integrate facebook testing fallback subscribe

Link: https://christoph-rumpel.com/2018/03/build-a-newsletter-chatbot-in-php-part-3

Ivan Enderlin:
How Automattic (WordPress.com & co.) partly moved away from PHPUnit to atoum?
Mar 22, 2018 @ 12:08:37

In a post to his blog Ivan Enderlin talks about a move that his team at Automattic (the company behind WordPress) made away from using PHPUnit for their application testing over to atoum and some of the reasoning behind it.

Few months ago at tagged: wordpress testing unittest atoum phpunit comparison migration

Link: https://mnt.io/2018/02/26/how-automattic-partly-moved-away-from-phpunit-to-atoum/