Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

phpunit mocks comprehensive documentation

Tags:

php

phpunit

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

like image 470
zerkms Avatar asked Aug 21 '11 10:08

zerkms


4 Answers

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"

like image 86
meze Avatar answered Sep 20 '22 12:09

meze


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)."

like image 45
rkulla Avatar answered Sep 22 '22 12:09

rkulla


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.

like image 31
edorian Avatar answered Sep 19 '22 12:09

edorian


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.

like image 40
Richard Brinkman Avatar answered Sep 20 '22 12:09

Richard Brinkman