Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup and small refactorings #3830

10 changes: 0 additions & 10 deletions src/Framework/MockObject/Invocation.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,16 +100,6 @@ public function getParameters(): array
return $this->parameters;
}

public function getReturnType(): string
{
return $this->returnType;
}

public function isReturnTypeNullable(): bool
{
return $this->isReturnTypeNullable;
}

/**
* @throws RuntimeException
*
Expand Down
118 changes: 27 additions & 91 deletions src/Runner/StandardTestSuiteLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
*/
namespace PHPUnit\Runner;

use PHPUnit\Framework\TestCase;
use PHPUnit\Util\FileLoader;
use PHPUnit\Util\Filesystem;
use ReflectionClass;
Expand All @@ -25,97 +24,26 @@ final class StandardTestSuiteLoader implements TestSuiteLoader
*/
public function load(string $suiteClassName, string $suiteClassFile = ''): ReflectionClass
{
$suiteClassName = \str_replace('.php', '', $suiteClassName);
$suiteClassName = \str_replace('.php', '', \basename($suiteClassName));

if (empty($suiteClassFile)) {
$suiteClassFile = Filesystem::classNameToFilename(
$suiteClassName
);
}

if (!\class_exists($suiteClassName, false)) {
$loadedClasses = \get_declared_classes();

$filename = FileLoader::checkAndLoad($suiteClassFile);

$loadedClasses = \array_values(
\array_diff(\get_declared_classes(), $loadedClasses)
);
}

if (!empty($loadedClasses) && !\class_exists($suiteClassName, false)) {
$offset = 0 - \strlen($suiteClassName);

foreach ($loadedClasses as $loadedClass) {
try {
$class = new ReflectionClass($loadedClass);
} catch (\ReflectionException $e) {
throw new Exception(
$e->getMessage(),
(int) $e->getCode(),
$e
);
}

if (\substr($loadedClass, $offset) === $suiteClassName &&
$class->getFileName() == $filename) {
$suiteClassName = $loadedClass;

break;
}
}
}

if (!empty($loadedClasses) && !\class_exists($suiteClassName, false)) {
$testCaseClass = TestCase::class;

foreach ($loadedClasses as $loadedClass) {
try {
$class = new ReflectionClass($loadedClass);
} catch (\ReflectionException $e) {
throw new Exception(
$e->getMessage(),
(int) $e->getCode(),
$e
);
}

$classFile = $class->getFileName();

if ($class->isSubclassOf($testCaseClass) && !$class->isAbstract()) {
$suiteClassName = $loadedClass;
$testCaseClass = $loadedClass;

if ($classFile == \realpath($suiteClassFile)) {
break;
}
}

if ($class->hasMethod('suite')) {
try {
$method = $class->getMethod('suite');
} catch (\ReflectionException $e) {
throw new Exception(
$e->getMessage(),
(int) $e->getCode(),
$e
);
}

if (!$method->isAbstract() && $method->isPublic() && $method->isStatic()) {
$suiteClassName = $loadedClass;
$loadedClasses = \get_declared_classes();
$filename = FileLoader::checkAndLoad($suiteClassFile);
$loadedClasses = \array_values(
\array_diff(\get_declared_classes(), $loadedClasses)
);

if ($classFile == \realpath($suiteClassFile)) {
break;
}
}
}
}
}
$offset = 0 - \strlen($suiteClassName);
$class = null;

if (\class_exists($suiteClassName, false)) {
foreach ($loadedClasses as $loadedClass) {
try {
$class = new ReflectionClass($suiteClassName);
$class = new ReflectionClass($loadedClass);
} catch (\ReflectionException $e) {
throw new Exception(
$e->getMessage(),
Expand All @@ -124,18 +52,26 @@ public function load(string $suiteClassName, string $suiteClassFile = ''): Refle
);
}

if ($class->getFileName() == \realpath($suiteClassFile)) {
return $class;
if (\substr($loadedClass, $offset) === $suiteClassName &&
$class->getFileName() == $filename) {
$suiteClassName = $loadedClass;

break;
}
}

throw new Exception(
\sprintf(
"Class '%s' could not be found in '%s'.",
$suiteClassName,
$suiteClassFile
)
);
if (!\class_exists($suiteClassName, false) ||
!($class instanceof ReflectionClass)) {
throw new Exception(
\sprintf(
"Class '%s' could not be found in '%s'.",
$suiteClassName,
$suiteClassFile
)
);
}

return $class;
}

public function reload(ReflectionClass $aClass): ReflectionClass
Expand Down
22 changes: 11 additions & 11 deletions src/TextUI/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use PHPUnit\Framework\Test;
use PHPUnit\Framework\TestListener;
use PHPUnit\Framework\TestSuite;
use PHPUnit\Runner\PhptTestCase;
use PHPUnit\Runner\StandardTestSuiteLoader;
use PHPUnit\Runner\TestSuiteLoader;
use PHPUnit\Runner\TestSuiteSorter;
Expand Down Expand Up @@ -777,6 +776,10 @@ protected function handleArguments(array $argv): void

$this->handleCustomTestSuite();

if (!isset($this->arguments['testSuffixes'])) {
$this->arguments['testSuffixes'] = ['Test.php', '.phpt'];
}

if (!isset($this->arguments['test'])) {
if (isset($this->options[1][0])) {
$this->arguments['test'] = $this->options[1][0];
Expand Down Expand Up @@ -804,10 +807,14 @@ protected function handleArguments(array $argv): void
$this->arguments['testFile'] = \realpath($this->arguments['test']);
$this->arguments['test'] = \substr($this->arguments['test'], 0, \strrpos($this->arguments['test'], '.'));
}
}

if (!isset($this->arguments['testSuffixes'])) {
$this->arguments['testSuffixes'] = ['Test.php', '.phpt'];
if (isset($this->arguments['test']) &&
\is_string($this->arguments['test']) &&
\substr($this->arguments['test'], -5, 5) === '.phpt') {
$suite = new TestSuite;
$suite->addTestFile($this->arguments['test']);
$this->arguments['test'] = $suite;
}
}

if (isset($includePath)) {
Expand Down Expand Up @@ -921,13 +928,6 @@ protected function handleArguments(array $argv): void
$this->arguments['printer'] = $this->handlePrinter($this->arguments['printer']);
}

if (isset($this->arguments['test']) && \is_string($this->arguments['test']) && \substr($this->arguments['test'], -5, 5) === '.phpt') {
$test = new PhptTestCase($this->arguments['test']);

$this->arguments['test'] = new TestSuite;
$this->arguments['test']->addTest($test);
}

if (!isset($this->arguments['test'])) {
$this->showHelp();
exit(TestRunner::EXCEPTION_EXIT);
Expand Down
2 changes: 1 addition & 1 deletion tests/end-to-end/regression/GitHub/2830.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ GH-2830: @runClassInSeparateProcess fails for tests with a @dataProvider
--FILE--
<?php declare(strict_types=1);
$_SERVER['argv'][1] = '--no-configuration';
$_SERVER['argv'][2] = 'Issue2830';
$_SERVER['argv'][2] = 'Issue2830Test';
$_SERVER['argv'][3] = __DIR__ . '/2830/Issue2830Test.php';

require __DIR__ . '/../../../bootstrap.php';
Expand Down