I18nCompletionValidatorTest.php 4.1 KB

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