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

EZEE-3465: Refactored RepositoryConfigurationProvider #3085

Merged
merged 10 commits into from
Feb 12, 2021
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,7 @@ public function getRepositoryConfig()
// Takes configured repository as the reference, if it exists.
// If not, the first configured repository is considered instead.
$repositoryAlias = $this->configResolver->getParameter('repository');
if ($repositoryAlias === null) {
$aliases = array_keys($this->repositories);
$repositoryAlias = array_shift($aliases);
}
$repositoryAlias = $repositoryAlias ?: $this->getDefaultRepositoryAlias();

if (empty($repositoryAlias) || !isset($this->repositories[$repositoryAlias])) {
throw new InvalidRepositoryException(
Expand All @@ -54,6 +51,18 @@ public function getRepositoryConfig()
return ['alias' => $repositoryAlias] + $this->repositories[$repositoryAlias];
}

public function getCurrentRepositoryAlias(): string
{
return $this->getRepositoryConfig()['alias'];
}

public function getDefaultRepositoryAlias(): ?string
{
$aliases = array_keys($this->repositories);

return array_shift($aliases);
Comment on lines +61 to +63
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be replaced by https://www.php.net/manual/en/function.array-key-first during merge up to 3.x

}

public function getStorageConnectionName(): string
{
$repositoryConfig = $this->getRepositoryConfig();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,19 +69,13 @@ public function testGetRepositoryConfigNotSpecifiedRepository()
}

/**
* @dataProvider providerForRepositories
*
* @expectedException \eZ\Bundle\EzPublishCoreBundle\ApiLoader\Exception\InvalidRepositoryException
*/
public function testGetRepositoryConfigUndefinedRepository()
public function testGetRepositoryConfigUndefinedRepository(array $repositories): void
{
$configResolver = $this->getConfigResolverMock();
$repositories = [
'main' => [
'engine' => 'foo',
],
'another' => [
'engine' => 'bar',
],
];

$configResolver
->expects($this->once())
Expand All @@ -93,6 +87,48 @@ public function testGetRepositoryConfigUndefinedRepository()
$provider->getRepositoryConfig();
}

/**
* @dataProvider providerForRepositories
*/
public function testGetDefaultRepositoryAlias(array $repositories): void
{
$configResolver = $this->getConfigResolverMock();

$provider = new RepositoryConfigurationProvider($configResolver, $repositories);
$provider->getRepositoryConfig();

self::assertSame('first', $provider->getDefaultRepositoryAlias());
}

/**
* @dataProvider providerForRepositories
*/
public function testGetCurrentRepositoryAlias(array $repositories): void
{
$configResolver = $this->getConfigResolverMock();

$provider = new RepositoryConfigurationProvider($configResolver, $repositories);
$provider->getRepositoryConfig();

self::assertSame('first', $provider->getCurrentRepositoryAlias());
}

public function providerForRepositories(): array
{
return [
[
[
'first' => [
'engine' => 'foo',
],
'second' => [
'engine' => 'bar',
],
],
],
];
}

/**
* @return \PHPUnit\Framework\MockObject\MockObject|\eZ\Publish\Core\MVC\ConfigResolverInterface
*/
Expand Down