News Feed

News Archive
feed this:

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

Konstantin Kudryashov:
Conceptual difference between Mockery and Prophecy
January 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.

0 comments voice your opinion now!
difference mockery prophecy unittest mocking framework

Mocking with Phake
March 13, 2013 @ 12:53:26

On the QaFoo blog today there's a new tutorial posted showing you how to make unit test mocks with Phake, a testing tool that works a bit differently that other options (PHPUnit's own mocking and Mockery).

The use of Mock and Stub Objects is an important skill to learn when using Test Driven Development (TDD). Mock objects allow you to replace dependencies of an object with lookalikes, much like crash test dummies are used during automobile safety tests so humans aren't harmed. [...] Using Mocks in PHPUnit tests means using the built-in MockObjects library for quite some years. In the last years two contenders emerged that can be used as optional dependencies in PHPUnit. [...] This blog post introduces Phake, because it works quite differently than both PHPUnit Mock Objects and Mockery.

They include a code example of how to create mocks with Phake, showing it's different approach to defining the mocked methods. They explain each part of the code, detailing the use of the "when", "thenReturn" and "verify" methods. You can find out more about Phake and see other examples in the project's documentation.

0 comments voice your opinion now!
mocking unittest phake library tutorial introduction

Zumba Engineering Blog:
Mocking Singleton PHP classes with PHPUnit
November 26, 2012 @ 09:51:04

On the Zumba Engineering blog today Chris Taylor has a new post about mocking in PHPUnit, specifically how to handle those pesky Singleton methods lurking around your codebase.

In many of our projects, utilities and vendor classes are implemented with a singleton pattern. [...] In this post, we'll cover a nice way to inject a PHPUnit mock object for use in testing methods that utilize singleton classes.

He starts by introducing mocking and how to use mock classes in PHPUnit with a simple "sayHello" example. Adding on another layer, he creates a "SomeclassMock" class, defining its own "expects" and "cleanup" methods. This class forces the Singleton method to act more like a regular non-static method and "resets" it after each use.

0 comments voice your opinion now!
mocking phpunit class singleton expects cleanup tutorial
All About Mocking with PHPUnit
September 28, 2012 @ 09:46:21 has another post for those out there wanting to further their PHP unit testing knowledge. In this new tutorial, Csaba Patkos introduces you to mocking objects with PHP Unit - a powerful method to help test some of the more difficult things.

There are two styles of testing: "black box" and "white box" styles. Black box testing focuses on the object's state; whereas, white box testing focuses on behavior. The two styles complement each other and can be combined to thoroughly test code. Mocking allows us to test behavior, and this tutorial combines the mocking concept with TDD to build an example class that uses several other components to achieve its goal.

He illustrates mocking with his sample "toy car" application and shows how to use a few different types of mocks (test doubles) to create some tests. These types include dummy objects, test stubs, test mocks and test fakes. Code is included for all test examples, including some showing the use of the actual PHPUnit mocking functionality.

0 comments voice your opinion now!
mocking phpunit tutorial doubles unittest
Advanced PHPUnit Testing Annotations and Mocking
February 17, 2011 @ 09:33:08

On today there's a new article in their unit testing series posted today - a look at annotations and mocking in advanced unit testing with PHPUnit. (Disclaimer: I am the author of this article series.)

PHPUnit has lots of advanced features that can be amazingly helpful when that special case comes around. This includes extending the framework itself, making test suites, building static datasets, and the focus of this article: annotations and mocking. Don't worry if you're unsure what either of these are; I'm here to help. I trust by the end that you'll see how these two features can be useful to both you and your tests.

The article starts with annotations showing how they can be used both on the class side to help with test generation and on the test side to help in evaluation (like expectedException). In the mocking section, it shows you how create simple and complex mock objects to replace a sample database query.

0 comments voice your opinion now!
tutorial unittest annotation mocking phpunit testing

Devis Lucato's Blog:
Anonymous objects in PHP - Composition, Mocks, Refactoring
November 23, 2010 @ 13:17:53

In a new post to his blog Devis Lucato points out something he noticed when working with objects and anonymous functions/closures - they're not all as they seem.

Both solutions allow to instantiate an anonymous object with properties. They are used as value objects and have no other purpose than storing values, so no logic can be included and they don't come with methods. They can be used as function parameters instead of arrays, for instance. PHP 5.3.0 introduced anonymous functions and closures, so it is now possible to attach functions to these VOs (*). [...] The first thing to notice is that these properties are not methods but callable functions:

In his example, an anonymous function dynamically appended to an object doesn't have access to a property set on the object just one line before. There's a way around it with call_user_func, but it's not practical. His proposed solution is to create a type of Anonymous class that uses the __call method to catch the methods and translate them into calls to call_user_func_array automatically.

0 comments voice your opinion now!
anonymous objects composition mocking refactoring

Community Events

Don't see your event here?
Let us know!

series podcast introduction code community testing laravel unittest list language release install interview opinion framework refactor api threedevsandamaybe wordpress developer

All content copyright, 2014 :: - Powered by the Solar PHP Framework