From 90f9e6b8245ffcaa4741232acb9f4a8650df90a8 Mon Sep 17 00:00:00 2001 From: Pierre Ducoudray Date: Wed, 12 Jul 2017 18:43:57 +0200 Subject: [PATCH] Possibility to add a limit to the number of object in a batch --- .travis.yml | 1 - Features/Context/TranslationContextTrait.php | 3 +- Features/copywriting.feature | 17 ++++++++ Features/translation.feature | 17 ++++++++ .../Service/ProductFinder.php | 6 ++- .../Service/Project/Author.php | 6 +-- .../Translation/TranslationGeneratorTest.php | 42 ++++++++++++++++++- Translation/TranslatableFinderInterface.php | 3 +- Translation/TranslationGenerator.php | 5 ++- Translation/TranslationGeneratorInterface.php | 4 +- Translation/TranslationManager.php | 1 - 11 files changed, 92 insertions(+), 13 deletions(-) diff --git a/.travis.yml b/.travis.yml index 01cddd3..9528294 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,7 +5,6 @@ sudo: false php: - 5.6 - 7.0 - - hhvm cache: directories: diff --git a/Features/Context/TranslationContextTrait.php b/Features/Context/TranslationContextTrait.php index 86c1100..5dec5aa 100644 --- a/Features/Context/TranslationContextTrait.php +++ b/Features/Context/TranslationContextTrait.php @@ -100,7 +100,8 @@ public function createTranslationProject(TableNode $table) json_decode($data['options'], true), isset($data['activity']) ? $data['activity'] : null, isset($data['workTemplate']) ? $data['workTemplate'] : null, - json_decode($data['useMyTextmasters']) + json_decode($data['useMyTextmasters']), + isset($data['limit']) ? (int) $data['limit'] : null ); if (null !== $project) { diff --git a/Features/copywriting.feature b/Features/copywriting.feature index 9b24147..a69fd8f 100644 --- a/Features/copywriting.feature +++ b/Features/copywriting.feature @@ -44,3 +44,20 @@ Feature: Copywriting management | finder | filter | name | languageFrom | category | briefing | options | activity | useMyTextmasters | | product | {} | PROJECT-1 | de | C054 | Nothing | {"language_level": "premium"} | copywriting | false | Then I should have "2" translatables with job for class "Worldia\Bundle\ProductTestBundle\Entity\Product" and locale de + + Scenario: Generate EN copywriting for products with limit + Given I have "2" products + And I have the following translations for product "1": + | title | description | locale | + | Hello Paris | Paris is the city of lights | en | + And I have the following translations for product "2": + | title | description | locale | + | Hello NYC | NYC is the big apple | en | + Then I should have "0" translatables with job for class "Worldia\Bundle\ProductTestBundle\Entity\Product" and locale en + When I generate a translation batch with the following parameters: + | finder | filter | name | languageFrom | category | briefing | options | activity | useMyTextmasters | limit | + | product | {} | PROJECT-1 | en | C054 | Nothing | {"language_level": "premium"} | copywriting | false | 1 | + Then I should have "1" translatables with job for class "Worldia\Bundle\ProductTestBundle\Entity\Product" and locale en + Then I should have the following jobs: + | id | translatable | project | document | status | locale | + | 1 | 1 | PROJECT-1 | en-copywriting-1 | created | en | diff --git a/Features/translation.feature b/Features/translation.feature index b7fbfec..11ea7ed 100644 --- a/Features/translation.feature +++ b/Features/translation.feature @@ -46,3 +46,20 @@ Feature: Translation management | product | {} | PROJECT-1 | en | de | C054 | Nothing | {"language_level": "premium"} | translation | true | Then I should have "2" translatables with job for class "Worldia\Bundle\ProductTestBundle\Entity\Product" and locale de Then I should have "2" translatables with job for class "Worldia\Bundle\ProductTestBundle\Entity\Product" and locale fr + + Scenario: Generate FR translations for products with limit + Given I have "2" products + And I have the following translations for product "1": + | title | description | locale | + | Hello Paris | Paris is the city of lights | en | + And I have the following translations for product "2": + | title | description | locale | + | Hello NYC | NYC is the big apple | en | + Then I should have "0" translatables with job for class "Worldia\Bundle\ProductTestBundle\Entity\Product" and locale fr + When I generate a translation batch with the following parameters: + | finder | filter | name | languageFrom | languageTo | category | briefing | options | activity | useMyTextmasters | limit | + | product | {} | PROJECT-1 | en | fr | C054 | Nothing | {"language_level": "premium"} | translation | true | 1 | + Then I should have "1" translatables with job for class "Worldia\Bundle\ProductTestBundle\Entity\Product" and locale fr + Then I should have the following jobs: + | id | translatable | project | document | status | locale | + | 1 | 1 | PROJECT-1 | en-fr-1 | created | fr | diff --git a/Tests/Fixtures/Project/src/Worldia/Bundle/ProductTestBundle/Service/ProductFinder.php b/Tests/Fixtures/Project/src/Worldia/Bundle/ProductTestBundle/Service/ProductFinder.php index a9502f7..6408efb 100644 --- a/Tests/Fixtures/Project/src/Worldia/Bundle/ProductTestBundle/Service/ProductFinder.php +++ b/Tests/Fixtures/Project/src/Worldia/Bundle/ProductTestBundle/Service/ProductFinder.php @@ -40,7 +40,7 @@ public function getCode() /** * {@inheritdoc} */ - public function find($locale, array $filter = []) + public function find($locale, array $filter = [], $limit = null) { $qb = $this->manager->createQueryBuilder(); $qb @@ -56,6 +56,10 @@ public function find($locale, array $filter = []) ; } + if (null !== $limit) { + $qb->setMaxResults($limit); + } + return $qb->getQuery()->getResult(); } } diff --git a/Tests/Fixtures/Project/src/Worldia/Bundle/ProductTestBundle/Service/Project/Author.php b/Tests/Fixtures/Project/src/Worldia/Bundle/ProductTestBundle/Service/Project/Author.php index b60a418..fbdbe44 100644 --- a/Tests/Fixtures/Project/src/Worldia/Bundle/ProductTestBundle/Service/Project/Author.php +++ b/Tests/Fixtures/Project/src/Worldia/Bundle/ProductTestBundle/Service/Project/Author.php @@ -30,14 +30,14 @@ public function all($status = null) 'status' => 'my_textmaster', 'id' => '58f9febce27d6b5dd2b0b4ec', 'author_id' => '55c3763e656462000b000027', - ] - ] + ], + ], ]; } public function setPage($page) { - $this->page = (null === $page ? $page : (int)$page); + $this->page = (null === $page ? $page : (int) $page); return $this; } diff --git a/Tests/Units/Translation/TranslationGeneratorTest.php b/Tests/Units/Translation/TranslationGeneratorTest.php index 70fac60..bf76a84 100644 --- a/Tests/Units/Translation/TranslationGeneratorTest.php +++ b/Tests/Units/Translation/TranslationGeneratorTest.php @@ -2,6 +2,7 @@ namespace Worldia\Bundle\TextmasterBundle\Tests\Units\Translation; +use Textmaster\Model\ProjectInterface; use Worldia\Bundle\TextmasterBundle\Translation\TranslationGenerator; class TranslationGeneratorTest extends \PHPUnit_Framework_TestCase @@ -37,7 +38,7 @@ public function shouldGenerateProject() $generator = new TranslationGenerator($translationManagerMock); $generator->addTranslatableFinder($translatableFinderMock); - $project = $generator->generate('code', [], 'languageFrom', 'languageTo', 'name', 'category', 'briefing'); + $project = $generator->generate('code', [], 'languageFrom', 'name', 'category', 'briefing'); $this->assertSame($projectMock, $project); } @@ -70,7 +71,44 @@ public function shouldGenerateProjectWithTranslationMemory() $generator = new TranslationGenerator($translationManagerMock); $generator->addTranslatableFinder($translatableFinderMock); - $project = $generator->generate('code', [], 'languageFrom', 'languageTo', 'name', 'category', 'briefing'); + $project = $generator->generate('code', [], 'languageFrom', 'name', 'category', 'briefing'); + + $this->assertSame($projectMock, $project); + } + + /** + * @test + */ + public function shouldGenerateProjectWithLimit() + { + $translationManagerMock = $this->getMock('Worldia\Bundle\TextmasterBundle\Translation\TranslationManagerInterface'); + $translatableFinderMock = $this->getMock('Worldia\Bundle\TextmasterBundle\Translation\TranslatableFinderInterface'); + $translatableMock = $this->getMock('TranslatableInterface'); + $projectMock = $this->getMockBuilder('Textmaster\Model\Project')->disableOriginalConstructor()->getMock(); + + $translatableFinderMock->expects($this->once()) + ->method('getCode') + ->willReturn('code'); + $translatableFinderMock->expects($this->once()) + ->method('find') + ->with('languageFrom', [], 5) + ->willReturn([$translatableMock]); + + $translationManagerMock->expects($this->once()) + ->method('create') + ->willReturn($projectMock); + + $projectMock->expects($this->once()) + ->method('getOptions') + ->willReturn([]); + $projectMock->expects($this->once()) + ->method('launch') + ->willReturn($projectMock); + + $generator = new TranslationGenerator($translationManagerMock); + $generator->addTranslatableFinder($translatableFinderMock); + + $project = $generator->generate('code', [], 'languageFrom', 'name', 'category', 'briefing', null, [], ProjectInterface::ACTIVITY_TRANSLATION, null, true, 5); $this->assertSame($projectMock, $project); } diff --git a/Translation/TranslatableFinderInterface.php b/Translation/TranslatableFinderInterface.php index 57fbc98..c45c522 100644 --- a/Translation/TranslatableFinderInterface.php +++ b/Translation/TranslatableFinderInterface.php @@ -16,8 +16,9 @@ public function getCode(); * * @param string $locale locale code (ex: fr_FR, en_US, de_DE...) * @param array $filter array of parameters to filter the objects + * @param array $limit max number of objects to retrieve * * @return object[] */ - public function find($locale, array $filter = []); + public function find($locale, array $filter = [], $limit = null); } diff --git a/Translation/TranslationGenerator.php b/Translation/TranslationGenerator.php index 1fa6dd6..be43e38 100644 --- a/Translation/TranslationGenerator.php +++ b/Translation/TranslationGenerator.php @@ -50,12 +50,13 @@ public function generate( array $options = [], $activity = ProjectInterface::ACTIVITY_TRANSLATION, $workTemplate = null, - $useMyTextmasters = true + $useMyTextmasters = true, + $limit = null ) { if (null === $locale = $languageTo) { $locale = $languageFrom; } - $translatables = $this->translatableFinders[$finderCode]->find($locale, $filter); + $translatables = $this->translatableFinders[$finderCode]->find($locale, $filter, $limit); if (!count($translatables)) { return; diff --git a/Translation/TranslationGeneratorInterface.php b/Translation/TranslationGeneratorInterface.php index c45fc6f..f9395d3 100644 --- a/Translation/TranslationGeneratorInterface.php +++ b/Translation/TranslationGeneratorInterface.php @@ -20,6 +20,7 @@ interface TranslationGeneratorInterface * @param string $activity textmaster project activity * @param string $workTemplate textmaster project work template * @param bool $useMyTextmasters use my favorite authors for the project + * @param int $limit limit the number of document in the batch * * @return ProjectInterface|null */ @@ -34,6 +35,7 @@ public function generate( array $options = [], $activity, $workTemplate = null, - $useMyTextmasters = true + $useMyTextmasters = true, + $limit = null ); } diff --git a/Translation/TranslationManager.php b/Translation/TranslationManager.php index db07bcd..d2c614a 100644 --- a/Translation/TranslationManager.php +++ b/Translation/TranslationManager.php @@ -4,7 +4,6 @@ use Symfony\Component\Routing\Generator\UrlGeneratorInterface; use Textmaster\Manager; -use Textmaster\Model\Author; use Textmaster\Model\DocumentInterface; use Textmaster\Model\ProjectInterface; use Textmaster\Translator\TranslatorInterface;