From 66f2762e3bb1a74559283e5f1c2787773dd1338f Mon Sep 17 00:00:00 2001 From: Bartek Date: Fri, 12 Feb 2021 08:15:36 +0100 Subject: [PATCH] EZEE-3465: Refactored RepositoryConfigurationProvider (#3085) --- .../RepositoryConfigurationProvider.php | 17 ++++-- .../RepositoryConfigurationProviderTest.php | 54 +++++++++++++++---- 2 files changed, 58 insertions(+), 13 deletions(-) diff --git a/eZ/Bundle/EzPublishCoreBundle/ApiLoader/RepositoryConfigurationProvider.php b/eZ/Bundle/EzPublishCoreBundle/ApiLoader/RepositoryConfigurationProvider.php index dc3cf73e31..77783b3171 100644 --- a/eZ/Bundle/EzPublishCoreBundle/ApiLoader/RepositoryConfigurationProvider.php +++ b/eZ/Bundle/EzPublishCoreBundle/ApiLoader/RepositoryConfigurationProvider.php @@ -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( @@ -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); + } + public function getStorageConnectionName(): string { $repositoryConfig = $this->getRepositoryConfig(); diff --git a/eZ/Bundle/EzPublishCoreBundle/Tests/ApiLoader/RepositoryConfigurationProviderTest.php b/eZ/Bundle/EzPublishCoreBundle/Tests/ApiLoader/RepositoryConfigurationProviderTest.php index 820696c6fe..0de41280c5 100644 --- a/eZ/Bundle/EzPublishCoreBundle/Tests/ApiLoader/RepositoryConfigurationProviderTest.php +++ b/eZ/Bundle/EzPublishCoreBundle/Tests/ApiLoader/RepositoryConfigurationProviderTest.php @@ -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()) @@ -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 */