From d4e46555c0649ed84679fd12a27cf3b69ec76e2f Mon Sep 17 00:00:00 2001 From: Sebastian Bergmann Date: Thu, 18 Jan 2024 09:37:54 +0100 Subject: [PATCH] Implement SchemaFinder::available() --- src/Util/Xml/SchemaFinder.php | 27 ++++++++++++++++++++++++ tests/unit/Util/Xml/SchemaFinderTest.php | 13 ++++++++++-- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/src/Util/Xml/SchemaFinder.php b/src/Util/Xml/SchemaFinder.php index af17dfe94c2..eb5f4f15511 100644 --- a/src/Util/Xml/SchemaFinder.php +++ b/src/Util/Xml/SchemaFinder.php @@ -9,9 +9,12 @@ */ namespace PHPUnit\Util\Xml; +use function assert; use function defined; use function is_file; +use function rsort; use function sprintf; +use DirectoryIterator; use PHPUnit\Runner\Version; /** @@ -19,6 +22,30 @@ */ final class SchemaFinder { + /** + * @psalm-return non-empty-list + */ + public function available(): array + { + $result = [Version::series()]; + + foreach ((new DirectoryIterator($this->path() . 'schema')) as $file) { + if ($file->isDot()) { + continue; + } + + $version = $file->getBasename('.xsd'); + + assert(!empty($version)); + + $result[] = $version; + } + + rsort($result); + + return $result; + } + /** * @throws Exception */ diff --git a/tests/unit/Util/Xml/SchemaFinderTest.php b/tests/unit/Util/Xml/SchemaFinderTest.php index 47e24ed21d8..6ec00d416c3 100644 --- a/tests/unit/Util/Xml/SchemaFinderTest.php +++ b/tests/unit/Util/Xml/SchemaFinderTest.php @@ -9,6 +9,7 @@ */ namespace PHPUnit\Util\Xml; +use function count; use PHPUnit\Framework\TestCase; use PHPUnit\Runner\Version; @@ -19,12 +20,20 @@ */ final class SchemaFinderTest extends TestCase { - public function testFindsExistingSchemaForComposerInstallation(): void + public function testListsAvailableSchemas(): void + { + $schemas = (new SchemaFinder)->available(); + + $this->assertSame((new Version)->series(), $schemas[0]); + $this->assertSame('8.5', $schemas[count($schemas) - 1]); + } + + public function testFindsExistingSchema(): void { $this->assertFileExists((new SchemaFinder)->find((new Version)->series())); } - public function testDoesNotFindNonExistentSchemaForComposerInstallation(): void + public function testDoesNotFindNonExistentSchema(): void { $this->expectException(Exception::class);