I18nUsageValidatorTest.php 3.5 KB

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