Test Failed
Push — master ( 8413e5...65cae7 )
by Jakub
02:11
created

TranslatorTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 132
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 95
dl 0
loc 132
rs 10
c 1
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testTranslateEn() 0 39 1
A testTranslateX() 0 35 1
A setUp() 0 5 1
A testTranslateCs() 0 41 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Nexendrie\Translation;
5
6
use Tester\Assert;
7
8
require __DIR__ . "/../../bootstrap.php";
9
10
11
/**
12
 * TranslatorTest
13
 *
14
 * @author Jakub Konečný
15
 * @testCase
16
 */
17
final class TranslatorTest extends \Tester\TestCase {
18
  private Translator $translator;
19
  
20
  public function setUp(): void {
21
    $loader = new Loaders\NeonLoader();
22
    $loader->folders = [__DIR__ . "/../../lang", __DIR__ . "/../../lang2"];
23
    $this->translator = new Translator($loader);
24
    $this->translator->onUntranslated[] = [$this->translator, "logUntranslatedMessage"];
25
  }
26
  
27
  public function testTranslateEn(): void {
28
    Assert::count(0, $this->translator->untranslated);
29
    Assert::same("en", $this->translator->lang);
30
    // non-existing string
31
    Assert::type("string", $this->translator->translate("abc"));
32
    Assert::same("abc", $this->translator->translate("abc"));
33
    // existing string
34
    Assert::type("string", $this->translator->translate("book.content"));
35
    Assert::same("Content", $this->translator->translate("book.content"));
36
    Assert::same("XYZ", $this->translator->translate("xyz"));
37
    // parameters
38
    $result = $this->translator->translate("param", 0, ["param1" => "value1"]);
39
    Assert::type("string", $result);
40
    Assert::same("Param1: value1", $result);
41
    // string existing only in default translation
42
    Assert::type("string", $this->translator->translate("test"));
43
    Assert::same("Test", $this->translator->translate("test"));
44
    Assert::count(2, $this->translator->untranslated);
45
    // multi-level message
46
    Assert::type("string", $this->translator->translate("abc.multi.abc"));
47
    Assert::same("ABC", $this->translator->translate("abc.multi.abc"));
48
    Assert::same("abc.multi.def", $this->translator->translate("abc.multi.def"));
49
    Assert::type("string", $this->translator->translate("abc.multi2.def"));
50
    Assert::same("abc.multi2.def", $this->translator->translate("abc.multi2.def"));
51
    // plurals
52
    Assert::same("There are no apples.", $this->translator->translate("abc.pluralSimple", 0));
53
    Assert::same("There is one apple.", $this->translator->translate("abc.pluralSimple", 1));
54
    Assert::same("", $this->translator->translate("abc.pluralSimple", 5));
55
    Assert::same("There are 0 apples.", $this->translator->translate("abc.pluralSimpleParams", 0));
56
    Assert::same("There is 1 apple.", $this->translator->translate("abc.pluralSimpleParams", 1));
57
    Assert::same("There are no apples.", $this->translator->translate("abc.multi.pluralSimple", 0));
58
    Assert::same("There is one apple.", $this->translator->translate("abc.multi.pluralSimple", 1));
59
    Assert::same("There are 0 apples.", $this->translator->translate("abc.multi.pluralSimpleParams", 0));
60
    Assert::same("There is 1 apple.", $this->translator->translate("abc.multi.pluralSimpleParams", 1));
61
    // new style count
62
    Assert::same("There are no apples.", $this->translator->translate("abc.pluralSimple", ["count" => 0]));
63
    Assert::same("Param1: value1", $this->translator->translate("param", ["param1" => "value1"]));
64
    // test untranslated messages
65
    Assert::count(5, $this->translator->untranslated);
66
  }
67
  
68
  public function testTranslateCs(): void {
69
    Assert::count(0, $this->translator->untranslated);
70
    $this->translator->lang = "cs";
71
    Assert::same("cs", $this->translator->lang);
72
    // non-existing string
73
    Assert::type("string", $this->translator->translate("abc"));
74
    Assert::same("abc", $this->translator->translate("abc"));
75
    Assert::count(2, $this->translator->untranslated);
76
    // existing string
77
    Assert::type("string", $this->translator->translate("book.content"));
78
    Assert::same("Obsah", $this->translator->translate("book.content"));
79
    Assert::same("xyz", $this->translator->translate("xyz"));
80
    // parameters
81
    $result = $this->translator->translate("param", 0, ["param1" => "value1"]);
82
    Assert::type("string", $result);
83
    Assert::same("Param2: value1", $result);
84
    // string existing only in default translation
85
    Assert::type("string", $this->translator->translate("test"));
86
    Assert::same("Test", $this->translator->translate("test"));
87
    Assert::count(2, $this->translator->untranslated);
88
    // multi-level message
89
    Assert::type("string", $this->translator->translate("abc.multi.abc"));
90
    Assert::same("Abc", $this->translator->translate("abc.multi.abc"));
91
    Assert::same("abc.multi.def", $this->translator->translate("abc.multi.def"));
92
    Assert::type("string", $this->translator->translate("abc.multi2.def"));
93
    Assert::same("abc.multi2.def", $this->translator->translate("abc.multi2.def"));
94
    // plurals
95
    Assert::same("Není tu žádné jablko.", $this->translator->translate("abc.pluralSimple", 0));
96
    Assert::same("Je tu jedno jablko.", $this->translator->translate("abc.pluralSimple", 1));
97
    Assert::same("", $this->translator->translate("abc.pluralSimple", 5));
98
    Assert::same("Je tu 0 jablek.", $this->translator->translate("abc.pluralSimpleParams", 0));
99
    Assert::same("Je tu 1 jablko.", $this->translator->translate("abc.pluralSimpleParams", 1));
100
    Assert::same("Není tu žádné jablko.", $this->translator->translate("abc.multi.pluralSimple", 0));
101
    Assert::same("Je tu jedno jablko.", $this->translator->translate("abc.multi.pluralSimple", 1));
102
    Assert::same("Je tu 0 jablek.", $this->translator->translate("abc.multi.pluralSimpleParams", 0));
103
    Assert::same("Je tu 1 jablko.", $this->translator->translate("abc.multi.pluralSimpleParams", 1));
104
    // new style count
105
    Assert::same("Není tu žádné jablko.", $this->translator->translate("abc.pluralSimple", ["count" => 0]));
106
    Assert::same("Param2: value1", $this->translator->translate("param", ["param1" => "value1"]));
107
    // test untranslated messages
108
    Assert::count(5, $this->translator->untranslated);
109
  }
110
  
111
  /**
112
   * Test non-existing language
113
   */
114
  public function testTranslateX(): void {
115
    Assert::count(0, $this->translator->untranslated);
116
    $this->translator->lang = "x";
117
    Assert::same("x", $this->translator->lang);
118
    // non-existing string
119
    Assert::type("string", $this->translator->translate("abc"));
120
    Assert::same("abc", $this->translator->translate("abc"));
121
    // existing string
122
    Assert::type("string", $this->translator->translate("book.content"));
123
    Assert::same("Content", $this->translator->translate("book.content"));
124
    // string existing only in default translation
125
    Assert::type("string", $this->translator->translate("test"));
126
    Assert::same("Test", $this->translator->translate("test"));
127
    Assert::count(2, $this->translator->untranslated);
128
    // multi-level message
129
    Assert::type("string", $this->translator->translate("abc.multi.abc"));
130
    Assert::same("ABC", $this->translator->translate("abc.multi.abc"));
131
    Assert::same("abc.multi.def", $this->translator->translate("abc.multi.def"));
132
    Assert::type("string", $this->translator->translate("abc.multi2.def"));
133
    Assert::same("abc.multi2.def", $this->translator->translate("abc.multi2.def"));
134
    // plurals
135
    Assert::same("There are no apples.", $this->translator->translate("abc.pluralSimple", 0));
136
    Assert::same("There is one apple.", $this->translator->translate("abc.pluralSimple", 1));
137
    Assert::same("", $this->translator->translate("abc.pluralSimple", 5));
138
    Assert::same("There are 0 apples.", $this->translator->translate("abc.pluralSimpleParams", 0));
139
    Assert::same("There is 1 apple.", $this->translator->translate("abc.pluralSimpleParams", 1));
140
    Assert::same("There are no apples.", $this->translator->translate("abc.multi.pluralSimple", 0));
141
    Assert::same("There is one apple.", $this->translator->translate("abc.multi.pluralSimple", 1));
142
    Assert::same("There are 0 apples.", $this->translator->translate("abc.multi.pluralSimpleParams", 0));
143
    Assert::same("There is 1 apple.", $this->translator->translate("abc.multi.pluralSimpleParams", 1));
144
    // new style count
145
    Assert::same("There are no apples.", $this->translator->translate("abc.pluralSimple", ["count" => 0]));
146
    Assert::same("Param1: value1", $this->translator->translate("param", ["param1" => "value1"]));
147
      // test untranslated messages
148
    Assert::count(5, $this->translator->untranslated);
149
  }
150
}
151
152
$test = new TranslatorTest();
153
$test->run();
154
?>