I18nCompletionValidatorTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. require_once __DIR__ . '/../../../cli/i18n/I18nCompletionValidator.php';
  3. require_once __DIR__ . '/../../../cli/i18n/I18nValue.php';
  4. class I18nCompletionValidatorTest extends PHPUnit\Framework\TestCase {
  5. private $value;
  6. public function setUp(): void {
  7. $this->value = $this->getMockBuilder(I18nValue::class)
  8. ->disableOriginalConstructor()
  9. ->getMock();
  10. }
  11. public function testDisplayReport() {
  12. $validator = new I18nCompletionValidator([], []);
  13. $this->assertEquals("There is no data.\n", $validator->displayReport());
  14. $reflectionTotalEntries = new ReflectionProperty(I18nCompletionValidator::class, 'totalEntries');
  15. $reflectionTotalEntries->setAccessible(true);
  16. $reflectionTotalEntries->setValue($validator, 100);
  17. $this->assertEquals("Translation is 0.0% complete.\n", $validator->displayReport());
  18. $reflectionPassEntries = new ReflectionProperty(I18nCompletionValidator::class, 'passEntries');
  19. $reflectionPassEntries->setAccessible(true);
  20. $reflectionPassEntries->setValue($validator, 25);
  21. $this->assertEquals("Translation is 25.0% complete.\n", $validator->displayReport());
  22. $reflectionPassEntries->setValue($validator, 100);
  23. $this->assertEquals("Translation is 100.0% complete.\n", $validator->displayReport());
  24. $reflectionPassEntries->setValue($validator, 200);
  25. $this->expectException(\RuntimeException::class);
  26. $this->expectExceptionMessage('The number of translated strings cannot be higher than the number of strings');
  27. $validator->displayReport();
  28. }
  29. public function testValidateWhenNoData() {
  30. $validator = new I18nCompletionValidator([], []);
  31. $this->assertTrue($validator->validate());
  32. $this->assertEquals('', $validator->displayResult());
  33. }
  34. public function testValidateWhenKeyIsMissing() {
  35. $validator = new I18nCompletionValidator([
  36. 'file1.php' => [
  37. 'file1.l1.l2.k1' => $this->value,
  38. ],
  39. 'file2.php' => [
  40. 'file2.l1.l2.k1' => $this->value,
  41. ],
  42. ], []);
  43. $this->assertFalse($validator->validate());
  44. $this->assertEquals("Missing key file1.l1.l2.k1\nMissing key file2.l1.l2.k1\n", $validator->displayResult());
  45. }
  46. public function testValidateWhenKeyIsIgnored() {
  47. $this->value->expects($this->exactly(2))
  48. ->method('isIgnore')
  49. ->willReturn(true);
  50. $validator = new I18nCompletionValidator([
  51. 'file1.php' => [
  52. 'file1.l1.l2.k1' => $this->value,
  53. ],
  54. 'file2.php' => [
  55. 'file2.l1.l2.k1' => $this->value,
  56. ],
  57. ], [
  58. 'file1.php' => [
  59. 'file1.l1.l2.k1' => $this->value,
  60. ],
  61. 'file2.php' => [
  62. 'file2.l1.l2.k1' => $this->value,
  63. ],
  64. ]);
  65. $this->assertTrue($validator->validate());
  66. $this->assertEquals('', $validator->displayResult());
  67. }
  68. public function testValidateWhenValueIsEqual() {
  69. $this->value->expects($this->exactly(2))
  70. ->method('isIgnore')
  71. ->willReturn(false);
  72. $this->value->expects($this->exactly(2))
  73. ->method('equal')
  74. ->willReturn(true);
  75. $validator = new I18nCompletionValidator([
  76. 'file1.php' => [
  77. 'file1.l1.l2.k1' => $this->value,
  78. ],
  79. 'file2.php' => [
  80. 'file2.l1.l2.k1' => $this->value,
  81. ],
  82. ], [
  83. 'file1.php' => [
  84. 'file1.l1.l2.k1' => $this->value,
  85. ],
  86. 'file2.php' => [
  87. 'file2.l1.l2.k1' => $this->value,
  88. ],
  89. ]);
  90. $this->assertFalse($validator->validate());
  91. $this->assertEquals("Untranslated key file1.l1.l2.k1 - \nUntranslated key file2.l1.l2.k1 - \n", $validator->displayResult());
  92. }
  93. public function testValidateWhenValueIsDifferent() {
  94. $this->value->expects($this->exactly(2))
  95. ->method('isIgnore')
  96. ->willReturn(false);
  97. $this->value->expects($this->exactly(2))
  98. ->method('equal')
  99. ->willReturn(false);
  100. $validator = new I18nCompletionValidator([
  101. 'file1.php' => [
  102. 'file1.l1.l2.k1' => $this->value,
  103. ],
  104. 'file2.php' => [
  105. 'file2.l1.l2.k1' => $this->value,
  106. ],
  107. ], [
  108. 'file1.php' => [
  109. 'file1.l1.l2.k1' => $this->value,
  110. ],
  111. 'file2.php' => [
  112. 'file2.l1.l2.k1' => $this->value,
  113. ],
  114. ]);
  115. $this->assertTrue($validator->validate());
  116. $this->assertEquals('', $validator->displayResult());
  117. }
  118. }