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

Laravel News:
Easily Test Email with MailThief
Jun 24, 2016 @ 11:50:56

The Laravel News site has a post that gives you a quick introduction to MailThief, a library created by the developers at Tighten Co. to make mail testing simpler.

MailThief is a new package by Tighten Co. that provides a fake mailer for your Laravel application. This makes it easy to test email without actually sending any.

They include a simple example of a script that sends an email on user registration using Laravel's own Mail library. They also include a test for the registration action showing how MailThief can be used to "hijack" the mailer and make it simpler to get information about the mail being sent. You can find out more about the tool and what prompted it in this video from Adam Wathan.

tagged: mailthief testing unittest mailer email example introduction

Link: https://laravel-news.com/2016/06/mailthief/

Richard Bagshaw:
Prophecy
Jun 24, 2016 @ 09:11:01

Richard Bagshaw has a post to his site sharing some of his experience with the Prophecy testing tool and how it compares to Mockery for creating test doubles (mocks and stubs).

For a while now I have been using Mockery as my test double framework of choice, however recently I have been taking a look at Prophecy as an alternative.

[...] "Prophecy is a highly opinionated yet very powerful and flexible PHP object mocking framework. Though initially it was created to fulfil phpspec2 needs, it is flexible enough to be used inside any testing framework out there with minimal effort."

He then gets into some basic usage of the tool - creating a basic mock, assigning expectations and behaviors and performing the test. He steps through each line of the example explaining what's happening and what can be expected as a result. He ends the post with some final thoughts comparing Prophecy to the normal PHPUnit mocking tools and points out several other features it makes easier to work with as well.

tagged: prophecy unittest doubles mock stub example introduction tutorial

Link: http://www.richardbagshaw.co.uk/prophecy/

Paul Jones:
When Possible, Use File *Resources* Instead Of File *Names*
May 13, 2016 @ 10:37:38

In this new post to his site Paul Jones makes a recommendation to those working with files and other resources: pass around the resources themselves (the streams) not filenames.

In testing the Aura.Http package, I have realized that it’s much more flexible, testing wise, to pass around file resources (a.k.a. handles, pointers, or streams) than it is to pass around file names. When you do that, you can use a php://memory stream instead of attempting to touch the file system. [...] This places control of the file creation in your hands directly, not under the control of the system under test.

He gives examples based on some testing of one of his own packages where he needed to swap out the resource being tested and mock out a file system call. It's a simple trick but could help reduce some complexity in your overall code in the right situations.

tagged: file resource name unittest testing aurahttp

Link: http://paul-m-jones.com/archives/2487

Adam Wathan:
Writing Your Own Test Doubles
May 11, 2016 @ 10:19:58

In this recent post to his site Adam Wathan about writing your own custom test doubles (fakes) to help make your tests cleaner and improve their overall readability/maintainability.

Once in a while I run into a situation where trying to use a mocking library hurts the readability of my test. For example, say I’m building out a basic user registration flow where someone signs up and receives a welcome email. [...] To test that an account is created correctly, I can make a request to the endpoint and verify that the new account exists in a test database. [...] This covers creating the account itself, but what’s the best way to test the welcome email?

He goes through a few of the options that could be used to test this including using Mockery to replace the mailer class with a spy or actually sending emails. There's downfalls to both of these methods and he suggests using a custom "fake" where the mailer class is swapped out with an "in-memory" option with the same kind of interface. He does point out a few issues with this method, however, and offers a few tips to remember when using them.

tagged: custom test unittest doubles email example video screencast

Link: http://adamwathan.me/2016/01/25/writing-your-own-test-doubles/

Marcel Pociot:
Laravel TestTools (Chrome Extension)
Mar 22, 2016 @ 10:48:10

Marcel Pociot has a new post on his site introducing a new tool he's created allowing for the easier creation of tests for your Laravel application: the TestTools Chrome extension.

Testing a Laravel application really is an easy task - the "Integrated" package from Jeffrey Way, that later got merged into the core framework is fantastic and helps you with the otherwise cumbersome task of testing and interacting with your application. But still - are you actually using tests?

A lot of times people really love the idea of tests, but simply don't get their asses up to start using them in their own projects. That's why I created a chrome extension that hopefully saves you some time when you need to test your app

He also includes an animation or two of the Chrome extension in action, showing you what kind of results you can expect. He does point out that there's some limitations of the tool including the fact that you can't really test a single-page application with it as it relies on the normal browser interaction points, not Javascript events.

tagged: laravel testing unittest chrome extension browser

Link: http://marcelpociot.com/blog/2016-03-21-laravel-testtools

Carlos Buenosvinos:
First tests with #PHP7 in production at @AtrapaloEng
Mar 18, 2016 @ 11:15:45

On his site Carlos Buenosvinos has a new post talking about the experience they had at @AtrapaloEng with PHP 7 and shares some of the improvements they've already seen so far.

On Monday, Badoo blogged about its migration to PHP7 (https://techblog.badoo.com/blog/2016/03/14/how-badoo-saved-one-million-dollars-switching-to-php7/). Those are great results! At @AtrapaloEng, we’re running already tests in production to perform the same step. We could have started some months before, but we’ve been struggling with the php-msgpack extension and its (un)support for PHP7. We hope to deploy PHP7 in all our server during this week but we would like to share with you what we have seen so far.

They share some graphs showing the changes when PHP 7 was deployed on their systems for both memory consumption and overall load average. They also talk about the boost in performance as far as response times and, an often not reported statistic, how it sped up their unit test runs too.

tagged: test php7 atrapaloeng performance results graph unittest

Link: https://carlosbuenosvinos.com/first-tests-with-php7-in-production-at-atrapaloeng/

Rob Allen:
Testing Slim Framework actions
Mar 14, 2016 @ 10:45:52

Rob Allen has a quick post to his site showing you how to test Slim actions using PHPUnit and some simple pieces of the Slim framework itself to set up the needed environment.

To test a Slim Framework action, you need a request and a response object and mock whatever is in the action. This is one way to do this.

He gives an example of a simple endpoint that just returns a JSON string. He shows the code for this endpoint and how it registers with the application for an /echo route. He then gets in to the testing on the route's matching class, making an instance of the Request class and an Environment for it to work in. He ends the post by sharing teh code to pull all of these pieces together in a simple PHPUnit test that uses the assertSame assertion to verify the JSON response output.

tagged: slimframework action unittest phpunit testing tutorial request environment

Link: https://akrabat.com/testing-slim-framework-actions/

QaFoo:
Testing Effects of Commands With Phake::capture()
Mar 11, 2016 @ 09:15:15

On the QaFoo blog they've posted an article sharing a quick testing tip when using Phake to test APIs where the calls don't return any data.

Today I want to share a simple trick for the excellent Mocking library Phake (I wrote about it before) when testing state on APIs that don't return values.

Testing becomes harder when you are using command / query separation in your code and service operations don't return values anymore. If your command method creates objects and passes them down the stack, then you usually want to make assertions on the nature of the changes.

He gives an example of a class that creates an Order object and saves it but doesn't return a value. He shows how to update this with Phake in the test to capture and verify that the calls are correctly made and the number of products on the order is correct.

tagged: phake capture tutorial example unittest return value

Link: https://qafoo.com/blog/078_phake_capture.html

Adam Wathan:
Preventing API Drift with Contract Tests
Feb 03, 2016 @ 12:11:21

In this post to his site Adam Wathan shares a screencast talking about changing APIs (the structure of your code, not like REST/SOAP APIs) and how "API drift" could cause problems in your testing.

One of the risks of writing your own test doubles is that the API of the double can fall out of sync with the API of the real implementation.

In this screencast I walk through an example that explains: how interfaces can provide a false sense of security, why tests make better contracts than interfaces and how to run multiple implementations against a shared set of contract tests.

You can watch the screencast either through the in-page video player or over on Vimeo directly. It's about 10 minutes long but it covers an interesting topic that could throw you if you're not careful in your testing/code changes.

tagged: screencast contract test unittest api drift interface changes

Link: http://adamwathan.me/2016/02/01/preventing-api-drift-with-contract-tests/

Taha Shashtari:
A Gentle Introduction to Testing in PHP
Feb 02, 2016 @ 10:11:20

If you're not already doing testing (like unit testing) in your PHP-based applications but want to start, this new post on Taha Shashtari's site might just be for you. It provides a "gentle introduction" to unit testing, what it is and some of the tools you can use to get started.

Learning testing can be very overwhelming especially if you're just starting out. [...] You might have read a bunch of PHPUnit tutorials and maybe you tried to apply some of what you've learned in your projects, but it doesn't always feel right. And sometimes you get into situations where you have no clue how to test some feature in your application.

If anything of this happened to you, don't feel bad, we all have had this experience. And it's almost because of the way we learn it.

He starts by talking about some of the main goals of testing in your application and the difference between manual and automatic testing. He then gets into each of the three main testing types and follows it with links (and summaries) to tools you can use to get started testing. He ends the post with a brief look at test-driven development and, setting the stage for the next tutorial in the series, looks at the path ahead.

tagged: tdd definition basic introduction gentle testing unittest phpunit tools summary

Link: http://taha-sh.com/blog/a-gentle-introduction-to-testing-in-php