/** * Returns the value of an object's attribute. * This also works for attributes that are declared protected or private. * * @param object $object * @param string $attributeName * @return mixed * @throws InvalidArgumentException * @access public * @static * @since Method available since Release 3.1.0 */ public static function getObjectAttribute($object, $attributeName) { if (!is_object($object) || !is_string($attributeName)) { throw new InvalidArgumentException(); } self::assertObjectHasAttribute($attributeName, $object); if (property_exists($object, $attributeName)) { return $object->{$attributeName}; } else { $array = (array) $object; $protectedName = "*" . $attributeName; if (array_key_exists($protectedName, $array)) { return $array[$protectedName]; } else { $classes = PHPUnit_Util_Class::getHierarchy(get_class($object)); foreach ($classes as $class) { $privateName = sprintf("%s%s", $class, $attributeName); if (array_key_exists($privateName, $array)) { return $array[$privateName]; } } } } throw new RuntimeException(sprintf('Attribute "%s" not found in object.', $attributeName)); }
/** * Constructor. * * @param ReflectionClass $class * @param array $codeCoverage */ protected function __construct(ReflectionClass $class, &$codeCoverage = array()) { $this->class = $class; $className = $class->getName(); $packageInformation = PHPUnit_Util_Class::getPackageInformation($className); if (!empty($packageInformation['fullPackage'])) { $this->package = $packageInformation['fullPackage']; } $this->setCoverage($codeCoverage); $this->dit = count(PHPUnit_Util_Class::getHierarchy($class->getName())) - 1; $this->impl = count($class->getInterfaces()); foreach ($this->class->getMethods() as $method) { if ($method->getDeclaringClass()->getName() == $className) { $this->methods[$method->getName()] = PHPUnit_Util_Metrics_Function::factory($method, $codeCoverage); } else { $this->inheritedMethods[$method->getName()] = PHPUnit_Util_Metrics_Function::factory($method, $codeCoverage); } } $this->calculateAttributeMetrics(); $this->calculateMethodMetrics(); $this->calculateNumberOfChildren(); $this->calculatePolymorphismFactor(); $this->calculateDependencies(); }
/** * Returns the value of an object's attribute. * This also works for attributes that are declared protected or private. * * @param object $object * @param string $attributeName * @return mixed * @throws InvalidArgumentException * @since Method available since Release 3.1.0 */ public static function getObjectAttribute($object, $attributeName) { if (!is_object($object) || !is_string($attributeName)) { throw new InvalidArgumentException(); } self::assertObjectHasAttribute($attributeName, $object); try { $attribute = new ReflectionProperty($object, $attributeName); } catch (ReflectionException $e) { // Workaround for http://bugs.php.net/46064 if (version_compare(PHP_VERSION, '5.2.7', '<')) { $reflector = new ReflectionObject($object); $attributes = $reflector->getProperties(); foreach ($attributes as $_attribute) { if ($_attribute->getName() == $attributeName) { $attribute = $_attribute; break; } } } $reflector = new ReflectionObject($object); while ($reflector = $reflector->getParentClass()) { try { $attribute = $reflector->getProperty($attributeName); break; } catch (ReflectionException $e) { } } } if ($attribute->isPublic()) { return $object->{$attributeName}; } else { $array = (array) $object; $protectedName = "*" . $attributeName; if (array_key_exists($protectedName, $array)) { return $array[$protectedName]; } else { $classes = PHPUnit_Util_Class::getHierarchy(get_class($object)); foreach ($classes as $class) { $privateName = sprintf("%s%s", $class, $attributeName); if (array_key_exists($privateName, $array)) { return $array[$privateName]; } } } } throw new RuntimeException(sprintf('Attribute "%s" not found in object.', $attributeName)); }
/** * Runs a TestCase. * * @param PHPUnit_Framework_Test $test */ public function run(PHPUnit_Framework_Test $test) { PHPUnit_Framework_Assert::resetCount(); $error = FALSE; $failure = FALSE; $incomplete = FALSE; $skipped = FALSE; $this->startTest($test); $errorHandlerSet = FALSE; if ($this->convertErrorsToExceptions) { $oldErrorHandler = set_error_handler(array('PHPUnit_Util_ErrorHandler', 'handleError'), E_ALL | E_STRICT); if ($oldErrorHandler === NULL) { $errorHandlerSet = TRUE; } else { restore_error_handler(); } } if (self::$xdebugLoaded === NULL) { self::$xdebugLoaded = extension_loaded('xdebug'); self::$useXdebug = self::$xdebugLoaded; } $useXdebug = self::$useXdebug && $this->codeCoverage !== NULL && !$test instanceof PHPUnit_Extensions_SeleniumTestCase && !$test instanceof PHPUnit_Framework_Warning; if ($useXdebug) { // We need to blacklist test source files when no whitelist is used. if (!$this->codeCoverage->filter()->hasWhitelist()) { $classes = PHPUnit_Util_Class::getHierarchy(get_class($test), TRUE); foreach ($classes as $class) { $this->codeCoverage->filter()->addFileToBlacklist($class->getFileName()); } } $this->codeCoverage->start($test); } PHP_Timer::start(); try { if (!$test instanceof PHPUnit_Framework_Warning && $this->strictMode && extension_loaded('pcntl') && class_exists('PHP_Invoker')) { switch ($test->getSize()) { case PHPUnit_Util_Test::SMALL: $_timeout = $this->timeoutForSmallTests; break; case PHPUnit_Util_Test::MEDIUM: $_timeout = $this->timeoutForMediumTests; break; case PHPUnit_Util_Test::LARGE: $_timeout = $this->timeoutForLargeTests; break; } $invoker = new PHP_Invoker(); $invoker->invoke(array($test, 'runBare'), array(), $_timeout); } else { $test->runBare(); } } catch (PHPUnit_Framework_AssertionFailedError $e) { $failure = TRUE; if ($e instanceof PHPUnit_Framework_IncompleteTestError) { $incomplete = TRUE; } else { if ($e instanceof PHPUnit_Framework_SkippedTestError) { $skipped = TRUE; } } } catch (Exception $e) { $error = TRUE; } $time = PHP_Timer::stop(); $test->addToAssertionCount(PHPUnit_Framework_Assert::getCount()); if ($this->strictMode && $test->getNumAssertions() == 0) { $incomplete = TRUE; } if ($useXdebug) { try { $this->codeCoverage->stop(!$incomplete && !$skipped); } catch (PHP_CodeCoverage_Exception $cce) { $error = TRUE; if (!isset($e)) { $e = $cce; } } } if ($errorHandlerSet === TRUE) { restore_error_handler(); } if ($error === TRUE) { $this->addError($test, $e, $time); } else { if ($failure === TRUE) { $this->addFailure($test, $e, $time); } else { if ($this->strictMode && $test->getNumAssertions() == 0) { $this->addFailure($test, new PHPUnit_Framework_IncompleteTestError('This test did not perform any assertions'), $time); } else { if ($this->strictMode && $test->hasOutput()) { $this->addFailure($test, new PHPUnit_Framework_OutputError(sprintf('This test printed output: %s', $test->getActualOutput())), $time); } } } } $this->endTest($test, $time); }
protected function generateConstructorCode(ReflectionClass $class) { $arguments = ''; $constructor = $class->getConstructor(); if ($constructor !== NULL) { $constructorName = $constructor->getName(); foreach (PHPUnit_Util_Class::getHierarchy($class->getName(), TRUE) as $_class) { foreach ($_class->getInterfaces() as $interface) { if ($interface->hasMethod($constructorName)) { $arguments = PHPUnit_Util_Class::getMethodParameters($constructor); break 2; } } } } return sprintf(" public function __construct(%s) {\n" . " \$this->invocationMocker = new PHPUnit_Framework_MockObject_InvocationMocker(\$this);\n" . " }\n\n", $arguments); }