Navigation Menu

Skip to content

Commit

Permalink
EZP-30006: As a Developer I possibility to load several languages at …
Browse files Browse the repository at this point in the history
…once (#2524)

Introduces API to load several languages at once (specifically by id or locale), exposes more specific methods on languageMask handler and in SPI to make language handling more efficient.
  • Loading branch information
andrerom committed Feb 19, 2019
1 parent 7a8b5f7 commit c53d48d
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 12 deletions.
22 changes: 22 additions & 0 deletions Repository/LanguageService.php
Expand Up @@ -91,6 +91,28 @@ public function loadLanguages();
*/
public function loadLanguageById($languageId);

/**
* Bulk-load Languages by language codes.
*
* Note: it does not throw exceptions on load, just ignores erroneous Languages.
*
* @param string[] $languageCodes
*
* @return \eZ\Publish\API\Repository\Values\Content\Language[] list of Languages with language-code as keys
*/
public function loadLanguageListByCode(array $languageCodes): iterable;

/**
* Bulk-load Languages by ids.
*
* Note: it does not throw exceptions on load, just ignores erroneous Languages.
*
* @param int[] $languageIds
*
* @return \eZ\Publish\API\Repository\Values\Content\Language[] list of Languages with id as keys
*/
public function loadLanguageListById(array $languageIds): iterable;

/**
* Deletes a language from content repository.
*
Expand Down
55 changes: 43 additions & 12 deletions Repository/Tests/LanguageServiceTest.php
Expand Up @@ -152,13 +152,13 @@ public function testCreateLanguageThrowsInvalidArgumentException()
* Test for the loadLanguageById() method.
*
* @covers \eZ\Publish\API\Repository\LanguageService::loadLanguageById
* @depends eZ\Publish\API\Repository\Tests\LanguageServiceTest::testCreateLanguage
* @covers \eZ\Publish\API\Repository\LanguageService::loadLanguageListById
* @depends testCreateLanguage
*/
public function testLoadLanguageById()
{
$repository = $this->getRepository();

/* BEGIN: Use Case */
$languageService = $repository->getContentLanguageService();

$languageCreate = $languageService->newLanguageCreateStruct();
Expand All @@ -169,20 +169,25 @@ public function testLoadLanguageById()
$languageId = $languageService->createLanguage($languageCreate)->id;

$language = $languageService->loadLanguageById($languageId);
/* END: Use Case */

$this->assertInstanceOf(
Language::class,
$language
);

$languages = $languageService->loadLanguageListById([$languageId]);

$this->assertInternalType('iterable', $languages);
$this->assertCount(1, $languages);
$this->assertInstanceOf(Language::class, $languages[$languageId]);
}

/**
* Test for the loadLanguageById() method.
*
* @covers \eZ\Publish\API\Repository\LanguageService::loadLanguageById
* @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
* @depends eZ\Publish\API\Repository\Tests\LanguageServiceTest::testLoadLanguageById
* @covers \eZ\Publish\API\Repository\LanguageService::loadLanguageListById
* @depends testLoadLanguageById
*/
public function testLoadLanguageByIdThrowsNotFoundException()
{
Expand All @@ -192,7 +197,13 @@ public function testLoadLanguageByIdThrowsNotFoundException()
/* BEGIN: Use Case */
$languageService = $repository->getContentLanguageService();

// This call should fail with a "NotFoundException"
$languages = $languageService->loadLanguageListById([$nonExistentLanguageId]);

$this->assertInternalType('iterable', $languages);
$this->assertCount(0, $languages);

$this->expectException(NotFoundException::class);

$languageService->loadLanguageById($nonExistentLanguageId);
/* END: Use Case */
}
Expand Down Expand Up @@ -322,7 +333,8 @@ public function testDisableLanguage()
* Test for the loadLanguage() method.
*
* @covers \eZ\Publish\API\Repository\LanguageService::loadLanguage
* @depends eZ\Publish\API\Repository\Tests\LanguageServiceTest::testCreateLanguage
* @covers \eZ\Publish\API\Repository\LanguageService::loadLanguageListByCode
* @depends testCreateLanguage
*/
public function testLoadLanguage()
{
Expand Down Expand Up @@ -351,25 +363,44 @@ public function testLoadLanguage()
],
$language
);

$languages = $languageService->loadLanguageListByCode(['eng-NZ']);

$this->assertInternalType('iterable', $languages);
$this->assertCount(1, $languages);

$this->assertPropertiesCorrect(
[
'id' => $languageId,
'languageCode' => 'eng-NZ',
'name' => 'English',
'enabled' => true,
],
$languages['eng-NZ']
);
}

/**
* Test for the loadLanguage() method.
*
* @covers \eZ\Publish\API\Repository\LanguageService::loadLanguage
* @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
* @depends eZ\Publish\API\Repository\Tests\LanguageServiceTest::testLoadLanguage
* @covers \eZ\Publish\API\Repository\LanguageService::loadLanguageListByCode
* @depends testLoadLanguage
*/
public function testLoadLanguageThrowsNotFoundException()
{
$repository = $this->getRepository();

/* BEGIN: Use Case */
$languageService = $repository->getContentLanguageService();

// This call should fail with an exception
$languages = $languageService->loadLanguageListById(['fre-FR']);

$this->assertInternalType('iterable', $languages);
$this->assertCount(0, $languages);

$this->expectException(NotFoundException::class);

$languageService->loadLanguage('fre-FR');
/* END: Use Case */
}

/**
Expand Down

0 comments on commit c53d48d

Please sign in to comment.