Home » Php » php – phpunit mocks comprehensive documentation

php – phpunit mocks comprehensive documentation

Posted by: admin July 12, 2020 Leave a comment

Questions:

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

How to&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”

Answer:

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

Answer:

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.

Answer:

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.

Answer:

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