Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianbergmann committed May 21, 2019
1 parent 35253e0 commit 41a5260
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 44 deletions.
5 changes: 3 additions & 2 deletions src/Framework/TestCase.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -1717,7 +1718,7 @@ private function setExpectedExceptionFromAnnotation(): void
$this->expectExceptionMessageRegExp($expectedException['message_regex']);
}
}
} catch (\ReflectionException $e) {
} catch (UtilException $e) {
}
}

Expand Down
174 changes: 135 additions & 39 deletions src/Util/Test.php
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 = [];

Expand All @@ -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 = [];
}

Expand All @@ -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;
Expand Down Expand Up @@ -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;
}
Expand All @@ -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(
Expand All @@ -796,7 +843,7 @@ public static function getHookMethods(string $className): array
}
}
}
} catch (ReflectionException $e) {
} catch (\ReflectionException $e) {
}
}

Expand Down Expand Up @@ -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
{
Expand All @@ -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;
Expand All @@ -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 = [];

Expand Down Expand Up @@ -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);

Expand All @@ -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';

Expand All @@ -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))) {
Expand All @@ -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
);
}
}
}
}
Expand Down Expand Up @@ -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
);
}
}
}

Expand All @@ -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;
}
Expand Down
6 changes: 4 additions & 2 deletions src/Util/TestDox/NamePrettifier.php
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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;
Expand Down
1 change: 0 additions & 1 deletion tests/unit/Util/TestTest.php
Expand Up @@ -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;
Expand Down

0 comments on commit 41a5260

Please sign in to comment.