ConcatenateTest.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\TextData;
  3. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  4. class ConcatenateTest extends AllSetupTeardown
  5. {
  6. /**
  7. * @dataProvider providerCONCATENATE
  8. *
  9. * @param mixed $expectedResult
  10. * @param array $args
  11. */
  12. public function testCONCATENATE($expectedResult, ...$args): void
  13. {
  14. $this->mightHaveException($expectedResult);
  15. $sheet = $this->getSheet();
  16. $finalArg = '';
  17. $row = 0;
  18. foreach ($args as $arg) {
  19. ++$row;
  20. $this->setCell("A$row", $arg);
  21. $finalArg = "A1:A$row";
  22. }
  23. $this->setCell('B1', "=CONCAT($finalArg)");
  24. $result = $sheet->getCell('B1')->getCalculatedValue();
  25. self::assertEquals($expectedResult, $result);
  26. }
  27. public function providerCONCATENATE(): array
  28. {
  29. return require 'tests/data/Calculation/TextData/CONCATENATE.php';
  30. }
  31. public function testConcatenateWithIndexMatch(): void
  32. {
  33. $spreadsheet = new Spreadsheet();
  34. $sheet1 = $spreadsheet->getActiveSheet();
  35. $sheet1->setTitle('Formula');
  36. $sheet1->fromArray(
  37. [
  38. ['Number', 'Formula'],
  39. [52101293, '=CONCAT(INDEX(Lookup!B2, MATCH(A2, Lookup!A2, 0)))'],
  40. ]
  41. );
  42. $sheet2 = $spreadsheet->createSheet();
  43. $sheet2->setTitle('Lookup');
  44. $sheet2->fromArray(
  45. [
  46. ['Lookup', 'Match'],
  47. [52101293, 'PHP'],
  48. ]
  49. );
  50. self::assertSame('PHP', $sheet1->getCell('B2')->getCalculatedValue());
  51. $spreadsheet->disconnectWorksheets();
  52. }
  53. }