Yay! Mock: New PHP 5 mock object library

Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy.
This forum is not for asking programming related questions.

Moderator: General Moderators

Post Reply
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Yay! Mock: New PHP 5 mock object library

Post by Chris Corbyn »

This is a bit hot of the press (to the point it only has a home on Google Code), but I'd like to announce the release of the first beta for a new mock object library for PHP 5. It's entirely standalone so will work with any testing framework without conflict.

For anyone who's ever used jMock it will be instantly recognisable since the API is almost 100% taken from jMock (think of it as a jMock clone), but tailored to work well with PHP.

You'll need PHP 5.2 (EDIT | 5.1.2) or higher with the SPL extension to use it.

http://code.google.com/p/yaymock/ (will get a home on it's own website eventually)

This is the first release so any bugs should be posted on the Issue Tracker.

The Documentation is already pretty concise but I'll work on that more.

Differences from SimpleTest:

* Expectations based on method invocations not on timings
* The mock objects do not have any additional methods inside them (they really only have the mocked methods and no more)
* Expectations can be constrained to specific states
* PHP5 compliant

Current disadvantages over SimpleTest:

* The error messages are effectively a log dump of what happened, but do not describe parameters currently
* There is no partial mock support in this release

The outstanding issues will be addressed asap, but for now I just wish to get some people using it to pick up logic errors rather than features. I actually don't belive there are any bugs with this version since it's actually very simple the way it all works, but I'd like to address anything that does come to light before a non-beta release.

Feedback most welcome :)
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Re: Yay! Mock: New PHP 5 mock object library

Post by Ambush Commander »

Argh, PHP 5.2 kills it for me, unfortunately. I need PHP 5.0-5.3 support; otherwise, I would definitely look into it.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Yay! Mock: New PHP 5 mock object library

Post by Chris Corbyn »

I haven't yet tested it on less than 5.2. 5.1 may work, but 5.0 probably won't I doubt. Something for me to think about :)

Did 5.0 have SPL by default, along with Reflection?
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Re: Yay! Mock: New PHP 5 mock object library

Post by Ambush Commander »

They had SPL/Reflection, but some of the methods were a little flaky. Also, SPL can be turned off (though Reflection, as far as I can tell, can't). 5.1 is a pretty safe version to target; 5.0 is widely regarded as a lost cause, although previously PHP 4 only projects usually work in that version too.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Yay! Mock: New PHP 5 mock object library

Post by Chris Corbyn »

Ok, I'll have a play after work and see if it work (or if it would be much effort to make it work) with 5.0. I'm not sure when nullable type hints got introduced too.

Opening this up to out of date projects was never really an intention so PHP4 is a complete no-go, but PHP 5.0 is probably on the boundaries of how far back I'd go (Only if it won't mean considerable code changes).
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Re: Yay! Mock: New PHP 5 mock object library

Post by Luke »

Right on. I'll put it to use in a few days here when I get back into programming mode. :) I just wrapped up a couple pretty large personal projects and I need a break. I'll let you know how it goes.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Yay! Mock: New PHP 5 mock object library

Post by Chris Corbyn »

I just built 5.1.4 (ended up with the damn CGI and no CLI :x ) then ran the tests on command line using the "-q" option so it behaves as CLI and they all pass so 5.1.4 is a yes. I'll amend my docs. Building 5.0.4 now.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Yay! Mock: New PHP 5 mock object library

Post by Chris Corbyn »

Hmm, doesn't appear this has stirred as much interest as I might have hoped. McGruff over at SitePoint suggested trimming down the "returnValue" parts of the API, so I will add the action wrappers:

-> returns($value)
-> returnsReference($ref)
-> throws($e)

Then code like this:

Code: Select all

$context->checking(Yay_Expectations::create()
  ->atLeast(3)->of($mock)->something() -> will(Yay::returnValue(99))
  ->one($otherMock)->doAnother($x) -> will(Yay::throwException($e))
  );
 
Becomes (optionally):

Code: Select all

$context->checking(Yay_Expectations::create()
  ->atLeast(3)->of($mock)->something() -> returns(99)
  ->one($otherMock)->doAnother($x) -> throws($e)
  );
Ambush, I installed 5.0.4 and it didn't work (spl_autoload_register()) didn't exist then. That's obviously not a show-stopper so I'll look at it again tomorrow, but I wouldn't be surprised if 5.0.x is riddled with issues. 5.1.2 is the earliest I know it works in its current state (I'm happy with that).

With regards to lack of interest, maybe I'm just being too quick to expect responses, but:

a) Who's read this thread and being genuinely uninterested?
b) Who's read this thread and not known what it's all about? (That's fine, it's a very specific subject area)
c) Who's read this thread and knows what it's about but don't unit test so have no use for it?
d) Who's read this thread and would rather use something else?

:)
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Re: Yay! Mock: New PHP 5 mock object library

Post by Jenk »

In answer to your questions, I am a mix of C and D, with a tiny bit of A as I rarely use PHP any more. I am also unfamiliar with jMock, but more familiar with PHPSpec (note: I assume I would be - I haven't even looked at the code base for a number of months now) and it's Mock objects, as my usage of SSpec has been intensive.

:)
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Yay! Mock: New PHP 5 mock object library

Post by RobertGonzalez »

I have read the thread and visited the current home of the project.

I think I need to understand how this will benefit me as a developer coming from either a "new to unit testing" background or a "I love SimpleTest/PHPUnit so why should I use this" background.

Of course I am partial to just about any code you produce Chris. It is always very elegant and very logical. I just want to know why I should use it. Swift? That is a no brainer. Yay!? Well, I am not so sure just yet.
matthijs
DevNet Master
Posts: 3360
Joined: Thu Oct 06, 2005 3:57 pm

Re: Yay! Mock: New PHP 5 mock object library

Post by matthijs »

Am reading this thread but since I've barely begun TDD-ing I'd have to dive into the documentation to understand what it does, howit works and how it's useful to me. Anything that'll allow me to write better code, I find interesting.

I started with Simpletest a while ago, gave up because I found it too cumbersome, didn't help me (read: I probably did something wrong) and lack of examples/tutorials. Lately I've read a bit about how simpletest is "out" and that Spec is the future. And now you come with Yay Mock... hard to keep up for us lesser php-ers.. :?

But I'll study the docs, I am interested.
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Re: Yay! Mock: New PHP 5 mock object library

Post by Zoxive »

Chris Corbyn wrote:With regards to lack of interest, maybe I'm just being too quick to expect responses, but:

a) Who's read this thread and being genuinely uninterested?
b) Who's read this thread and not known what it's all about? (That's fine, it's a very specific subject area)
c) Who's read this thread and knows what it's about but don't unit test so have no use for it?
d) Who's read this thread and would rather use something else?

:)
a) I am interested, but lack the knowledge of unit testing to join in.
c) I have a general idea of Unit testing, but have never played around with it to understand it. This is something I want to look into and learn.
I think its because I haven't really made some super complex classes(of classes) in which simple testing wouldn't suffice.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Yay! Mock: New PHP 5 mock object library

Post by Chris Corbyn »

I've simplified the API a little and amended the docs accordingly.

Since the documentation never really elaborated on what mocks are for, I've also added a tutorial. The tutorial does assume you have at least a basic idea of how unit testing works however. I can add more tutorial stuff if requested.

http://code.google.com/p/yaymock/wiki/U ... t_Tutorial

PS: Sorry for the blatant *bump*
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Yay! Mock: New PHP 5 mock object library

Post by RobertGonzalez »

I think you should write tutorials for a living Chris. That one was really good. I think I might be giving Yay and play around in a couple of days.
Post Reply