1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Validator\Tests; |
6
|
|
|
|
7
|
|
|
use Yiisoft\Translator\CategorySource; |
8
|
|
|
use Yiisoft\Translator\SimpleMessageFormatter; |
9
|
|
|
use Yiisoft\Validator\RuleHandlerResolverInterface; |
10
|
|
|
use Yiisoft\Validator\SimpleRuleHandlerContainer; |
11
|
|
|
use Yiisoft\Validator\Validator; |
12
|
|
|
use Yiisoft\Validator\ValidatorInterface; |
13
|
|
|
|
14
|
|
|
final class ConfigTest extends BaseConfigTest |
15
|
|
|
{ |
16
|
|
|
public function testBase(): void |
17
|
|
|
{ |
18
|
|
|
$container = $this->createContainer(); |
19
|
|
|
|
20
|
|
|
$validator = $container->get(ValidatorInterface::class); |
21
|
|
|
$this->assertInstanceOf(Validator::class, $validator); |
22
|
|
|
|
23
|
|
|
$ruleHandlerResolver = $container->get(RuleHandlerResolverInterface::class); |
24
|
|
|
$this->assertInstanceOf(SimpleRuleHandlerContainer::class, $ruleHandlerResolver); |
25
|
|
|
|
26
|
|
|
/** @var CategorySource[] $translationCategorySources */ |
27
|
|
|
$translationCategorySources = $container->get('[email protected]'); |
28
|
|
|
$this->assertCount(1, $translationCategorySources); |
29
|
|
|
|
30
|
|
|
$translationCategorySource = $translationCategorySources[0]; |
31
|
|
|
$this->assertInstanceOf(CategorySource::class, $translationCategorySource); |
32
|
|
|
$this->assertSame(Validator::DEFAULT_TRANSLATION_CATEGORY, $translationCategorySource->getName()); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function testCustomTranslationCategory(): void |
36
|
|
|
{ |
37
|
|
|
$params = [ |
38
|
|
|
'yiisoft/validator' => [ |
39
|
|
|
'translation.category' => 'yii-validator-custom', |
40
|
|
|
], |
41
|
|
|
]; |
42
|
|
|
$container = $this->createContainer($params); |
43
|
|
|
|
44
|
|
|
/** @var CategorySource $translationCategorySource */ |
45
|
|
|
$translationCategorySource = $container->get('[email protected]')[0]; |
46
|
|
|
$this->assertSame('yii-validator-custom', $translationCategorySource->getName()); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function testTranslationCategorySource(): void |
50
|
|
|
{ |
51
|
|
|
$container = $this->createContainer(); |
52
|
|
|
|
53
|
|
|
/** @var CategorySource[] $translationCategorySource */ |
54
|
|
|
$translationCategorySources = $container->get('[email protected]'); |
55
|
|
|
$this->assertCount(1, $translationCategorySources); |
56
|
|
|
|
57
|
|
|
$translationCategorySource = $translationCategorySources[0]; |
58
|
|
|
$this->assertInstanceOf(CategorySource::class, $translationCategorySource); |
59
|
|
|
|
60
|
|
|
$this->assertSame('Значение неверно.', $translationCategorySource->getMessage('This value is invalid.', 'ru')); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function testIntlMessageFormatter(): void |
64
|
|
|
{ |
65
|
|
|
$container = $this->createContainer(); |
66
|
|
|
|
67
|
|
|
/** @var CategorySource $translationCategorySource */ |
68
|
|
|
$translationCategorySource = $container->get('[email protected]')[0]; |
69
|
|
|
$message = '{n, selectordinal, one{#-one} two{#-two} few{#-few} other{#-other}}'; |
70
|
|
|
// The default formatter argument is ignored in favor of formatter set in config. |
71
|
|
|
$this->assertSame( |
72
|
|
|
'1-one', |
73
|
|
|
$translationCategorySource->format($message, ['n' => 1], 'en', new SimpleMessageFormatter()), |
74
|
|
|
); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|