I18nUsageValidatorTest.php 3.6 KB

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