-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathTranslatorServiceTest.php
More file actions
110 lines (96 loc) · 4.13 KB
/
TranslatorServiceTest.php
File metadata and controls
110 lines (96 loc) · 4.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
declare(strict_types=1);
/**
* This source file is available under the terms of the
* Pimcore Open Core License (POCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (https://www.pimcore.com)
* @license Pimcore Open Core License (POCL)
*/
namespace Pimcore\Bundle\StudioBackendBundle\Tests\Unit\Service\Translator;
use Codeception\Test\Unit;
use Exception;
use Pimcore\Bundle\StaticResolverBundle\Lib\CacheResolverInterface;
use Pimcore\Bundle\StaticResolverBundle\Lib\ToolResolverInterface;
use Pimcore\Bundle\StudioBackendBundle\Listing\Service\FilterMapperServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\Listing\Service\ListingFilterInterface;
use Pimcore\Bundle\StudioBackendBundle\Security\Service\SecurityServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\Translation\Hydrator\TranslationsHydratorInterface;
use Pimcore\Bundle\StudioBackendBundle\Translation\Repository\TranslationRepositoryInterface;
use Pimcore\Bundle\StudioBackendBundle\Translation\Service\AdminLanguageServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\Translation\Service\TranslatorService;
use Pimcore\Bundle\StudioBackendBundle\Translation\Service\TranslatorServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\Util\Constant\PublicTranslations;
use Pimcore\Config;
use Pimcore\Translation\Translator;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use function count;
final class TranslatorServiceTest extends Unit
{
/**
* @throws Exception
*/
public function testGetAllTranslations(): void
{
$translatorService = $this->mockTranslatorService();
$locale = 'en';
$translations = $translatorService->getAllTranslationsByLocale($locale, true);
$this->assertEquals($locale, $translations->getLocale());
$this->assertEmpty($translations->getKeys());
}
public function testGetAllTranslationsNotLoggedIn(): void
{
$translatorService = $this->mockTranslatorService(false);
$locale = 'en';
$translations = $translatorService->getAllTranslationsByLocale($locale, true);
$this->assertEquals($locale, $translations->getLocale());
$this->assertCount(count(PublicTranslations::PUBLIC_KEYS), $translations->getKeys());
}
/**
* @throws Exception
*/
public function testGetTranslationsForKeys(): void
{
$translatorService = $this->mockTranslatorService();
$locale = 'fr';
$keys = PublicTranslations::PUBLIC_KEYS;
$translations = $translatorService->getTranslationsForKeys($locale, $keys);
$this->assertEquals($locale, $translations->getLocale());
foreach ($keys as $key) {
$this->assertArrayHasKey($key, $translations->getKeys());
}
}
/**
* @throws Exception
*/
private function mockTranslatorService(bool $loggedIn = true): TranslatorServiceInterface
{
$translator = $this->makeEmpty(Translator::class);
$repository = $this->makeEmpty(TranslationRepositoryInterface::class);
$securityService = $this->makeEmpty(SecurityServiceInterface::class, [
'isLoggedIn' => $loggedIn,
]);
$adminLanguageService = $this->makeEmpty(AdminLanguageServiceInterface::class);
$listingFilter = $this->makeEmpty(ListingFilterInterface::class);
$filterMapper = $this->makeEmpty(FilterMapperServiceInterface::class);
$translationsHydrator = $this->makeEmpty(TranslationsHydratorInterface::class);
$eventDispatcher = $this->makeEmpty(EventDispatcherInterface::class);
$cacheResolver = $this->makeEmpty(CacheResolverInterface::class);
$toolResolver = $this->makeEmpty(ToolResolverInterface::class);
return new TranslatorService(
new Config(),
$translator,
$repository,
$securityService,
$adminLanguageService,
$listingFilter,
$filterMapper,
$translationsHydrator,
$eventDispatcher,
$cacheResolver,
$toolResolver
);
}
}