Skip to content

Commit

Permalink
TranslatorBag::diff now iterates over catalogue domains instead of op…
Browse files Browse the repository at this point in the history
…eration domains
  • Loading branch information
welcoMattic committed Mar 27, 2023
1 parent 29c12fd commit 9a40139
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 1 deletion.
46 changes: 46 additions & 0 deletions Tests/Command/TranslationPushCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,52 @@ public function testPushNewMessages()
$this->assertStringContainsString('[OK] New local translations has been sent to "null" (for "en, fr" locale(s), and "messages" domain(s)).', trim($tester->getDisplay()));
}

public function testPushNewIntlIcuMessages()
{
$arrayLoader = new ArrayLoader();
$xliffLoader = new XliffFileLoader();
$locales = ['en', 'fr'];
$domains = ['messages'];

// Simulate existing messages on Provider
$providerReadTranslatorBag = new TranslatorBag();
$providerReadTranslatorBag->addCatalogue($arrayLoader->load(['note' => 'NOTE'], 'en'));
$providerReadTranslatorBag->addCatalogue($arrayLoader->load(['note' => 'NOTE'], 'fr'));

$provider = $this->createMock(ProviderInterface::class);
$provider->expects($this->once())
->method('read')
->with($domains, $locales)
->willReturn($providerReadTranslatorBag);

// Create local files, with a new message
$filenameEn = $this->createFile([
'note' => 'NOTE',
'new.foo' => 'newFooIntlIcu',
], 'en', 'messages+intl-icu.%locale%.xlf');
$filenameFr = $this->createFile([
'note' => 'NOTE',
'new.foo' => 'nouveauFooIntlIcu',
], 'fr', 'messages+intl-icu.%locale%.xlf');
$localTranslatorBag = new TranslatorBag();
$localTranslatorBag->addCatalogue($xliffLoader->load($filenameEn, 'en'));
$localTranslatorBag->addCatalogue($xliffLoader->load($filenameFr, 'fr'));

$provider->expects($this->once())
->method('write')
->with($localTranslatorBag->diff($providerReadTranslatorBag));

$provider->expects($this->once())
->method('__toString')
->willReturn('null://default');

$tester = $this->createCommandTester($provider, $locales, $domains);

$tester->execute(['--locales' => ['en', 'fr'], '--domains' => ['messages']]);

$this->assertStringContainsString('[OK] New local translations has been sent to "null" (for "en, fr" locale(s), and "messages" domain(s)).', trim($tester->getDisplay()));
}

public function testPushForceMessages()
{
$xliffLoader = new XliffFileLoader();
Expand Down
28 changes: 28 additions & 0 deletions Tests/TranslatorBagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,34 @@ public function testDiff()
], $this->getAllMessagesFromTranslatorBag($bagResult));
}

public function testDiffWithIntlDomain()
{
$catalogueA = new MessageCatalogue('en', [
'domain1+intl-icu' => ['foo' => 'foo', 'bar' => 'bar'],
'domain2' => ['baz' => 'baz', 'qux' => 'qux'],
]);

$bagA = new TranslatorBag();
$bagA->addCatalogue($catalogueA);

$catalogueB = new MessageCatalogue('en', [
'domain1' => ['foo' => 'foo'],
'domain2' => ['baz' => 'baz', 'corge' => 'corge'],
]);

$bagB = new TranslatorBag();
$bagB->addCatalogue($catalogueB);

$bagResult = $bagA->diff($bagB);

$this->assertEquals([
'en' => [
'domain1' => ['bar' => 'bar'],
'domain2' => ['qux' => 'qux'],
],
], $this->getAllMessagesFromTranslatorBag($bagResult));
}

public function testIntersect()
{
$catalogueA = new MessageCatalogue('en', ['domain1' => ['foo' => 'foo', 'bar' => 'bar'], 'domain2' => ['baz' => 'baz', 'qux' => 'qux']]);
Expand Down
2 changes: 1 addition & 1 deletion TranslatorBag.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function diff(TranslatorBagInterface $diffBag): self
$operation->moveMessagesToIntlDomainsIfPossible(AbstractOperation::NEW_BATCH);
$newCatalogue = new MessageCatalogue($locale);

foreach ($operation->getDomains() as $domain) {
foreach ($catalogue->getDomains() as $domain) {
$newCatalogue->add($operation->getNewMessages($domain), $domain);
}

Expand Down

0 comments on commit 9a40139

Please sign in to comment.