Skip to content
This repository was archived by the owner on Dec 15, 2024. It is now read-only.

Commit 0aad386

Browse files
fix: TranslationService not available publicly; add: tests for TranslationService
1 parent 96cf0f3 commit 0aad386

File tree

6 files changed

+80
-11
lines changed

6 files changed

+80
-11
lines changed

src/Command/TranslationReloadCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
8585
$io->success(sprintf('Yay, a total success! Added %s translations in total.', $translationsTotal));
8686

8787
return 0;
88-
}
88+
}
8989

9090
$io->error(sprintf('Uh oh. Something went wrong. Please try again in a few seconds (updated translations: %s)', $translationsTotal));
9191

src/Resources/config/services.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414
<argument>%googlesheets_translations.sheet.publicId%</argument>
1515
<argument>%googlesheets_translations.sheet.mode%</argument>
1616
</service>
17-
<service id="Phiil\GoogleSheetsTranslationBundle\Service\GoogleSheetsService" alias="phil_googlesheets_translations_bundle.googlesheets_service" public="false" />
17+
<service id="Phiil\GoogleSheetsTranslationBundle\Service\GoogleSheetsService" alias="phil_googlesheets_translations_bundle.googlesheets_service" public="true" />
1818

19-
<service id="phil_googlesheets_translations_bundle.translation_service" class="Phiil\GoogleSheetsTranslationBundle\Service\TranslationService" public="false">
19+
<service id="phil_googlesheets_translations_bundle.translation_service" class="Phiil\GoogleSheetsTranslationBundle\Service\TranslationService" public="true">
2020
<argument type="service" id="Phiil\GoogleSheetsTranslationBundle\Service\GoogleSheetsService"/>
2121
</service>
22-
<service id="Phiil\GoogleSheetsTranslationBundle\Service\TranslationService" alias="phil_googlesheets_translations_bundle.translation_service" public="false" />
22+
<service id="Phiil\GoogleSheetsTranslationBundle\Service\TranslationService" alias="phil_googlesheets_translations_bundle.translation_service" public="true" />
2323

2424
<service id="phil_googlesheets_translations_bundle.translation_loader" class="Phiil\GoogleSheetsTranslationBundle\Translation\TranslationLoader" public="true">
2525
<argument type="service" id="Phiil\GoogleSheetsTranslationBundle\Service\GoogleSheetsService"/>

src/Service/TranslationService.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,20 @@
33
namespace Phiil\GoogleSheetsTranslationBundle\Service;
44

55
use Phiil\GoogleSheetsTranslationBundle\Service\GoogleSheetsService;
6+
use Symfony\Contracts\Translation\TranslatorInterface;
67

78
class TranslationService
89
{
9-
private $sheetsService;
10+
protected $sheetsService;
11+
protected $translator;
1012

1113
public function __construct(GoogleSheetsService $sheetsService)
1214
{
1315
$this->sheetsService = $sheetsService;
1416
}
1517

16-
public function localeIsSupported($locale)
18+
public function localeIsSupported(string $locale): bool
1719
{
1820
return in_array($locale, array_values($this->sheetsService->getLocales()));
1921
}
20-
21-
public function trans($string)
22-
{
23-
return $this->translator->trans($string);
24-
}
2522
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Phiil\GoogleSheetsTranslationBundle\Tests\Fixtures;
4+
5+
class PhiilSheetTestCase extends PhiilTestCase
6+
{
7+
protected function getContainer()
8+
{
9+
$kernel = new TestSheetKernel('test', true);
10+
$kernel->boot();
11+
12+
return $kernel->getContainer();
13+
}
14+
}

tests/Fixtures/TestSheetKernel.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Phiil\GoogleSheetsTranslationBundle\Tests\Fixtures;
4+
5+
use Symfony\Component\DependencyInjection\ContainerBuilder;
6+
7+
class TestSheetKernel extends TestKernel
8+
{
9+
/**
10+
* Add kernel.secret because some packages depend on it
11+
*/
12+
public function build(ContainerBuilder $container)
13+
{
14+
parent::build($container); // TODO: Change the autogenerated stub
15+
$container->setParameter('googlesheets_translations.sheet.publicId', '1E9r6LCSeQV6rrEVq2YuSnm1OEbbTjgc50G9OHHUvihk');
16+
}
17+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace Phiil\GoogleSheetsTranslationBundle\Tests\Service;
4+
5+
use Phiil\GoogleSheetsTranslationBundle\Service\TranslationService;
6+
use Phiil\GoogleSheetsTranslationBundle\Tests\Fixtures\PhiilSheetTestCase;
7+
8+
class TranslationServiceTest extends PhiilSheetTestCase
9+
{
10+
public function testLocaleIsSupported_supportedLocales()
11+
{
12+
$service = $this->_getService();
13+
14+
$this->assertTrue($service->localeIsSupported('en'), 'Translation service returned that locale "en" is not supported.');
15+
$this->assertTrue($service->localeIsSupported('de'), 'Translation service returned that locale "de" is not supported.');
16+
}
17+
18+
public function testLocaleIsSupported_unsupportedLocales()
19+
{
20+
$service = $this->_getService();
21+
22+
$this->assertFalse($service->localeIsSupported('cats'), 'Translation service returned that locale "cats" is supported.');
23+
$this->assertFalse($service->localeIsSupported('nomnom'), 'Translation service returned that locale "nomnom" is supported.');
24+
}
25+
26+
public function testLocaleIsSupported_emptyLocale()
27+
{
28+
$service = $this->_getService();
29+
30+
$this->assertFalse($service->localeIsSupported(''), 'Translation service returned that empty locale is supported.');
31+
}
32+
33+
/**
34+
* HELPER FUNCTIONS TO EXECUTE THE TESTS
35+
*/
36+
37+
private function _getService(): TranslationService
38+
{
39+
return $this->get('phil_googlesheets_translations_bundle.translation_service');
40+
}
41+
}

0 commit comments

Comments
 (0)