Skip to content

Commit

Permalink
Add more comments about namespace detection
Browse files Browse the repository at this point in the history
  • Loading branch information
totten authored and sebastianbergmann committed Aug 11, 2022
1 parent c1f34a7 commit 2bdc999
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/Framework/TestSuite.php
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,7 @@ public function addTestFile(string $filename): void
$this->addTest($method->invoke(null, $className));
}
} elseif ($class->implementsInterface(Test::class)) {
// Do we have modern namespacing ('Foo\Bar\WhizBangTest') or old-school namespacing ('Foo_Bar_WhizBangTest')?
$isPsr0 = (!$class->inNamespace()) && (strpos($class->getName(), '_') !== false);
$expectedClassName = $isPsr0 ? $className : $shortName;

Expand Down
4 changes: 3 additions & 1 deletion src/Runner/StandardTestSuiteLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,18 @@ public function load(string $suiteClassFile): ReflectionClass
}

if (!class_exists($suiteClassName, false)) {
// this block will handle namespaced classes
// Perhaps this file defines a class inside a namespace? Let's check...
$offset = 0 - strlen($suiteClassName);

foreach ($loadedClasses as $loadedClass) {
// Detect modern namespace (eg './tests/Foo/Bar/WhizBangTest.php' <=> 'Foo\Bar\WhizBangTest')
if (stripos(substr($loadedClass, $offset - 1), '\\' . $suiteClassName) === 0) {
$suiteClassName = $loadedClass;

break;
}

// Detect old-school namespace (eg 'tests/Foo/Bar/WhizBangTest.php' <=> 'Foo_Bar_WhizBangTest'; #5020)
if (stripos(substr($loadedClass, $offset - 1), '_' . $suiteClassName) === 0) {
$suiteClassName = $loadedClass;

Expand Down

0 comments on commit 2bdc999

Please sign in to comment.