IndirectInternationalTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\LookupRef;
  3. use PhpOffice\PhpSpreadsheet\Calculation\Functions;
  4. use PhpOffice\PhpSpreadsheet\Settings;
  5. class IndirectInternationalTest extends AllSetupTeardown
  6. {
  7. /** @var string */
  8. private $locale;
  9. protected function setUp(): void
  10. {
  11. parent::setUp();
  12. $this->locale = Settings::getLocale();
  13. }
  14. protected function tearDown(): void
  15. {
  16. Settings::setLocale($this->locale);
  17. // CompatibilityMode is restored in parent
  18. parent::tearDown();
  19. }
  20. /**
  21. * @dataProvider providerInternational
  22. */
  23. public function testR1C1International(string $locale): void
  24. {
  25. Settings::setLocale($locale);
  26. $sameAsEnglish = ['en', 'xx', 'ru', 'tr', 'cs', 'pl'];
  27. $sheet = $this->getSheet();
  28. $sheet->getCell('C1')->setValue('text');
  29. $sheet->getCell('A2')->setValue('en');
  30. $sheet->getCell('B2')->setValue('=INDIRECT("R1C3", false)');
  31. $sheet->getCell('A3')->setValue('fr');
  32. $sheet->getCell('B3')->setValue('=INDIRECT("L1C3", false)');
  33. $sheet->getCell('A4')->setValue('de');
  34. $sheet->getCell('B4')->setValue('=INDIRECT("Z1S3", false)');
  35. $sheet->getCell('A5')->setValue('es');
  36. $sheet->getCell('B5')->setValue('=INDIRECT("F1C3", false)');
  37. $sheet->getCell('A6')->setValue('xx');
  38. $sheet->getCell('B6')->setValue('=INDIRECT("R1C3", false)');
  39. $sheet->getCell('A7')->setValue('ru');
  40. $sheet->getCell('B7')->setValue('=INDIRECT("R1C3", false)');
  41. $sheet->getCell('A8')->setValue('cs');
  42. $sheet->getCell('B8')->setValue('=INDIRECT("R1C3", false)');
  43. $sheet->getCell('A9')->setValue('tr');
  44. $sheet->getCell('B9')->setValue('=INDIRECT("R1C3", false)');
  45. $sheet->getCell('A10')->setValue('pl');
  46. $sheet->getCell('B10')->setValue('=INDIRECT("R1C3", false)');
  47. $maxRow = $sheet->getHighestRow();
  48. for ($row = 2; $row <= $maxRow; ++$row) {
  49. $rowLocale = $sheet->getCell("A$row")->getValue();
  50. if (in_array($rowLocale, $sameAsEnglish, true) && in_array($locale, $sameAsEnglish, true)) {
  51. $expectedResult = 'text';
  52. } else {
  53. $expectedResult = ($locale === $sheet->getCell("A$row")->getValue()) ? 'text' : '#REF!';
  54. }
  55. self::assertSame($expectedResult, $sheet->getCell("B$row")->getCalculatedValue(), "Locale $locale error in cell B$row $rowLocale");
  56. }
  57. }
  58. public function providerInternational(): array
  59. {
  60. return [
  61. 'English' => ['en'],
  62. 'French' => ['fr'],
  63. 'German' => ['de'],
  64. 'Made-up' => ['xx'],
  65. 'Spanish' => ['es'],
  66. 'Russian' => ['ru'],
  67. 'Czech' => ['cs'],
  68. 'Polish' => ['pl'],
  69. 'Turkish' => ['tr'],
  70. ];
  71. }
  72. /**
  73. * @dataProvider providerRelativeInternational
  74. */
  75. public function testRelativeInternational(string $locale, string $cell, string $relative): void
  76. {
  77. Settings::setLocale($locale);
  78. $sheet = $this->getSheet();
  79. $sheet->getCell('C3')->setValue('text');
  80. $sheet->getCell($cell)->setValue("=INDIRECT(\"$relative\", false)");
  81. self::assertSame('text', $sheet->getCell($cell)->getCalculatedValue());
  82. }
  83. public function providerRelativeInternational(): array
  84. {
  85. return [
  86. 'English A3' => ['en', 'A3', 'R[]C[+2]'],
  87. 'French B4' => ['fr', 'B4', 'L[-1]C[+1]'],
  88. 'German C5' => ['de', 'C5', 'Z[-2]S[]'],
  89. 'Spanish E1' => ['es', 'E1', 'F[+2]C[-2]'],
  90. ];
  91. }
  92. /**
  93. * @dataProvider providerCompatibility
  94. */
  95. public function testCompatibilityInternational(string $compatibilityMode): void
  96. {
  97. Functions::setCompatibilityMode($compatibilityMode);
  98. if ($compatibilityMode === Functions::COMPATIBILITY_EXCEL) {
  99. $expected1 = '#REF!';
  100. $expected2 = 'text';
  101. } else {
  102. $expected2 = '#REF!';
  103. $expected1 = 'text';
  104. }
  105. Settings::setLocale('fr');
  106. $sheet = $this->getSheet();
  107. $sheet->getCell('C3')->setValue('text');
  108. $sheet->getCell('A1')->setValue('=INDIRECT("R3C3", false)');
  109. $sheet->getCell('A2')->setValue('=INDIRECT("L3C3", false)');
  110. self::assertSame($expected1, $sheet->getCell('A1')->getCalculatedValue());
  111. self::assertSame($expected2, $sheet->getCell('A2')->getCalculatedValue());
  112. }
  113. public function providerCompatibility(): array
  114. {
  115. return [
  116. [Functions::COMPATIBILITY_EXCEL],
  117. [Functions::COMPATIBILITY_OPENOFFICE],
  118. [Functions::COMPATIBILITY_GNUMERIC],
  119. ];
  120. }
  121. }