News Feed
Sections




News Archive
feed this:

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

SitePoint PHP Blog:
Mock your Test Dependencies with Mockery
June 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.

0 comments voice your opinion now!
mock unittest phpunit mockery tool introduction tutorial

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

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

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

Chris Hartjes:
Test Spies and Mockery
December 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.

0 comments voice your opinion now!
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
August 19, 2013 @ 09:49:57

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.

0 comments voice your opinion now!
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
January 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.

0 comments voice your opinion now!
mocks mockery class tutorial phpunit unittest


NetTuts.com:
Mockery A Better Way
November 22, 2012 @ 12:06:02

On NetTuts.com there's a new tutorial posted showing you how to use Mockery in your unit testing as an alternative to the built-in mocking tools of something like PHPUnit.

Mockery is a PHP extension that offers a superior mocking experience, particularly when compared to PHPUnit. While PHPUnit's mocking framework is powerful, Mockery offers a more natural language with a Hamcrest-like set of matchers. In this article, I'll compare the two mocking frameworks and highlight the best features of Mockery.

He walks you through the installation of Mockery and includes the code for a sample PHPUnit test that creates a Mockery instance and mocks out a class object for testing. He moves on to more examples including:

  • Adding expects for method calls (single and multiple)
  • Returning values from the methods
  • Calling multiple methods
  • Returning different things based on parameters
  • Partial mocks
  • Handling constructor parameters
0 comments voice your opinion now!
mockery unittest library phpunit mock object tutorial


Emran Hasan's Blog:
Using Mockery as a mocking framework with PHPUnit
March 26, 2012 @ 14:52:03

Emran Hasan has posted an introduction to using the Mockery mocking tool with PHPUnit for a more powerful, easier to use object mocking system.

A few days ago I was explaining mocking to a few colleagues. I came up with the following quick n dirty code to show them the elegance of Mockery as a mocking framework. The code is straight-forward: it checks the health of a server using a tiny heart beat script.

He provides both sides of the code - a simple "heartbeat" script that just responds with a JSON result, the class to consume it, a HTTP wrapper class that will be the target of the mock and his PHPUnit tests (testSystemIsOnlineWhenServiceRuns). You can see the use of the Mockery tool in this test.

0 comments voice your opinion now!
mockery framework mock object phpunit unittest


Jakub Zalas' Blog:
Mocking Symfony Container services in Behat scenarios with Mockery
January 20, 2012 @ 13:54:52

Jakub Zalas has a recent post to his blog with a hint about how to test Symfony container services by mocking them (when testing with Behat) with the help of Mockery (and the PSSMockeryBundle).

Mocking objects in unit tests is pretty straightforward as every object used in a test case is usually created in a scope of one test class. In functional tests it's a bit harder since we either don't have full control over objects being created or it's simply too laborious to mock half the framework. [...] We're getting the service from a container [in the example] and calling a method which should send a lead. The problem is we don't want to actually call an API while executing Behat scenarios.

Rather than hitting up the API for each test, he opts to create mock objects and results with the tools Mockery has to offer. He gives code for a "is API available" method that either returns a valid container or a mocked object, depending on how it was called.

0 comments voice your opinion now!
mock mockery object behat symfony api tutorial


Wojciech Sznapka's Blog:
Why Mockery is better than PHPUnit Mock Builder (with Symfony2)
November 02, 2011 @ 13:02:06

Wojciech Sznapka has a new post today sharing his opinions as to why Mockery is better than PHPUnit Mock Builder in testing Symfony2-based applications.

Recently I did a lot of Test Driven Development on my Symfony2 bundle. I used PHPUnit's built-in mocks and stubs for many projects, so I took it again. But while I was working on mocking Symfony2 core objects I found those mocks very uncomfortable in use. I tried Mockery and it saved my day. Let's see how to get it working with Symfony2 and how it kicks ass!

He shows how to get things set up - adding Mockery to the dependencies file (deps), getting the latest version from their git repository and an example mock method that shows the difference in mocking the Doctrine2 entity manager - PHPUnit vs Mockery.

0 comments voice your opinion now!
mockery phpunit mock object unittest


DZone.com:
A Mockery Review
May 09, 2011 @ 13:05:14

On the Web Builder Zone (a part of DZone.com) there's a recent post from Giorgio Sironi reviewing the Mockery library, a mock object framework created by Padraic Brady.

Mockery is a mock object framework (more properly Test Double framework) from @padraicb, independent from testing frameworks like PHPUnit. It can be used to quickly prepare Mocks, Stubs and other Test Doubles to use inside your unit tests. I've tried Mockery via a PEAR installation and I must say its expressive power is higher than that of PHPUnit mocking system. However, it may be too powerful for effective usage.

He talks about some of the features that are in Mockery that aren't in PHPUnit like alternative expectations, recording of the expected calls and the mocking of non-existent methods. He notes that it can be too overwhelming at times, though, with so many features that may or may not be useful for the large majority of testers. He includes some sample code showing a set of five tests on a simple class implementing an interface.

0 comments voice your opinion now!
mockery testdoubles unittest mock object



Community Events





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


introduction interview list threedevsandamaybe library series podcast code release language application developer configure unittest wordpress api community laravel framework install

All content copyright, 2014 PHPDeveloper.org :: info@phpdeveloper.org - Powered by the Solar PHP Framework