MidTest.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 MidTest extends AllSetupTeardown
  7. {
  8. /**
  9. * @dataProvider providerMID
  10. *
  11. * @param mixed $expectedResult
  12. * @param mixed $str string from which to extract
  13. * @param mixed $start position at which to start
  14. * @param mixed $cnt number of characters to extract
  15. */
  16. public function testMID($expectedResult, $str = 'omitted', $start = 'omitted', $cnt = 'omitted'): void
  17. {
  18. $this->mightHaveException($expectedResult);
  19. $sheet = $this->getSheet();
  20. if ($str === 'omitted') {
  21. $sheet->getCell('B1')->setValue('=MID()');
  22. } elseif ($start === 'omitted') {
  23. $this->setCell('A1', $str);
  24. $sheet->getCell('B1')->setValue('=MID(A1)');
  25. } elseif ($cnt === 'omitted') {
  26. $this->setCell('A1', $str);
  27. $this->setCell('A2', $start);
  28. $sheet->getCell('B1')->setValue('=MID(A1, A2)');
  29. } else {
  30. $this->setCell('A1', $str);
  31. $this->setCell('A2', $start);
  32. $this->setCell('A3', $cnt);
  33. $sheet->getCell('B1')->setValue('=MID(A1, A2, A3)');
  34. }
  35. $result = $sheet->getCell('B1')->getCalculatedValue();
  36. self::assertEquals($expectedResult, $result);
  37. }
  38. public function providerMID(): array
  39. {
  40. return require 'tests/data/Calculation/TextData/MID.php';
  41. }
  42. /**
  43. * @dataProvider providerLocaleMID
  44. *
  45. * @param string $expectedResult
  46. * @param mixed $value
  47. * @param mixed $locale
  48. * @param mixed $offset
  49. * @param mixed $characters
  50. */
  51. public function testMiddleWithLocaleBoolean($expectedResult, $locale, $value, $offset, $characters): void
  52. {
  53. $newLocale = Settings::setLocale($locale);
  54. if ($newLocale === false) {
  55. self::markTestSkipped('Unable to set locale for locale-specific test');
  56. }
  57. $sheet = $this->getSheet();
  58. $this->setCell('A1', $value);
  59. $this->setCell('A2', $offset);
  60. $this->setCell('A3', $characters);
  61. $sheet->getCell('B1')->setValue('=MID(A1, A2, A3)');
  62. $result = $sheet->getCell('B1')->getCalculatedValue();
  63. self::assertEquals($expectedResult, $result);
  64. }
  65. public function providerLocaleMID(): array
  66. {
  67. return [
  68. ['RA', 'fr_FR', true, 2, 2],
  69. ['AA', 'nl_NL', true, 2, 2],
  70. ['OS', 'fi', true, 2, 2],
  71. ['СТИН', 'bg', true, 2, 4],
  72. ['AU', 'fr_FR', false, 2, 2],
  73. ['NWA', 'nl_NL', false, 2, 3],
  74. ['PÄTO', 'fi', false, 2, 4],
  75. ['ОЖ', 'bg', false, 2, 2],
  76. ];
  77. }
  78. /**
  79. * @dataProvider providerCalculationTypeMIDTrue
  80. */
  81. public function testCalculationTypeTrue(string $type, string $resultB1, string $resultB2, string $resultB3): void
  82. {
  83. Functions::setCompatibilityMode($type);
  84. $sheet = $this->getSheet();
  85. $this->setCell('A1', true);
  86. $this->setCell('A2', 'hello');
  87. $this->setCell('B1', '=MID(A1, 3, 1)');
  88. $this->setCell('B2', '=MID(A2, A1, 1)');
  89. $this->setCell('B3', '=MID(A2, 2, A1)');
  90. self::assertEquals($resultB1, $sheet->getCell('B1')->getCalculatedValue());
  91. self::assertEquals($resultB2, $sheet->getCell('B2')->getCalculatedValue());
  92. self::assertEquals($resultB3, $sheet->getCell('B3')->getCalculatedValue());
  93. }
  94. public function providerCalculationTypeMIDTrue(): array
  95. {
  96. return [
  97. 'Excel MID(true,3,1), MID("hello",true, 1), MID("hello", 2, true)' => [
  98. Functions::COMPATIBILITY_EXCEL,
  99. 'U',
  100. 'h',
  101. 'e',
  102. ],
  103. 'Gnumeric MID(true,3,1), MID("hello",true, 1), MID("hello", 2, true)' => [
  104. Functions::COMPATIBILITY_GNUMERIC,
  105. 'U',
  106. 'h',
  107. 'e',
  108. ],
  109. 'OpenOffice MID(true,3,1), MID("hello",true, 1), MID("hello", 2, true)' => [
  110. Functions::COMPATIBILITY_OPENOFFICE,
  111. '',
  112. '#VALUE!',
  113. '#VALUE!',
  114. ],
  115. ];
  116. }
  117. /**
  118. * @dataProvider providerCalculationTypeMIDFalse
  119. */
  120. public function testCalculationTypeFalse(string $type, string $resultB1, string $resultB2): void
  121. {
  122. Functions::setCompatibilityMode($type);
  123. $sheet = $this->getSheet();
  124. $this->setCell('A1', false);
  125. $this->setCell('A2', 'Hello');
  126. $this->setCell('B1', '=MID(A1, 3, 1)');
  127. $this->setCell('B2', '=MID(A2, A1, 1)');
  128. $this->setCell('B3', '=MID(A2, 2, A1)');
  129. self::assertEquals($resultB1, $sheet->getCell('B1')->getCalculatedValue());
  130. self::assertEquals($resultB2, $sheet->getCell('B2')->getCalculatedValue());
  131. }
  132. public function providerCalculationTypeMIDFalse(): array
  133. {
  134. return [
  135. 'Excel MID(false,3,1), MID("hello", false, 1), MID("hello", 2, false)' => [
  136. Functions::COMPATIBILITY_EXCEL,
  137. 'L',
  138. '#VALUE!',
  139. '',
  140. ],
  141. 'Gnumeric MID(false,3,1), MID("hello", false, 1), MID("hello", 2, false)' => [
  142. Functions::COMPATIBILITY_GNUMERIC,
  143. 'L',
  144. '#VALUE!',
  145. '',
  146. ],
  147. 'OpenOffice MID(false,3,1), MID("hello", false, 1), MID("hello", 2, false)' => [
  148. Functions::COMPATIBILITY_OPENOFFICE,
  149. '',
  150. '#VALUE!',
  151. '#VALUE!',
  152. ],
  153. ];
  154. }
  155. /**
  156. * @dataProvider providerCalculationTypeMIDNull
  157. */
  158. public function testCalculationTypeNull(string $type, string $resultB1, string $resultB2, string $resultB3): void
  159. {
  160. Functions::setCompatibilityMode($type);
  161. $sheet = $this->getSheet();
  162. $this->setCell('A2', 'Hello');
  163. $this->setCell('B1', '=MID(A1, 3, 1)');
  164. $this->setCell('B2', '=MID(A2, A1, 1)');
  165. $this->setCell('B3', '=MID(A2, 2, A1)');
  166. self::assertEquals($resultB1, $sheet->getCell('B1')->getCalculatedValue());
  167. self::assertEquals($resultB2, $sheet->getCell('B2')->getCalculatedValue());
  168. self::assertEquals($resultB3, $sheet->getCell('B3')->getCalculatedValue());
  169. }
  170. public function providerCalculationTypeMIDNull(): array
  171. {
  172. return [
  173. 'Excel MID(null,3,1), MID("hello", null, 1), MID("hello", 2, null)' => [
  174. Functions::COMPATIBILITY_EXCEL,
  175. '',
  176. '#VALUE!',
  177. '',
  178. ],
  179. 'Gnumeric MID(null,3,1), MID("hello", null, 1), MID("hello", 2, null)' => [
  180. Functions::COMPATIBILITY_GNUMERIC,
  181. '',
  182. '#VALUE!',
  183. '',
  184. ],
  185. 'OpenOffice MID(null,3,1), MID("hello", null, 1), MID("hello", 2, null)' => [
  186. Functions::COMPATIBILITY_OPENOFFICE,
  187. '',
  188. '#VALUE!',
  189. '',
  190. ],
  191. ];
  192. }
  193. /**
  194. * @dataProvider providerMidArray
  195. */
  196. public function testMidArray(array $expectedResult, string $argument1, string $argument2, string $argument3): void
  197. {
  198. $calculation = Calculation::getInstance();
  199. $formula = "=MID({$argument1}, {$argument2}, {$argument3})";
  200. $result = $calculation->_calculateFormulaValue($formula);
  201. self::assertEqualsWithDelta($expectedResult, $result, 1.0e-14);
  202. }
  203. public function providerMidArray(): array
  204. {
  205. return [
  206. 'row vector #1' => [[['lo Wor', 'Spread']], '{"Hello World", "PhpSpreadsheet"}', '4', '6'],
  207. 'column vector #1' => [[[' Wor'], ['read']], '{"Hello World"; "PhpSpreadsheet"}', '6', '4'],
  208. ];
  209. }
  210. }