I18nCompletionValidatorTest.php 4.0 KB

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