I18nUsageValidatorTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. private $value;
  6. public function setUp(): void {
  7. $this->value = $this->getMockBuilder(I18nValue::class)
  8. ->disableOriginalConstructor()
  9. ->getMock();
  10. }
  11. public function testDisplayReport() {
  12. $validator = new I18nUsageValidator([], []);
  13. $this->assertEquals("There is no data.\n", $validator->displayReport());
  14. $reflectionTotalEntries = new ReflectionProperty(I18nUsageValidator::class, 'totalEntries');
  15. $reflectionTotalEntries->setAccessible(true);
  16. $reflectionTotalEntries->setValue($validator, 100);
  17. $this->assertEquals(" 0.0% of translation keys are unused.\n", $validator->displayReport());
  18. $reflectionFailedEntries = new ReflectionProperty(I18nUsageValidator::class, 'failedEntries');
  19. $reflectionFailedEntries->setAccessible(true);
  20. $reflectionFailedEntries->setValue($validator, 25);
  21. $this->assertEquals(" 25.0% of translation keys are unused.\n", $validator->displayReport());
  22. $reflectionFailedEntries->setValue($validator, 100);
  23. $this->assertEquals("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 function testValidateWhenNoData() {
  30. $validator = new I18nUsageValidator([], []);
  31. $this->assertTrue($validator->validate());
  32. $this->assertEquals('', $validator->displayResult());
  33. }
  34. public function testValidateWhenParentKeyExistsWithoutTransformation() {
  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. $this->assertTrue($validator->validate());
  47. $this->assertEquals('', $validator->displayResult());
  48. }
  49. public function testValidateWhenParentKeyExistsWithTransformation() {
  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. $this->assertTrue($validator->validate());
  62. $this->assertEquals('', $validator->displayResult());
  63. }
  64. public function testValidateWhenParentKeyDoesNotExist() {
  65. $validator = new I18nUsageValidator([
  66. 'file1' => [
  67. 'file1.l1.l2._' => $this->value,
  68. ],
  69. 'file2' => [
  70. 'file2.l1.l2._' => $this->value,
  71. ],
  72. ], []);
  73. $this->assertFalse($validator->validate());
  74. $this->assertEquals("Unused key file1.l1.l2._ - \nUnused key file2.l1.l2._ - \n", $validator->displayResult());
  75. }
  76. public function testValidateWhenChildKeyExists() {
  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. $this->assertTrue($validator->validate());
  89. $this->assertEquals('', $validator->displayResult());
  90. }
  91. public function testValidateWhenChildKeyDoesNotExist() {
  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. $this->assertFalse($validator->validate());
  101. $this->assertEquals("Unused key file1.l1.l2.k1 - \nUnused key file2.l1.l2.k1 - \n", $validator->displayResult());
  102. }
  103. }