I18nUsageValidatorTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. declare(strict_types=1);
  3. require_once __DIR__ . '/../../../cli/i18n/I18nValue.php';
  4. require_once __DIR__ . '/../../../cli/i18n/I18nUsageValidator.php';
  5. class I18nUsageValidatorTest extends PHPUnit\Framework\TestCase {
  6. private I18nValue $value;
  7. #[\Override]
  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 I18nUsageValidator([], []);
  15. self::assertSame("There is no data.\n", $validator->displayReport());
  16. $reflectionTotalEntries = new ReflectionProperty(I18nUsageValidator::class, 'totalEntries');
  17. $reflectionTotalEntries->setAccessible(true);
  18. $reflectionTotalEntries->setValue($validator, 100);
  19. self::assertSame(" 0.0% of translation keys are unused.\n", $validator->displayReport());
  20. $reflectionFailedEntries = new ReflectionProperty(I18nUsageValidator::class, 'failedEntries');
  21. $reflectionFailedEntries->setAccessible(true);
  22. $reflectionFailedEntries->setValue($validator, 25);
  23. self::assertSame(" 25.0% of translation keys are unused.\n", $validator->displayReport());
  24. $reflectionFailedEntries->setValue($validator, 100);
  25. self::assertSame("100.0% of translation keys are unused.\n", $validator->displayReport());
  26. $reflectionFailedEntries->setValue($validator, 200);
  27. $this->expectException(\RuntimeException::class);
  28. $this->expectExceptionMessage('The number of unused strings cannot be higher than the number of strings');
  29. $validator->displayReport();
  30. }
  31. public static function testValidateWhenNoData(): void {
  32. $validator = new I18nUsageValidator([], []);
  33. self::assertTrue($validator->validate());
  34. self::assertSame('', $validator->displayResult());
  35. }
  36. public function testValidateWhenParentKeyExistsWithoutTransformation(): void {
  37. $validator = new I18nUsageValidator([
  38. 'file1' => [
  39. 'file1.l1.l2._' => $this->value,
  40. ],
  41. 'file2' => [
  42. 'file2.l1.l2._' => $this->value,
  43. ],
  44. ], [
  45. 'file1.l1.l2._',
  46. 'file2.l1.l2._',
  47. ]);
  48. self::assertTrue($validator->validate());
  49. self::assertSame('', $validator->displayResult());
  50. }
  51. public function testValidateWhenParentKeyExistsWithTransformation(): void {
  52. $validator = new I18nUsageValidator([
  53. 'file1' => [
  54. 'file1.l1.l2._' => $this->value,
  55. ],
  56. 'file2' => [
  57. 'file2.l1.l2._' => $this->value,
  58. ],
  59. ], [
  60. 'file1.l1.l2',
  61. 'file2.l1.l2',
  62. ]);
  63. self::assertTrue($validator->validate());
  64. self::assertSame('', $validator->displayResult());
  65. }
  66. public function testValidateWhenParentKeyDoesNotExist(): void {
  67. $validator = new I18nUsageValidator([
  68. 'file1' => [
  69. 'file1.l1.l2._' => $this->value,
  70. ],
  71. 'file2' => [
  72. 'file2.l1.l2._' => $this->value,
  73. ],
  74. ], []);
  75. self::assertFalse($validator->validate());
  76. self::assertSame("Unused key file1.l1.l2._ - \nUnused key file2.l1.l2._ - \n", $validator->displayResult());
  77. }
  78. public function testValidateWhenChildKeyExists(): void {
  79. $validator = new I18nUsageValidator([
  80. 'file1' => [
  81. 'file1.l1.l2.k1' => $this->value,
  82. ],
  83. 'file2' => [
  84. 'file2.l1.l2.k1' => $this->value,
  85. ],
  86. ], [
  87. 'file1.l1.l2.k1',
  88. 'file2.l1.l2.k1',
  89. ]);
  90. self::assertTrue($validator->validate());
  91. self::assertSame('', $validator->displayResult());
  92. }
  93. public function testValidateWhenChildKeyDoesNotExist(): void {
  94. $validator = new I18nUsageValidator([
  95. 'file1' => [
  96. 'file1.l1.l2.k1' => $this->value,
  97. ],
  98. 'file2' => [
  99. 'file2.l1.l2.k1' => $this->value,
  100. ],
  101. ], []);
  102. self::assertFalse($validator->validate());
  103. self::assertSame("Unused key file1.l1.l2.k1 - \nUnused key file2.l1.l2.k1 - \n", $validator->displayResult());
  104. }
  105. }