public function testForcedResult() { $validator = m::mock('TryAgain\\ValidatorInterface'); $validator->shouldReceive('mustRetry')->andReturnUsing(function ($handler) { $handler->setResult('FORCED_RESULT'); return false; }); $func = function () { throw new \Exception(); }; $h = new Handler($validator); $this->assertEquals('FORCED_RESULT', $h->execute($func)); $this->assertEquals('FORCED_RESULT', $h->getLastResult()); }
public function mustRetry(Handler $handler) { $mustRetry = false; $written = $handler->getLastResult(); list($fh, $text) = $handler->getArguments(); if ($written === false) { // we try 3 times before abandoning $mustRetry = ++$this->nbErrors <= 3; } else { $this->written += $written; if ($written !== strlen($text)) { $mustRetry = true; $handler->setArguments(array($fh, substr($text, $written))); } else { // we force the result with the total written length $handler->setResult($this->written); } } return $mustRetry; }