7

There is a description of what are the mocks and how to work with them: http://www.phpunit.de/manual/current/en/test-doubles.html

But there is no comprehensive explanation of all possible methods such as $this->any(), $this->once(), $this->equalTo() etc

So I'm looking for Mock generation API

PS: yes, I know I can look it in the sources, but I'm looking for an online API document

2
  • I'm afraid there isn't any. Been looking for it myself and ended up by looking through source and trial and error (and also frustration)
    – Mchl
    Aug 21, 2011 at 12:03
  • @Mchl: shame on us - there is one ;-) A person pointed me to it will post an answer in a moment.
    – zerkms
    Aug 21, 2011 at 12:05

5 Answers 5

5

There's no all API description in one place, but a part of api described under the link you gave. Another part is avaiable in "Writing Tests for PHPUnit"

1
2

Sebastian Bergmann's book "Real-World Solutions For Developing High-Quality PHP Frameworks and Applications" goes into more detail but mostly through examples. The good thing about well named methods is they don't need comprehensive documentation. You just need to understand the concepts of all this stuff first. Hint, equalTo() is often used inside custom asserts, e.g., $this->assertThat($foo, $this->logicalNot($this->equalTo($bar))); or more commonly inside expectations like ->with($this->equalTo('something')).

The main concepts are documented well, I've rarely had to dig into the source code. For example, with() "Specifies assertions to make on the param passed. In simplest cases, call it w/ the val you'd use to call the method, in identical order. Params include: $this->anything(), $this->greaterThan($n), $this->stringContains($str), isTrue(), isFalse(), isNull(), equalTo(), etc." Whereas, once() is called from within expects() and expects() "registers a new expectation, such as how many times the method, specified in method(), is expected to be called. Params can be: $this->exactly($count), once(), atLeastOnce(), never(), any(), or at($index)."

1
  • Yeah, I understand the concepts, I just needed the list of all that assertions. Thank you
    – zerkms
    Aug 21, 2011 at 22:12
2

With this part of phpunit, saidly (documentation patches can be provided on github), the source is the documentation.

A list of PHPUnit assertions

The list of all functions you use in mock objects

Those functions all return some sort of PHPUnit_Framework_Constraint that you could also use directly (though usage through the API is the preferred way). Those are mostly the same constraints as described in "Writing Tests for PHPUnit". The names are the same for most constraints like ->fileExists but some differ (_or vs ->logicalOr()) but you can look those up quite quickly in the source.

2

I was wondering myself, why the phpunit website itself isn't publishing the complete API documentation. Instead they only give examples. Therefore I downloaded the phpunit source and ran phpdoc over. I published the generated API documentation on http://richardbrinkman.hol.es/phpunit/4.5.0/ so that others does not have to go through the same hassle.

0
2

I have just generated the PHPUnit API documentation with Sami and published on GitHub Pages using Travis.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.