/** * Executes the operation and evaluates the result * * @param Pdo $pdo * @param callable $operation * * @return mixed */ public function execute(Pdo $pdo, callable $operation) { try { $startTime = microtime(true); $result = call_user_func($operation); $this->stop($startTime, $pdo->errorInfo()); } catch (\PdoException $exception) { $this->stop($startTime, $exception->errorInfo); throw $exception; } return $result; }
function it_can_execute_a_successful_operation(Pdo $pdo) { $result = 'foo'; $errorInfo = [Pdo::ERR_NONE, 'code', 'message']; $operation = function () use($result) { return $result; }; $pdo->errorInfo()->willreturn($errorInfo); $this->execute($pdo, $operation)->shouldReturn($result); $this->time()->shouldBeFloat(); $this->errorCode()->shouldBe($errorInfo[0]); $this->driverErrorCode()->shouldBe($errorInfo[1]); $this->driverErrorMessage()->shouldBe($errorInfo[2]); $this->isOk()->shouldBe(true); }