Skip to content

Proposition::given()

Jonathan Gjertsen edited this page Jun 18, 2017 · 5 revisions

Syntax

$proposition->given(array|Generator ...$generators);

Add the given $generators to the list of generators which will be used to construct arguments during Proposition::call().

For example, if you pass ONE generator into given(), the $hypothesis that you pass into call($hypothesis) should be a callable which takes a single argument. The value of that argument will be generated by the generator you passed into $given. And if given() is called with TWO generators, the value of $hypothesis should take TWO arguments: the value of the first argument is generated by the first generator, the value of the second argument is generated by the second generator. And so on.

Mixing generators

If you want an argument to be e.g. strings half the time and integers the rest of the time, you can do

$proposition->given([Proposition::integers(), Proposition::letterStrings()]);

This is equivalent to using Proposition::combine().

$proposition->given(Proposition::combine(Proposition::integers(), Proposition::letterStrings()));

Calling given() multiple times

You can of course call given() multiple times if you need to. The generators are simply appended one after the other.

Custom generators

You can provide a custom generator as input, as long as it never returns. Often, it is more convenient to use Proposition::stream() with a callback that returns once.