<?php
use PHPUnit\Framework\TestCase;
class ExactlyTest extends TestCase
{
public function testCounterCall() : void
{
$counter = $this->getMockBuilder(Counter::CLASS)
->enableProxyingToOriginalMethods()
->getMock();
$counter->expects($this->exactly(3))
->method('increment');
$this->assertEquals(1, $counter->increment());
$this->assertEquals(2, $counter->increment());
$this->assertEquals(3, $counter->increment());
// a 4th call of increment() is not allowed by this test
$this->assertEquals(4, $counter->increment());
}
}
class Counter
{
private $count = 0;
public function increment()
{
return ++$this->count;
}
}
Result
Time: 00:00.007, Memory: 4.00 MB
There was 1 failure:
1) ExactlyTest::testCounterCall
Counter::increment() was not expected to be called more than 3 times.
ExactlyTest.php:19
FAILURES!
Tests: 1, Assertions: 0, Failures: 1.