Skip to content
Permalink
Browse files

Cleanup

  • Loading branch information...
sebastianbergmann committed May 21, 2019
1 parent 35253e0 commit 41a52603095306e5e122d319985862367c985670
Showing with 142 additions and 44 deletions.
  1. +3 −2 src/Framework/TestCase.php
  2. +135 −39 src/Util/Test.php
  3. +4 −2 src/Util/TestDox/NamePrettifier.php
  4. +0 −1 tests/unit/Util/TestTest.php
@@ -32,6 +32,7 @@
use PHPUnit\Framework\MockObject\Stub\ReturnValueMap as ReturnValueMapStub;
use PHPUnit\Runner\BaseTestRunner;
use PHPUnit\Runner\PhptTestCase;
use PHPUnit\Util\Exception as UtilException;
use PHPUnit\Util\GlobalState;
use PHPUnit\Util\PHP\AbstractPhpProcess;
use PHPUnit\Util\Test as TestUtil;
@@ -673,7 +674,7 @@ public function hasFailed(): bool
* If no TestResult object is passed a new one will be created.
*
* @throws CodeCoverageException
* @throws \PHPUnit\Util\Exception
* @throws UtilException
* @throws \SebastianBergmann\CodeCoverage\CoveredCodeNotExecutedException
* @throws \SebastianBergmann\CodeCoverage\InvalidArgumentException
* @throws \SebastianBergmann\CodeCoverage\MissingCoversAnnotationException
@@ -1717,7 +1718,7 @@ private function setExpectedExceptionFromAnnotation(): void
$this->expectExceptionMessageRegExp($expectedException['message_regex']);
}
}
} catch (\ReflectionException $e) {
} catch (UtilException $e) {
}
}
@@ -12,20 +12,14 @@
use PharIo\Version\VersionConstraintParser;
use PHPUnit\Framework\Assert;
use PHPUnit\Framework\CodeCoverageException;
use PHPUnit\Framework\Exception;
use PHPUnit\Framework\InvalidCoversTargetException;
use PHPUnit\Framework\InvalidDataProviderException;
use PHPUnit\Framework\SelfDescribing;
use PHPUnit\Framework\SkippedTestError;
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\Warning;
use PHPUnit\Runner\Version;
use ReflectionClass;
use ReflectionException;
use ReflectionFunction;
use ReflectionMethod;
use SebastianBergmann\Environment\OperatingSystem;
use Traversable;
/**
* @internal This class is not covered by the backward compatibility promise for PHPUnit
@@ -180,21 +174,37 @@ public static function requiresCodeCoverageDataCollection(TestCase $test): bool
}
/**
* Returns the requirements for a test.
*
* @throws Warning
* @throws Exception
*/
public static function getRequirements(string $className, string $methodName): array
{
$reflector = new ReflectionClass($className);
try {
$reflector = new \ReflectionClass($className);
} catch (\ReflectionException $e) {
throw new Exception(
$e->getMessage(),
(int) $e->getCode(),
$e
);
}
$requires = [
'__OFFSET' => [
'__FILE' => \realpath($reflector->getFileName()),
],
];
$requires = self::parseRequirements((string) $reflector->getDocComment(), $reflector->getStartLine(), $requires);
$reflector = new ReflectionMethod($className, $methodName);
try {
$reflector = new \ReflectionMethod($className, $methodName);
} catch (\ReflectionException $e) {
throw new Exception(
$e->getMessage(),
(int) $e->getCode(),
$e
);
}
return self::parseRequirements((string) $reflector->getDocComment(), $reflector->getStartLine(), $requires);
}
@@ -407,7 +417,16 @@ public static function getMissingRequirements(string $className, string $methodN
*/
public static function getExpectedException(string $className, string $methodName)
{
$reflector = new ReflectionMethod($className, $methodName);
try {
$reflector = new \ReflectionMethod($className, $methodName);
} catch (\ReflectionException $e) {
throw new Exception(
$e->getMessage(),
(int) $e->getCode(),
$e
);
}
$docComment = (string) $reflector->getDocComment();
$docComment = (string) \substr($docComment, 3, -2);
@@ -462,11 +481,19 @@ public static function getExpectedException(string $className, string $methodNam
* Returns the provided data for a method.
*
* @throws Exception
* @throws ReflectionException
*/
public static function getProvidedData(string $className, string $methodName): ?array
{
$reflector = new ReflectionMethod($className, $methodName);
try {
$reflector = new \ReflectionMethod($className, $methodName);
} catch (\ReflectionException $e) {
throw new Exception(
$e->getMessage(),
(int) $e->getCode(),
$e
);
}
$docComment = (string) $reflector->getDocComment();
$data = self::getDataFromDataProviderAnnotation($docComment, $className, $methodName);
@@ -538,7 +565,16 @@ public static function getDataFromTestWithAnnotation(string $docComment): ?array
public static function parseTestMethodAnnotations(string $className, ?string $methodName = ''): array
{
if (!isset(self::$annotationCache[$className])) {
$class = new ReflectionClass($className);
try {
$class = new \ReflectionClass($className);
} catch (\ReflectionException $e) {
throw new Exception(
$e->getMessage(),
(int) $e->getCode(),
$e
);
}
$traits = $class->getTraits();
$annotations = [];
@@ -559,9 +595,9 @@ public static function parseTestMethodAnnotations(string $className, ?string $me
if ($methodName !== null && !isset(self::$annotationCache[$cacheKey])) {
try {
$method = new ReflectionMethod($className, $methodName);
$method = new \ReflectionMethod($className, $methodName);
$annotations = self::parseAnnotations((string) $method->getDocComment());
} catch (ReflectionException $e) {
} catch (\ReflectionException $e) {
$annotations = [];
}
@@ -576,7 +612,16 @@ public static function parseTestMethodAnnotations(string $className, ?string $me
public static function getInlineAnnotations(string $className, string $methodName): array
{
$method = new ReflectionMethod($className, $methodName);
try {
$method = new \ReflectionMethod($className, $methodName);
} catch (\ReflectionException $e) {
throw new Exception(
$e->getMessage(),
(int) $e->getCode(),
$e
);
}
$code = \file($method->getFileName());
$lineNumber = $method->getStartLine();
$startLine = $method->getStartLine() - 1;
@@ -761,7 +806,7 @@ public static function getHookMethods(string $className): array
self::$hookMethods[$className] = self::emptyHookMethodsArray();
try {
foreach ((new ReflectionClass($className))->getMethods() as $method) {
foreach ((new \ReflectionClass($className))->getMethods() as $method) {
if ($method->getDeclaringClass()->getName() === Assert::class) {
continue;
}
@@ -770,7 +815,9 @@ public static function getHookMethods(string $className): array
continue;
}
if ($methodComment = $method->getDocComment()) {
$methodComment = $method->getDocComment();
if ($methodComment) {
if ($method->isStatic()) {
if (\strpos($methodComment, '@beforeClass') !== false) {
\array_unshift(
@@ -796,7 +843,7 @@ public static function getHookMethods(string $className): array
}
}
}
} catch (ReflectionException $e) {
} catch (\ReflectionException $e) {
}
}
@@ -892,8 +939,7 @@ private static function parseAnnotationContent(string $message): string
}
/**
* @throws \ReflectionException
* @throws \PHPUnit\Framework\InvalidDataProviderException
* @throws InvalidDataProviderException
*/
private static function getDataFromDataProviderAnnotation(string $docComment, string $className, string $methodName): ?iterable
{
@@ -917,10 +963,19 @@ private static function getDataFromDataProviderAnnotation(string $docComment, st
$dataProviderClassName = $dataProviderMethodNameNamespace . \array_pop($leaf);
}
$dataProviderClass = new ReflectionClass($dataProviderClassName);
$dataProviderMethod = $dataProviderClass->getMethod(
$dataProviderMethodName
);
try {
$dataProviderClass = new \ReflectionClass($dataProviderClassName);
$dataProviderMethod = $dataProviderClass->getMethod(
$dataProviderMethodName
);
} catch (\ReflectionException $e) {
throw new Exception(
$e->getMessage(),
(int) $e->getCode(),
$e
);
}
if ($dataProviderMethod->isStatic()) {
$object = null;
@@ -934,7 +989,7 @@ private static function getDataFromDataProviderAnnotation(string $docComment, st
$data = $dataProviderMethod->invoke($object, $methodName);
}
if ($data instanceof Traversable) {
if ($data instanceof \Traversable) {
$origData = $data;
$data = [];
@@ -1024,7 +1079,15 @@ private static function resolveElementToReflectionObjects(string $element): arra
$codeToCoverList = [];
if (\function_exists($element) && \strpos($element, '\\') !== false) {
$codeToCoverList[] = new ReflectionFunction($element);
try {
$codeToCoverList[] = new \ReflectionFunction($element);
} catch (\ReflectionException $e) {
throw new Exception(
$e->getMessage(),
(int) $e->getCode(),
$e
);
}
} elseif (\strpos($element, '::') !== false) {
[$className, $methodName] = \explode('::', $element);
@@ -1044,7 +1107,16 @@ private static function resolveElementToReflectionObjects(string $element): arra
);
}
$methods = (new ReflectionClass($className))->getMethods();
try {
$methods = (new \ReflectionClass($className))->getMethods();
} catch (\ReflectionException $e) {
throw new Exception(
$e->getMessage(),
(int) $e->getCode(),
$e
);
}
$inverse = isset($methodName[1]) && $methodName[1] === '!';
$visibility = 'isPublic';
@@ -1067,9 +1139,17 @@ private static function resolveElementToReflectionObjects(string $element): arra
foreach ($classes as $className) {
if ($className === '' && \function_exists($methodName)) {
$codeToCoverList[] = new ReflectionFunction(
$methodName
);
try {
$codeToCoverList[] = new \ReflectionFunction(
$methodName
);
} catch (\ReflectionException $e) {
throw new Exception(
$e->getMessage(),
(int) $e->getCode(),
$e
);
}
} else {
if (!((\class_exists($className) || \interface_exists($className) || \trait_exists($className)) &&
\method_exists($className, $methodName))) {
@@ -1082,10 +1162,18 @@ private static function resolveElementToReflectionObjects(string $element): arra
);
}
$codeToCoverList[] = new ReflectionMethod(
$className,
$methodName
);
try {
$codeToCoverList[] = new \ReflectionMethod(
$className,
$methodName
);
} catch (\ReflectionException $e) {
throw new Exception(
$e->getMessage(),
(int) $e->getCode(),
$e
);
}
}
}
}
@@ -1120,7 +1208,15 @@ private static function resolveElementToReflectionObjects(string $element): arra
);
}
$codeToCoverList[] = new ReflectionClass($className);
try {
$codeToCoverList[] = new \ReflectionClass($className);
} catch (\ReflectionException $e) {
throw new Exception(
$e->getMessage(),
(int) $e->getCode(),
$e
);
}
}
}
@@ -1132,7 +1228,7 @@ private static function resolveReflectionObjectsToLines(array $reflectors): arra
$result = [];
foreach ($reflectors as $reflector) {
if ($reflector instanceof ReflectionClass) {
if ($reflector instanceof \ReflectionClass) {
foreach ($reflector->getTraits() as $trait) {
$reflectors[] = $trait;
}
@@ -11,6 +11,8 @@
use PHPUnit\Framework\TestCase;
use PHPUnit\Util\Color;
use PHPUnit\Util\Exception as UtilException;
use PHPUnit\Util\Test;
use SebastianBergmann\Exporter\Exporter;
/**
@@ -39,12 +41,12 @@ public function __construct($useColor = false)
public function prettifyTestClass(string $className): string
{
try {
$annotations = \PHPUnit\Util\Test::parseTestMethodAnnotations($className);
$annotations = Test::parseTestMethodAnnotations($className);
if (isset($annotations['class']['testdox'][0])) {
return $annotations['class']['testdox'][0];
}
} catch (\ReflectionException $e) {
} catch (UtilException $e) {
}
$result = $className;
@@ -11,7 +11,6 @@
use PharIo\Version\VersionConstraint;
use PHPUnit\Framework\CodeCoverageException;
use PHPUnit\Framework\Exception;
use PHPUnit\Framework\InvalidDataProviderException;
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\Warning;

0 comments on commit 41a5260

Please sign in to comment.
You can’t perform that action at this time.