Page 1 of 1

Yay! Mock: New PHP 5 mock object library

Posted: Sun Mar 16, 2008 6:01 am
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 :)

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

Posted: Sun Mar 16, 2008 1:32 pm
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.

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

Posted: Sun Mar 16, 2008 4:10 pm
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?

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

Posted: Sun Mar 16, 2008 4:13 pm
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.

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

Posted: Sun Mar 16, 2008 4:35 pm
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).

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

Posted: Sun Mar 16, 2008 6:01 pm
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.

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

Posted: Sun Mar 16, 2008 7:51 pm
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.

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

Posted: Mon Mar 17, 2008 8:46 am
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?

:)

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

Posted: Mon Mar 17, 2008 10:05 am
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.

:)

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

Posted: Mon Mar 17, 2008 10:36 am
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.

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

Posted: Mon Mar 17, 2008 10:39 am
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.

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

Posted: Mon Mar 17, 2008 10:43 am
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.

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

Posted: Tue Apr 08, 2008 6:49 am
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*

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

Posted: Tue Apr 08, 2008 10:27 am
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.