Skip to content

Commit

Permalink
ENH Add functionality required by other repositories (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
GuySartorelli authored May 8, 2024
1 parent cde6b55 commit 08ca1f1
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 3 deletions.
31 changes: 28 additions & 3 deletions src/MetaData.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,31 @@ public static function getMetaDataForLocksteppedRepos(): array
}

/**
* Get all metadata about all repositories we have information about
* Get metadata about all repositories we have information about,
* but only if they're supported for the given CMS major version.
*
* @param array $metadata Flat array of repository metadata, e.g. from getAllRepositoryMetaData(false)
* @param bool $keepWildcardMap If true, repositories with a "*" CMS major mapping are kept in the output
*/
public static function getAllRepositoryMetaData(): array
public static function removeReposNotInCmsMajor(array $metadata, string|int $cmsMajor, bool $keepWildcardMap = false): array
{
foreach ($metadata as $i => $repo) {
if (
!array_key_exists($cmsMajor, $repo['majorVersionMapping']) &&
(!$keepWildcardMap || !array_key_exists('*', $repo['majorVersionMapping']))
) {
unset($metadata[$i]);
}
}
// Use array_values to reset array indices
return array_values($metadata);
}

/**
* Get all metadata about all repositories we have information about.
* @param bool $categorised If true, output is grouped by category.
*/
public static function getAllRepositoryMetaData(bool $categorised = true): array
{
if (empty(self::$repositoryMetaData)) {
$rawJson = file_get_contents(__DIR__ . '/../repositories.json');
Expand All @@ -160,6 +182,9 @@ public static function getAllRepositoryMetaData(): array
}
self::$repositoryMetaData = $decodedJson;
}
return self::$repositoryMetaData;
if ($categorised) {
return self::$repositoryMetaData;
}
return array_merge(...array_values(self::$repositoryMetaData));
}
}
60 changes: 60 additions & 0 deletions tests/MetaDataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,66 @@ public function testGetMetaDataForLocksteppedRepos(string $repoName, bool $isLoc
}
}

public function provideRemoveReposNotInCmsMajor(): array
{
return [
'int major, no wildcard' => [
'cmsMajor' => 4,
'keepWildcardMap' => false,
'expectEmpty' => false,
],
'string major, with wildcard' => [
'cmsMajor' => '5',
'keepWildcardMap' => true,
'expectEmpty' => false,
],
'non-existant major, no wildcard' => [
'cmsMajor' => 'random',
'keepWildcardMap' => false,
'expectEmpty' => true,
],
'non-existant major, with wildcard' => [
'cmsMajor' => 'random',
'keepWildcardMap' => true,
'expectEmpty' => false,
],
];
}

/**
* @dataProvider provideRemoveReposNotInCmsMajor
*/
public function testRemoveReposNotInCmsMajor(int|string $cmsMajor, bool $keepWildcardMap, bool $expectEmpty): void
{
$flatData = MetaData::getAllRepositoryMetaData(false);
$result = MetaData::removeReposNotInCmsMajor($flatData, $cmsMajor, $keepWildcardMap);
if ($expectEmpty) {
$this->assertEmpty($result);
} else {
$this->assertNotEmpty($result);
$expected = [];
foreach ($flatData as $repo) {
if (array_key_exists($cmsMajor, $repo['majorVersionMapping'])) {
$expected[] = $repo;
}
if ($keepWildcardMap && array_key_exists('*', $repo['majorVersionMapping'])) {
$expected[] = $repo;
}
}
$this->assertSame($expected, $result);
}
}

public function testGetAllRepositoryMetaDataNoCategories(): void
{
$withCategories = MetaData::getAllRepositoryMetaData(true);
$expected = [];
foreach ($withCategories as $repos) {
$expected = array_merge($expected, $repos);
}
$this->assertSame($expected, MetaData::getAllRepositoryMetaData(false));
}

public function testGetAllRepositoryMetaData(): void
{
// Validate data has correct categories
Expand Down

0 comments on commit 08ca1f1

Please sign in to comment.