コード例 #1
0
ファイル: Assert.php プロジェクト: jakuborava/walletapp
 /**
  * Checks if the function generates PHP error or throws exception.
  * @param  callable
  * @param  int|string|array
  * @param  string message
  * @return null|Exception
  */
 public static function error($function, $expectedType, $expectedMessage = NULL)
 {
     if (is_string($expectedType) && !preg_match('#^E_[A-Z_]+\\z#', $expectedType)) {
         return static::exception($function, $expectedType, $expectedMessage);
     }
     $expected = is_array($expectedType) ? $expectedType : array(array($expectedType, $expectedMessage));
     foreach ($expected as &$item) {
         list($expectedType, $expectedMessage) = $item;
         if (is_int($expectedType)) {
             $item[2] = Helpers::errorTypeToString($expectedType);
         } elseif (is_string($expectedType)) {
             $item[0] = constant($item[2] = $expectedType);
         } else {
             throw new \Exception('Error type must be E_* constant.');
         }
     }
     set_error_handler(function ($severity, $message, $file, $line) use(&$expected) {
         if (($severity & error_reporting()) !== $severity) {
             return;
         }
         $errorStr = Helpers::errorTypeToString($severity) . ($message ? " ({$message})" : '');
         list($expectedType, $expectedMessage, $expectedTypeStr) = array_shift($expected);
         if ($expectedType === NULL) {
             restore_error_handler();
             Assert::fail("Generated more errors than expected: {$errorStr} was generated in file {$file} on line {$line}");
         } elseif ($severity !== $expectedType) {
             restore_error_handler();
             Assert::fail("{$expectedTypeStr} was expected, but {$errorStr} was generated in file {$file} on line {$line}");
         } elseif ($expectedMessage && !Assert::isMatching($expectedMessage, $message)) {
             restore_error_handler();
             Assert::fail("{$expectedTypeStr} with a message matching %2 was expected but got %1", $message, $expectedMessage);
         }
     });
     reset($expected);
     call_user_func($function);
     restore_error_handler();
     if ($expected) {
         self::fail('Error was expected, but was not generated');
     }
 }
コード例 #2
0
ファイル: Assert.php プロジェクト: richard-ejem/tester
 /**
  * Checks if the function does neither generate PHP error nor throw Exception
  * @param  callable
  * @param  string
  * @return null|\Exception
  */
 public static function noError($function, $message = NULL)
 {
     self::$counter++;
     set_error_handler(function ($severity, $msg, $file, $line) use(&$expected, $message) {
         if (($severity & error_reporting()) !== $severity) {
             return;
         }
         $errorStr = Helpers::errorTypeToString($severity) . ($msg ? " ({$msg})" : '');
         restore_error_handler();
         Assert::fail(($message ? "{$message}: " : '') . "occurred error {$errorStr}");
     });
     try {
         call_user_func($function);
     } catch (\Exception $e) {
         if ($e instanceof AssertException) {
             // assertion exception already thrown by error handler, re-throw
             throw $e;
         }
         restore_error_handler();
         Assert::fail(($message ? "{$message}: " : '') . sprintf('%s was thrown. Code: %d Message: %s', get_class($e), $e->getCode(), $e->getMessage()));
     }
     restore_error_handler();
 }