getMockBuilder(RetryStrategyInterface::class)->getMock(); $strategy ->expects($this->exactly(3)) ->method('shouldRetry') ->will($this->returnCallback(function ($e, $c) use ($exception, &$count) { $this->assertSame($e, $exception); $this->assertEquals($c + 1, $count); return true; })); $retry = new CommandRetry($strategy, 5); $this->assertEquals(4, $retry->run($action)); } /** * Test attempts exceeded * * @return void */ public function testExceedAttempts() { $exception = new Exception('this is failing'); $action = function () use ($exception) { throw $exception; }; $strategy = $this->getMockBuilder(RetryStrategyInterface::class)->getMock(); $strategy ->expects($this->exactly(4)) ->method('shouldRetry') ->will($this->returnCallback(function ($e) use ($exception) { return true; })); $retry = new CommandRetry($strategy, 3); $this->expectException(Exception::class); $this->expectExceptionMessage('this is failing'); $retry->run($action); } /** * Test that the strategy is respected * * @return void */ public function testRespectStrategy() { $action = function () { throw new Exception('this is failing'); }; $strategy = $this->getMockBuilder(RetryStrategyInterface::class)->getMock(); $strategy ->expects($this->once()) ->method('shouldRetry') ->will($this->returnCallback(function () { return false; })); $retry = new CommandRetry($strategy, 3); $this->expectException(Exception::class); $this->expectExceptionMessage('this is failing'); $retry->run($action); } }