I18nCompletionValidatorTest.php 4.1 KB

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