Mocken

Via Mocking kann sichergestellt werden, dass eine Methode mit den vorgegebenen Argumenten aufgerufen wurde.

<?php
$mock = Mokka::mock(SampleClass::class);

// Verify sure that the method getBar() gets called once
Mokka::verify($mock)->getBar();

An Mokka::verify() kann optional eine Invokation Rule übergeben werden:

<?php
// Verify sure that the method getBar() is never called
Mokka::verify($mock, Mokka::never())->getBar();

// Make sure getBar() gets called at least twice
Mokka::verify($mock, Mokka::atLeast(2)->getBar();

// Make sure getBar() gets called exactly three times
Mokka::verify($mock, Mokka::exactly(3)->getBar();

Eine Methode kann mehrfach mit unterschiedlichen Argumenten gemockt werden:

<?php
// Make sure getBar() gets called once with the argument 'foo' and once with argument 'bar'
Mokka::verify($mock)->getBar('foo');
Mokka::verify($mock)->getBar('bar');

AnythingArgument

Mit dem AnythingArgument ist es nicht nötig, jedes einzelne Argument vorzugeben.

<?php
// Make sure getBar() gets called with the second argument 'foo'. The first argument can be anything.
Mokka::verify($mock)->getBar(Mokka::anything(), 'foo');