RightTest.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\TextData;
  3. use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
  4. use PhpOffice\PhpSpreadsheet\Calculation\Functions;
  5. use PhpOffice\PhpSpreadsheet\Settings;
  6. class RightTest extends AllSetupTeardown
  7. {
  8. /**
  9. * @dataProvider providerRIGHT
  10. *
  11. * @param mixed $expectedResult
  12. * @param mixed $str string from which to extract
  13. * @param mixed $cnt number of characters to extract
  14. */
  15. public function testRIGHT($expectedResult, $str = 'omitted', $cnt = 'omitted'): void
  16. {
  17. $this->mightHaveException($expectedResult);
  18. $sheet = $this->getSheet();
  19. if ($str === 'omitted') {
  20. $sheet->getCell('B1')->setValue('=RIGHT()');
  21. } elseif ($cnt === 'omitted') {
  22. $this->setCell('A1', $str);
  23. $sheet->getCell('B1')->setValue('=RIGHT(A1)');
  24. } else {
  25. $this->setCell('A1', $str);
  26. $this->setCell('A2', $cnt);
  27. $sheet->getCell('B1')->setValue('=RIGHT(A1, A2)');
  28. }
  29. $result = $sheet->getCell('B1')->getCalculatedValue();
  30. self::assertEquals($expectedResult, $result);
  31. }
  32. public function providerRIGHT(): array
  33. {
  34. return require 'tests/data/Calculation/TextData/RIGHT.php';
  35. }
  36. /**
  37. * @dataProvider providerLocaleRIGHT
  38. *
  39. * @param string $expectedResult
  40. * @param mixed $value
  41. * @param mixed $locale
  42. * @param mixed $characters
  43. */
  44. public function testLowerWithLocaleBoolean($expectedResult, $locale, $value, $characters): void
  45. {
  46. $newLocale = Settings::setLocale($locale);
  47. if ($newLocale === false) {
  48. self::markTestSkipped('Unable to set locale for locale-specific test');
  49. }
  50. $sheet = $this->getSheet();
  51. $this->setCell('A1', $value);
  52. $this->setCell('A2', $characters);
  53. $sheet->getCell('B1')->setValue('=RIGHT(A1, A2)');
  54. $result = $sheet->getCell('B1')->getCalculatedValue();
  55. self::assertEquals($expectedResult, $result);
  56. }
  57. public function providerLocaleRIGHT(): array
  58. {
  59. return [
  60. ['RAI', 'fr_FR', true, 3],
  61. ['AAR', 'nl_NL', true, 3],
  62. ['OSI', 'fi', true, 3],
  63. ['ИНА', 'bg', true, 3],
  64. ['UX', 'fr_FR', false, 2],
  65. ['WAAR', 'nl_NL', false, 4],
  66. ['ÄTOSI', 'fi', false, 5],
  67. ['ЖЬ', 'bg', false, 2],
  68. ];
  69. }
  70. /**
  71. * @dataProvider providerCalculationTypeRIGHTTrue
  72. */
  73. public function testCalculationTypeTrue(string $type, string $resultB1, string $resultB2): void
  74. {
  75. Functions::setCompatibilityMode($type);
  76. $sheet = $this->getSheet();
  77. $this->setCell('A1', true);
  78. $this->setCell('A2', 'Hello');
  79. $this->setCell('B1', '=RIGHT(A1, 1)');
  80. $this->setCell('B2', '=RIGHT(A2, A1)');
  81. self::assertEquals($resultB1, $sheet->getCell('B1')->getCalculatedValue());
  82. self::assertEquals($resultB2, $sheet->getCell('B2')->getCalculatedValue());
  83. }
  84. public function providerCalculationTypeRIGHTTrue(): array
  85. {
  86. return [
  87. 'Excel RIGHT(true, 1) AND RIGHT("hello", true)' => [
  88. Functions::COMPATIBILITY_EXCEL,
  89. 'E',
  90. 'o',
  91. ],
  92. 'Gnumeric RIGHT(true, 1) AND RIGHT("hello", true)' => [
  93. Functions::COMPATIBILITY_GNUMERIC,
  94. 'E',
  95. 'o',
  96. ],
  97. 'OpenOffice RIGHT(true, 1) AND RIGHT("hello", true)' => [
  98. Functions::COMPATIBILITY_OPENOFFICE,
  99. '1',
  100. '#VALUE!',
  101. ],
  102. ];
  103. }
  104. /**
  105. * @dataProvider providerCalculationTypeRIGHTFalse
  106. */
  107. public function testCalculationTypeFalse(string $type, string $resultB1, string $resultB2): void
  108. {
  109. Functions::setCompatibilityMode($type);
  110. $sheet = $this->getSheet();
  111. $this->setCell('A1', false);
  112. $this->setCell('A2', 'Hello');
  113. $this->setCell('B1', '=RIGHT(A1, 1)');
  114. $this->setCell('B2', '=RIGHT(A2, A1)');
  115. self::assertEquals($resultB1, $sheet->getCell('B1')->getCalculatedValue());
  116. self::assertEquals($resultB2, $sheet->getCell('B2')->getCalculatedValue());
  117. }
  118. public function providerCalculationTypeRIGHTFalse(): array
  119. {
  120. return [
  121. 'Excel RIGHT(false, 1) AND RIGHT("hello", false)' => [
  122. Functions::COMPATIBILITY_EXCEL,
  123. 'E',
  124. '',
  125. ],
  126. 'Gnumeric RIGHT(false, 1) AND RIGHT("hello", false)' => [
  127. Functions::COMPATIBILITY_GNUMERIC,
  128. 'E',
  129. '',
  130. ],
  131. 'OpenOffice RIGHT(false, 1) AND RIGHT("hello", false)' => [
  132. Functions::COMPATIBILITY_OPENOFFICE,
  133. '0',
  134. '#VALUE!',
  135. ],
  136. ];
  137. }
  138. /**
  139. * @dataProvider providerCalculationTypeRIGHTNull
  140. */
  141. public function testCalculationTypeNull(string $type, string $resultB1, string $resultB2): void
  142. {
  143. Functions::setCompatibilityMode($type);
  144. $sheet = $this->getSheet();
  145. $this->setCell('A2', 'Hello');
  146. $this->setCell('B1', '=RIGHT(A1, 1)');
  147. $this->setCell('B2', '=RIGHT(A2, A1)');
  148. self::assertEquals($resultB1, $sheet->getCell('B1')->getCalculatedValue());
  149. self::assertEquals($resultB2, $sheet->getCell('B2')->getCalculatedValue());
  150. }
  151. public function providerCalculationTypeRIGHTNull(): array
  152. {
  153. return [
  154. 'Excel RIGHT(null, 1) AND RIGHT("hello", null)' => [
  155. Functions::COMPATIBILITY_EXCEL,
  156. '',
  157. '',
  158. ],
  159. 'Gnumeric RIGHT(null, 1) AND RIGHT("hello", null)' => [
  160. Functions::COMPATIBILITY_GNUMERIC,
  161. '',
  162. 'o',
  163. ],
  164. 'OpenOffice RIGHT(null, 1) AND RIGHT("hello", null)' => [
  165. Functions::COMPATIBILITY_OPENOFFICE,
  166. '',
  167. '',
  168. ],
  169. ];
  170. }
  171. /**
  172. * @dataProvider providerRightArray
  173. */
  174. public function testRightArray(array $expectedResult, string $argument1, string $argument2): void
  175. {
  176. $calculation = Calculation::getInstance();
  177. $formula = "=RIGHT({$argument1}, {$argument2})";
  178. $result = $calculation->_calculateFormulaValue($formula);
  179. self::assertEqualsWithDelta($expectedResult, $result, 1.0e-14);
  180. }
  181. public function providerRightArray(): array
  182. {
  183. return [
  184. 'row vector #1' => [[['llo', 'rld', 'eet']], '{"Hello", "World", "PhpSpreadsheet"}', '3'],
  185. 'column vector #1' => [[['llo'], ['rld'], ['eet']], '{"Hello"; "World"; "PhpSpreadsheet"}', '3'],
  186. 'matrix #1' => [[['llo', 'rld'], ['eet', 'cel']], '{"Hello", "World"; "PhpSpreadsheet", "Excel"}', '3'],
  187. 'column vector #2' => [[['eet'], ['sheet']], '"PhpSpreadsheet"', '{3; 5}'],
  188. ];
  189. }
  190. }