})->run(function () { return 3; }); expect($total_tries)->to->be(5); expect($stubborn->totalTries())->to->be(3); expect($result)->to->be(2); }); }); describe('->exceptionHandler()', function ($test) { it('should make Stubborn retry 3 times', function ($test) { $exception = null; $e_type = 'Stubborn\\Tests\\StubbornTestException'; $rHandler = function ($stubborn) use($e_type) { if ($stubborn->retries() < $stubborn->maxRetries()) { throw new $e_type(); } }; $eHandler = function ($stubborn) use(&$exception) { $exception = $stubborn->exception(); $stubborn->retry(); }; $stubborn = new Stubborn(); $result = $stubborn->retries(3)->exceptionHandler($eHandler)->resultHandler($rHandler)->run(function () { return 'Boosh'; }); expect(is_a($exception, $e_type))->to->be(true); expect($stubborn->totalTries())->to->be(4); expect($result)->to->be('Boosh'); }); }); });
/** * Tests that when the ->run returns a Stubburn response object it will correctly return it and end the loop */ public function testResponseStubbornMockZeroRetries() { $stubbornAwareObject = $this->getStubbornMock(); $stubbornResponseObject = $this->getStubbornResponseMock(); $stubbornResponseObject->expects($this->once())->method('getData')->will($this->returnValue('{"status":"true"}')); $stubbornResponseObject->expects($this->once())->method('getHttpCode')->will($this->returnValue(200)); // Run it 4 times $stubbornAwareObject->method('getRetryNumber')->will($this->returnValue(5)); $stubbornAwareObject->method('getHttpActionRequest')->will($this->returnValue(false)); $stubbornAwareObject->method('getExceptionActionRequest')->will($this->returnValue(false)); $stubbornAwareObject->method('getRetryWaitSeconds')->will($this->returnValue(0)); $stubbornAwareObject->expects($this->once())->method('run')->will($this->returnValue($stubbornResponseObject)); $stubborn = new Stubborn($stubbornAwareObject); $response = $stubborn->run(); $this->assertInstanceOf('Stubborn\\StubbornResponseInterface', $response); $this->assertEquals(0, $response->getRetryCount()); $this->assertEquals('{"status":"true"}', $response->getData()); $this->assertEquals(200, $response->getHttpCode()); }