Skip to content

Commit

Permalink
Implement SchemaFinder::available()
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianbergmann committed Jan 18, 2024
1 parent 78b6f7e commit d4e4655
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
27 changes: 27 additions & 0 deletions src/Util/Xml/SchemaFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,43 @@
*/
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;

/**
* @internal This class is not covered by the backward compatibility promise for PHPUnit
*/
final class SchemaFinder
{
/**
* @psalm-return non-empty-list<non-empty-string>
*/
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
*/
Expand Down
13 changes: 11 additions & 2 deletions tests/unit/Util/Xml/SchemaFinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/
namespace PHPUnit\Util\Xml;

use function count;
use PHPUnit\Framework\TestCase;
use PHPUnit\Runner\Version;

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

Expand Down

0 comments on commit d4e4655

Please sign in to comment.