Author: Sebastian Bergmann (sebastian@phpunit.de)
 /**
  * @param string       $route
  * @param array        $parameters
  * @param string|array $itemRoutes
  * @param array        $itemsRoutesParameters
  * @param boolean      $expected
  * @param boolean      $deprecatedCall
  *
  * @dataProvider provideData
  */
 public function testMatching($route, array $parameters, $itemRoutes, array $itemsRoutesParameters, $expected, $deprecatedCall = false)
 {
     $item = $this->getMock('Knp\\Menu\\ItemInterface');
     $item->expects($this->any())->method('getExtra')->with($this->logicalOr($this->equalTo('routes'), $this->equalTo('routesParameters')))->will($this->returnCallback(function ($parameter) use($itemRoutes, $itemsRoutesParameters) {
         switch ($parameter) {
             case 'routes':
                 return $itemRoutes;
             case 'routesParameters':
                 return $itemsRoutesParameters;
         }
     }));
     $request = new Request();
     $request->attributes->set('_route', $route);
     $request->attributes->set('_route_params', $parameters);
     $voter = new RouteVoter();
     $voter->setRequest($request);
     $deprecatedErrorCatched = false;
     set_error_handler(function ($errorNumber, $message, $file, $line) use(&$deprecatedErrorCatched) {
         if ($errorNumber & E_USER_DEPRECATED) {
             $deprecatedErrorCatched = true;
             return true;
         }
         return \PHPUnit_Util_ErrorHandler::handleError($errorNumber, $message, $file, $line);
     });
     try {
         $this->assertSame($expected, $voter->matchItem($item));
     } catch (\Exception $e) {
         restore_error_handler();
         throw $e;
     }
     restore_error_handler();
     $this->assertSame($deprecatedCall, $deprecatedErrorCatched);
 }
 public function deprecationErrorHandler($errorNumber, $message, $file, $line, $context)
 {
     if ($errorNumber & E_USER_DEPRECATED) {
         return true;
     }
     return \PHPUnit_Util_ErrorHandler::handleError($errorNumber, $message, $file, $line);
 }
Exemple #3
0
 public static function pregMatchSafe($pattern, $subject, $matches = null, $flags = 0, $offset = 0)
 {
     $handler_terminator = PHPUnit_Util_ErrorHandler::handleErrorOnce(E_WARNING);
     $match = preg_match($pattern, $subject, $matches, $flags, $offset);
     $handler_terminator();
     // cleaning
     return $match;
 }
 public static function handleError($level, $message, $file, $line)
 {
     $isFiltered = false;
     if (version_compare(PHPUnit_Runner_Version::id(), '3.5.0') >= 0) {
         $isFiltered = PHP_CodeCoverage::getInstance()->filter()->isFiltered($file, array(), true);
     } else {
         $isFiltered = PHPUnit_Util_Filter::isFiltered($file, true, true);
     }
     if (!$isFiltered) {
         return PHPUnit_Util_ErrorHandler::handleError($level, $message, $file, $line);
     }
 }
 /**
  * Treat any error, which wasn't handled by PHPUnit as a failure
  */
 function _error_handler($errno, $errstr, $errfile, $errline)
 {
     // @ in front of statement
     if (error_reporting() == 0) {
         return;
     }
     // notices and strict warnings are passed on to the phpunit error handler but don't trigger an exception
     if ($errno | $this->_phpunit_err_mask) {
         PHPUnit_Util_ErrorHandler::handleError($errno, $errstr, $errfile, $errline);
     } else {
         error_log("Testing: {$errstr} in {$errfile} on line {$errline}");
         //TODO: we should raise custom exception here, sth like WP_PHPError
         throw new PHPUnit_Framework_Error($errstr, $errno, $errfile, $errline, $trace);
     }
 }
 public function testCreateItemWithRoute()
 {
     $generator = $this->getMock('Symfony\\Component\\Routing\\Generator\\UrlGeneratorInterface');
     $generator->expects($this->once())->method('generate')->with('homepage', array(), false)->will($this->returnValue('/foobar'));
     $deprecatedErrorCatched = false;
     set_error_handler(function ($errorNumber, $message, $file, $line) use(&$deprecatedErrorCatched) {
         if ($errorNumber & E_USER_DEPRECATED) {
             $deprecatedErrorCatched = true;
             return true;
         }
         return \PHPUnit_Util_ErrorHandler::handleError($errorNumber, $message, $file, $line);
     });
     try {
         $factory = new RouterAwareFactory($generator);
     } catch (\Exception $e) {
         restore_error_handler();
         throw $e;
     }
     restore_error_handler();
     $this->assertTrue($deprecatedErrorCatched, 'The RouterAwareFactory throws a E_USER_DEPRECATED when instantiating it.');
     $item = $factory->createItem('test_item', array('uri' => '/hello', 'route' => 'homepage'));
     $this->assertEquals('/foobar', $item->getUri());
 }
 /**
  * @param $level
  * @param $message
  * @param $file
  * @param $line
  */
 public function handleError($level, $message, $file, $line)
 {
     return PHPUnit_Util_ErrorHandler::handleError($level, $message, $file, $line);
 }
 /**
  * Registers and configures the deprecation handler.
  *
  * The following reporting modes are supported:
  * - use "weak" to hide the deprecation report but keep a global count;
  * - use "/some-regexp/" to stop the test suite whenever a deprecation
  *   message matches the given regular expression;
  * - use a number to define the upper bound of allowed deprecations,
  *   making the test suite fail whenever more notices are trigerred.
  *
  * @param int|string|false $mode The reporting mode. Defaults to not allowing any deprecations.
  */
 public static function register($mode = 0)
 {
     if (self::$isRegistered) {
         return;
     }
     if (self::MODE_WEAK !== $mode && (!isset($mode[0]) || '/' !== $mode[0])) {
         $mode = preg_match('/^[1-9][0-9]*$/', $mode) ? (int) $mode : 0;
     }
     $deprecations = array('unsilencedCount' => 0, 'remainingCount' => 0, 'legacyCount' => 0, 'otherCount' => 0, 'unsilenced' => array(), 'remaining' => array(), 'legacy' => array(), 'other' => array());
     $deprecationHandler = function ($type, $msg, $file, $line, $context) use(&$deprecations, $mode) {
         if (E_USER_DEPRECATED !== $type) {
             return \PHPUnit_Util_ErrorHandler::handleError($type, $msg, $file, $line, $context);
         }
         $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS | DEBUG_BACKTRACE_PROVIDE_OBJECT);
         $i = count($trace);
         while (isset($trace[--$i]['class']) && ('ReflectionMethod' === $trace[$i]['class'] || 0 === strpos($trace[$i]['class'], 'PHPUnit_'))) {
             // No-op
         }
         if (isset($trace[$i]['object']) || isset($trace[$i]['class'])) {
             $class = isset($trace[$i]['object']) ? get_class($trace[$i]['object']) : $trace[$i]['class'];
             $method = $trace[$i]['function'];
             if (0 !== error_reporting()) {
                 $group = 'unsilenced';
             } elseif (0 === strpos($method, 'testLegacy') || 0 === strpos($method, 'provideLegacy') || 0 === strpos($method, 'getLegacy') || strpos($class, '\\Legacy') || in_array('legacy', \PHPUnit_Util_Test::getGroups($class, $method), true)) {
                 $group = 'legacy';
             } else {
                 $group = 'remaining';
             }
             if (isset($mode[0]) && '/' === $mode[0] && preg_match($mode, $msg)) {
                 $e = new \Exception($msg);
                 $r = new \ReflectionProperty($e, 'trace');
                 $r->setAccessible(true);
                 $r->setValue($e, array_slice($trace, 1, $i));
                 echo "\n" . ucfirst($group) . ' deprecation triggered by ' . $class . '::' . $method . ':';
                 echo "\n" . $msg;
                 echo "\nStack trace:";
                 echo "\n" . str_replace(' ' . getcwd() . DIRECTORY_SEPARATOR, ' ', $e->getTraceAsString());
                 echo "\n";
                 exit(1);
             }
             if ('legacy' !== $group && self::MODE_WEAK !== $mode) {
                 $ref =& $deprecations[$group][$msg]['count'];
                 ++$ref;
                 $ref =& $deprecations[$group][$msg][$class . '::' . $method];
                 ++$ref;
             }
         } else {
             $group = 'other';
             $ref =& $deprecations[$group][$msg]['count'];
             ++$ref;
         }
         ++$deprecations[$group . 'Count'];
     };
     $oldErrorHandler = set_error_handler($deprecationHandler);
     if (null !== $oldErrorHandler) {
         restore_error_handler();
         if (array('PHPUnit_Util_ErrorHandler', 'handleError') === $oldErrorHandler) {
             restore_error_handler();
             self::register($mode);
         }
     } elseif (!isset($mode[0]) || '/' !== $mode[0]) {
         self::$isRegistered = true;
         if (self::hasColorSupport()) {
             $colorize = function ($str, $red) {
                 $color = $red ? '41;37' : '43;30';
                 return "[{$color}m{$str}";
             };
         } else {
             $colorize = function ($str) {
                 return $str;
             };
         }
         register_shutdown_function(function () use($mode, &$deprecations, $deprecationHandler, $colorize) {
             $currErrorHandler = set_error_handler('var_dump');
             restore_error_handler();
             if ($currErrorHandler !== $deprecationHandler) {
                 echo "\n", $colorize('THE ERROR HANDLER HAS CHANGED!', true), "\n";
             }
             $cmp = function ($a, $b) {
                 return $b['count'] - $a['count'];
             };
             foreach (array('unsilenced', 'remaining', 'legacy', 'other') as $group) {
                 if ($deprecations[$group . 'Count']) {
                     echo "\n", $colorize(sprintf('%s deprecation notices (%d)', ucfirst($group), $deprecations[$group . 'Count']), 'legacy' !== $group), "\n";
                     uasort($deprecations[$group], $cmp);
                     foreach ($deprecations[$group] as $msg => $notices) {
                         echo "\n", rtrim($msg, '.'), ': ', $notices['count'], "x\n";
                         arsort($notices);
                         foreach ($notices as $method => $count) {
                             if ('count' !== $method) {
                                 echo '    ', $count, 'x in ', preg_replace('/(.*)\\\\(.*?::.*?)$/', '$2 from $1', $method), "\n";
                             }
                         }
                     }
                 }
             }
             if (!empty($notices)) {
                 echo "\n";
             }
             if (DeprecationErrorHandler::MODE_WEAK !== $mode && $mode < $deprecations['unsilencedCount'] + $deprecations['remainingCount'] + $deprecations['otherCount']) {
                 exit(1);
             }
         });
     }
 }
 public function testAddViewInlineAction()
 {
     $terminateErrorIgnore = \PHPUnit_Util_ErrorHandler::handleErrorOnce(E_USER_DEPRECATED);
     $this->assertFalse($this->listMapper->has('_action'));
     $this->listMapper->add('_action', 'actions', array('actions' => array('view' => array())));
     $this->assertTrue($this->listMapper->has('_action'));
     $fieldDescription = $this->listMapper->get('_action');
     $this->assertInstanceOf('Sonata\\AdminBundle\\Admin\\FieldDescriptionInterface', $fieldDescription);
     $this->assertEquals('_action', $fieldDescription->getName());
     $this->assertCount(1, $fieldDescription->getOption('actions'));
     $this->assertEquals(array('show' => array()), $fieldDescription->getOption('actions'));
     $terminateErrorIgnore();
 }
Exemple #10
0
 /**
  * Registers a message handler to catch deprecation errors
  *
  * This method is based on \Symfony\Bridge\PhpUnit\DeprecationErrorHandler::register()
  *
  * @return  void
  *
  * @since   3.5
  */
 public static function registerDeprecationHandler()
 {
     if (self::$isDeprecationHandlerRegistered) {
         return;
     }
     $deprecations = array('phpCount' => 0, 'userCount' => 0, 'php' => array(), 'user' => array());
     $deprecationHandler = function ($type, $msg, $file, $line, $context) use(&$deprecations) {
         // Check if the type is E_DEPRECATED or E_USER_DEPRECATED
         if (!in_array($type, array(E_DEPRECATED, E_USER_DEPRECATED))) {
             return PHPUnit_Util_ErrorHandler::handleError($type, $msg, $file, $line, $context);
         }
         $trace = debug_backtrace(PHP_VERSION_ID >= 50400 ? DEBUG_BACKTRACE_IGNORE_ARGS | DEBUG_BACKTRACE_PROVIDE_OBJECT : true);
         $i = count($trace);
         while (isset($trace[--$i]['class']) && ('ReflectionMethod' === $trace[$i]['class'] || 0 === strpos($trace[$i]['class'], 'PHPUnit_'))) {
             // Nothing to do
         }
         $group = $type === E_USER_DEPRECATED ? 'user' : 'php';
         if (isset($trace[$i]['object']) || isset($trace[$i]['class'])) {
             $class = isset($trace[$i]['object']) ? get_class($trace[$i]['object']) : $trace[$i]['class'];
             $method = $trace[$i]['function'];
             $ref =& $deprecations[$group][$msg]['count'];
             $ref++;
             $ref =& $deprecations[$group][$msg][$class . '::' . $method];
             $ref++;
         } else {
             $ref =& $deprecations[$group][$msg]['count'];
             $ref++;
         }
         $deprecations[$group . 'Count']++;
     };
     $oldErrorHandler = set_error_handler($deprecationHandler);
     if (null !== $oldErrorHandler) {
         restore_error_handler();
         if (array('PHPUnit_Util_ErrorHandler', 'handleError') === $oldErrorHandler) {
             restore_error_handler();
             self::register();
         }
     } else {
         self::$isDeprecationHandlerRegistered = true;
         if (self::hasColorSupport()) {
             $colorize = function ($str, $red) {
                 $color = $red ? '41;37' : '43;30';
                 return "[{$color}m{$str}";
             };
         } else {
             $colorize = function ($str) {
                 return $str;
             };
         }
         register_shutdown_function(function () use(&$deprecations, $deprecationHandler, $colorize) {
             $currErrorHandler = set_error_handler('var_dump');
             restore_error_handler();
             if ($currErrorHandler !== $deprecationHandler) {
                 echo "\n", $colorize('THE ERROR HANDLER HAS CHANGED!', true), "\n";
             }
             $cmp = function ($a, $b) {
                 return $b['count'] - $a['count'];
             };
             foreach (array('php', 'user') as $group) {
                 if ($deprecations[$group . 'Count']) {
                     echo "\n", $colorize(sprintf('%s deprecation notices (%d)', ucfirst($group), $deprecations[$group . 'Count']), true), "\n";
                     uasort($deprecations[$group], $cmp);
                     foreach ($deprecations[$group] as $msg => $notices) {
                         echo "\n", rtrim($msg, '.'), ': ', $notices['count'], "x\n";
                         arsort($notices);
                         foreach ($notices as $method => $count) {
                             if ('count' !== $method) {
                                 echo '    ', $count, 'x in ', preg_replace('/(.*)\\\\(.*?::.*?)$/', '$2 from $1', $method), "\n";
                             }
                         }
                     }
                 }
             }
             if (!empty($notices)) {
                 echo "\n";
             }
         });
     }
 }
 public static function register($mode = false)
 {
     if (self::$isRegistered) {
         return;
     }
     $deprecations = array('unsilencedCount' => 0, 'remainingCount' => 0, 'legacyCount' => 0, 'otherCount' => 0, 'unsilenced' => array(), 'remaining' => array(), 'legacy' => array(), 'other' => array());
     $deprecationHandler = function ($type, $msg, $file, $line, $context) use(&$deprecations, $mode) {
         if (E_USER_DEPRECATED !== $type) {
             return \PHPUnit_Util_ErrorHandler::handleError($type, $msg, $file, $line, $context);
         }
         $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS | DEBUG_BACKTRACE_PROVIDE_OBJECT);
         $i = count($trace);
         while (isset($trace[--$i]['class']) && ('ReflectionMethod' === $trace[$i]['class'] || 0 === strpos($trace[$i]['class'], 'PHPUnit_'))) {
             // No-op
         }
         if (0 !== error_reporting()) {
             $group = 'unsilenced';
             $ref =& $deprecations[$group][$msg]['count'];
             ++$ref;
         } elseif (isset($trace[$i]['object']) || isset($trace[$i]['class'])) {
             $class = isset($trace[$i]['object']) ? get_class($trace[$i]['object']) : $trace[$i]['class'];
             $method = $trace[$i]['function'];
             $group = 0 === strpos($method, 'testLegacy') || 0 === strpos($method, 'provideLegacy') || 0 === strpos($method, 'getLegacy') || strpos($class, '\\Legacy') || in_array('legacy', \PHPUnit_Util_Test::getGroups($class, $method), true) ? 'legacy' : 'remaining';
             if ('legacy' !== $group && 'weak' !== $mode) {
                 $ref =& $deprecations[$group][$msg]['count'];
                 ++$ref;
                 $ref =& $deprecations[$group][$msg][$class . '::' . $method];
                 ++$ref;
             }
         } else {
             $group = 'other';
             $ref =& $deprecations[$group][$msg]['count'];
             ++$ref;
         }
         ++$deprecations[$group . 'Count'];
     };
     $oldErrorHandler = set_error_handler($deprecationHandler);
     if (null !== $oldErrorHandler) {
         restore_error_handler();
         if (array('PHPUnit_Util_ErrorHandler', 'handleError') === $oldErrorHandler) {
             restore_error_handler();
             self::register($mode);
         }
     } else {
         self::$isRegistered = true;
         if (self::hasColorSupport()) {
             $colorize = function ($str, $red) {
                 $color = $red ? '41;37' : '43;30';
                 return "[{$color}m{$str}";
             };
         } else {
             $colorize = function ($str) {
                 return $str;
             };
         }
         register_shutdown_function(function () use($mode, &$deprecations, $deprecationHandler, $colorize) {
             $currErrorHandler = set_error_handler('var_dump');
             restore_error_handler();
             if ($currErrorHandler !== $deprecationHandler) {
                 echo "\n", $colorize('THE ERROR HANDLER HAS CHANGED!', true), "\n";
             }
             $cmp = function ($a, $b) {
                 return $b['count'] - $a['count'];
             };
             foreach (array('unsilenced', 'remaining', 'legacy', 'other') as $group) {
                 if ($deprecations[$group . 'Count']) {
                     echo "\n", $colorize(sprintf('%s deprecation notices (%d)', ucfirst($group), $deprecations[$group . 'Count']), 'legacy' !== $group), "\n";
                     uasort($deprecations[$group], $cmp);
                     foreach ($deprecations[$group] as $msg => $notices) {
                         echo "\n", rtrim($msg, '.'), ': ', $notices['count'], "x\n";
                         arsort($notices);
                         foreach ($notices as $method => $count) {
                             if ('count' !== $method) {
                                 echo '    ', $count, 'x in ', preg_replace('/(.*)\\\\(.*?::.*?)$/', '$2 from $1', $method), "\n";
                             }
                         }
                     }
                 }
             }
             if (!empty($notices)) {
                 echo "\n";
             }
             if ('weak' !== $mode && ($deprecations['unsilenced'] || $deprecations['remaining'] || $deprecations['other'])) {
                 exit(1);
             }
         });
     }
 }
Exemple #12
0
 public static function handleError($level, $message, $file, $line)
 {
     if (!PHPUnit_Util_Filter::isFiltered($file, true, true)) {
         return PHPUnit_Util_ErrorHandler::handleError($level, $message, $file, $line);
     }
 }