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

ENH Add functionality required by other repositories #29

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading