Exemple #1
0
 public function testSimpliestCase()
 {
     $add = function ($a, $b) {
         return $a + $b;
     };
     $h = new Handler();
     $this->assertEquals(0, $h->getNbTries());
     $this->assertEquals(5, $h->execute($add, array(2, 3)));
     $this->assertEquals(5, $h->getLastResult());
     $this->assertEquals(1, $h->getNbTries());
     $this->assertNull($h->getLastException());
 }
Exemple #2
0
<?php

/**
 * This example shows how it can handle exceptions.
 */
namespace TryAgain\Examples;

use TryAgain\Handler;
use TryAgain\Validator\AnonymousValidator;
require_once __DIR__ . '/../vendor/autoload.php';
function divide($a, $b)
{
    if ($b == 0) {
        throw new \InvalidArgumentException('Cannot divide by zero');
    }
    return $a / $b;
}
$handler = new Handler(new AnonymousValidator(function ($handler) {
    if ($handler->getLastException() instanceof \InvalidArgumentException) {
        $handler->setResult($handler->getLastException()->getMessage());
    }
    return false;
}));
header("Content-Type: text/plain");
$a = 6;
for ($b = -3; $b <= 3; $b++) {
    echo sprintf('%d / %d = %s', $a, $b, $handler->execute('TryAgain\\Examples\\divide', array($a, $b))), PHP_EOL;
}