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

Lanre Adelowo:
A Subtle Introduction to Mocking
Dec 08, 2016 @ 12:16:10

Lanre Adelowo has a recent post to his site introducing you to some of the basics of "mocking" in unit testing, some of the reasons to use it and plenty of examples of it in action using the Mockery library.

Mocking is simply the process of replacing an object with a fake that can act as a replacement. [...] The major reasons why we mock are Dependency elimination and removal of side effects. Think things like databases, 3rd party API requests and network requests, code that has to hit the filesystem.

This stuffs aren’t always guaranteed to be available or can prove tedious to set up (an internet connection for example) and even when they are (a logger that writes to the filesystem for example), they always tend to make your tests run extremely slow.

After covering some of the basics of mocking he talks about how they differ from stubs and how to get Mockery installed. He illustrates some of the basics concepts with a "user search" functionality based on a API request to GitHub. He's writing the results to the file system (via a Logger) so this is the main target of the mock. He creates a mock "FileSystem" class the Logger is refactored to use. He then mocks this dependency out and defines a "shouldReceive" handler for the call to write the log. This replaces the need for the test to write to the file system and makes it possible to test things in isolation rather than relying on the environment.

tagged: mocking introduction unittest mockery tutorial

Link: http://lanreadelowo.com/blog/2016/12/02/a-subtle-introduction-to-mocking/

Adam Wathan:
Replacing Mocks with Spies
Oct 13, 2016 @ 10:25:14

In this post to his site Adam Wathan shares a unit testing tip that can help you with more correct verification in your testing - replacing mocks with spies.

Mock objects are useful when verifying that a method was called is more important than verifying the outcome of calling that method.

[...] Mocks are a great tool, but it’s always bugged me that they force you to set expectations in advance instead of making assertions at the end like you would in a traditional test.

He gives a Laravel-based example of using Mockery to set an "expects" call on a method to ensure it's correctly called. He points out, however, that this method is more useful for checking the result of the method call and not really the fact that it was called (a slight but interesting difference). He then gives an example of testing the PHP function strrev and the phases you would go through in the testing process. He suggests that, in the creation of the mock object, you're mixing up the "setup" phase with the "assertion" phase and making them dependencies. He shows how, with a switch over to using spies instead, including the code updates for Mockery that change the mock object creation and split out the assertion from the creation.

tagged: mock spy unittest mockery assertion setup

Link: https://adamwathan.me/2016/10/12/replacing-mocks-with-spies/

SitePoint PHP Blog:
Building a SparkPost Client: TDD with PhpUnit and Mockery
May 04, 2016 @ 12:26:32

On the SitePoint PHP blog they've continued their series covering the SparkPost mail delivery service and integrating it in to your application. In this latest part of the series author Christopher Pitt starts looking at the SparkPost API and uses it as a chance to practice some TDD (Test Driven Development) skills.

In a previous post, we looked at SparkPost (as an alternative to Mandrill), and explored a bit of the official PHP client. The official client handles a decent amount of work, but I got to thinking about what it would take to build a new client.

The more I thought about it, the more it made sense. I could learn about the SparkPost API, and practice Test Driven Development at the same time. So, in this post we’ll look to do just that!

He uses a few different libraries to explore the API and its endpoints: Guzzle for the HTTP requests and the Mockery+PHPUnit combination for the testing. He includes the setup and configuration for the testing environment and some sample tests for making sure things are connected. He then integrates Mockery into the testing, using it to mock the Guzzle requests and still have the tests pass even without the actual connection. He then works through several other tests and finishes the post with a mention of building coverage results for the "Client" class.

tagged: sparkpost client tutorial series tdd testdriven development mockery phpunit guzzle api

Link: http://www.sitepoint.com/building-a-sparkpost-client-tdd-with-phpunit-and-mockery/

Robert Basic:
Mocking hard dependencies with Mockery
Dec 26, 2014 @ 11:14:51

Robert Basic has a post today showing how you can mock hard dependencies with Mockery, a mocking library for use in unit testing. In this case, "hard" refers to work around the use of "new" creating objects in hard to test places.

One problem with unit testing legacy applications is that the code has new statements all over the place, instantiating new objects in a way that doesn't really makes it easier to test the code. Of course, the easy answer to this is "Just refactor your application!", but that's almost always easier said than done. If refactoring is an option, do it. If not, one option is to use Mockery to mock the hard dependencies.

He makes use of instance mocks to show the overloading of the service without the need for a refactor. This overrides it on a more global scale, so it could have an effect on other tests. He shows how autoloading and PHPUnit's own process isolation handling can fix tis problem (though it takes more time to run the tests this way). He includes sample code of the whole process so you can easily follow along too.

tagged: mockery dependency hard new instance phpunit unittest

Link: http://robertbasic.com/blog/mocking-hard-dependencies-with-mockery

Dave Marshall:
Mockery Spies
Oct 09, 2014 @ 10:29:08

In his latest post Dave Marshall takes a look at a handy feature of the Mockery mocking tool (helpful for unit testing) and how to use them in your testing.

Spies have been on the cards for mockery for a long time and even after putting together an implementation in February, I kind of stalled out on making a decision on the public API. Fast forward a few months and I figured it was just time to ship it, so I went with the most mockery like API and merged it in. Mockery still doesn't have a 1.0 release, so I can always make changes before we go 1.0.

For those not familiar with the concept of "spies" in testing he includes a brief definition and some of the reasoning behind using them. The first is relatively simple: how they can reveal the intent of the test. They also allow for two other types of testing methods, "Arrange-Act-Assert" or "Given-When-Then" thinking patterns. He does mention, however, some of the problems with using spies over mocks (including that they're less precise, possibly leading to looser testing). He finishes up the post with a quick note about partial spies and how they can provide a nice compromise in your testing.

tagged: mockery unittest spies doubles mock compare feature

Link: http://davedevelopment.co.uk/2014/10/09/mockery-spies.html

SitePoint PHP Blog:
Mock your Test Dependencies with Mockery
Jun 26, 2014 @ 14:26:58

The SitePoint PHP blog has a new post today by Peter Nijssen showing how to use a library that's an alternative to the internal PHPUnit mock handling. The post shows you how to use Mockery to test your applications and abstract out any outside dependencies.

Although not everyone is doing it yet, testing your application is one of the most essential parts of being a developer. Unit tests are the most common tests to run. With unit tests, you can check if a class behaves exactly like you intended it too. Sometimes, you are using a third party service within your application and it’s hard to get everything set up to get this unit tested. That’s exactly when mocking comes into play.

He starts with a brief introduction to the concept of mocking before getting into his examples. He shows how to get it installed (via Composer) and how to add it as a test listener to your PHPUnit configuration file. He then gets into an actual example: mocking out an external API dependency for a weather service. He shows a simple one-method mock example as well as a more complex example using a more randomized result rather than just a static one.

tagged: mock unittest phpunit mockery tool introduction tutorial

Link: http://www.sitepoint.com/mock-test-dependencies-mockery/

Konstantin Kudryashov:
Conceptual difference between Mockery and Prophecy
Jan 14, 2014 @ 10:47:20

In a new post to his site today Konstantin Kudryashov takes a look at two PHP testing tools and the differences/similarities between them - Mockery and Prophecy. Mockery is a mocking tool created by Pádraic Brady to make mocking simpler and Prophecy is more of a mocking framework, both for use in PHP unit testing.

Today I’ve been asked twice what’s the difference between Mockery and Prophecy just to suddenly discover that I didn’t clarify this aspect never before. Well, that’s about time. If we were to remove all the syntactical and implementation differences between two libraries what we’ll be left with is one really big conceptual difference and it’s the fact that in contradiction to Mockery, Prophecy puts messaging (aka how objects communicate) before structure (aka when objects communicate).

He starts with a sample class (the usual "calculator" example) and shows how it would look to mock it out with each tool, setting return values for both the "getRating" and "setRating" methods. He enhances the tests a bit more to include an event dispatcher and raising an event. The approach is similar, but Prophecy uses something called "message binding" to more effectively handle changes to the class under test.

tagged: difference mockery prophecy unittest mocking framework

Link: http://everzet.com/post/72910908762/conceptual-difference-between-mockery-and-prophecy

Chris Hartjes:
Test Spies and Mockery
Dec 31, 2013 @ 09:39:30

Chris Hartjes has a new post today looking at using test spies with Mockery, the alternative mocking framework to PHPUnit's own built-in functionality. Test spies are a feature that allows you to replace certain functions and methods of already created objects to help make mocking functionality easier.

While recording some screencasts I was struggling to figure out how to get PHPUnit’s built-in object mocking tools to allow me to create what is known as a “test spy”. I talk about them briefly in my PHPUnit Cookbook but I think that what I wanted to do in this instance was beyond what PHPUnit could give me.

He works through what he tried with the built-in PHPUnit mocking and found that it wasn't using the objects he provided as expected. He shifted over to Mockery and created a working version using the "shouldReceive" and "with" methods to handle the checks (spies) on the objects correctly.

tagged: unittest spies phpunit mockery example

Link: http://www.littlehart.net/atthekeyboard/2013/12/27/test-spies-and-mockery

Gonzalo Ayuso:
Building a BDD framework with PHP
Aug 19, 2013 @ 09:49:57

<p. Gonzalo Ayuso wanted to look into BDD (behavior driven development) in PHP and was looking around for a tool to fit his needs. He didn't find one right away and so decided to create a simple one as a proof of concept to try out the method for himself.

I want to write as less code as I can (it’s only a proof of concept), so I will reuse the assertion framework or PHPUnit. As I’ve seen when studying Behat, we can use the assertion part as standalone functions. We only need to include vendor/phpunit/phpunit/PHPUnit/Framework/Assert/Functions.php file.

He includes the sample code showing his basic interface - a string calculation object being passed into a closure for evaluation by the PHPUnit assertion methods. He also includes an example of using it along with Mockery for creating a mock object and testing based on that.

tagged: bdd behaviordriven development framework proofofconcept poc mockery

Link: http://gonzalo123.com/2013/08/19/building-a-bdd-framework-with-php

Simon Jodet:
Partial mocks with Mockery
Jan 15, 2013 @ 09:10:14

Simon Jodet has a new post to his site for the Mockery users out there (a mocking tool used in unit testing) showing how he created his partial object mocks.

I really like atoum and highly recommend it. However, because it's a young project, I have two problems with it: The documentation was really lagging but it's getting there and integration with other tools is poor. [...] And then I stumbled on Mockery. Unlike atoum, it doesn't replace PHPUnit, it just replaces its mocking system with a more elegant, powerful and reliable one. The big plus of Mockery over PHPUnit is that you don't need to have the class to mock to create a mock. [...] But writing partial mocks with Mockery is not obvious.

He illustrates with a "Database" class example containing four methods, only three of which he wants to mock out - dropTable, listTables and getSchema. The fourth method, "reset", is then called directly and the mocked methods are used inside that. The database connection mock is then injected into the Database mock and the test is run. Complete code examples are provided.

tagged: mocks mockery class tutorial phpunit unittest

Link: