|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\LookupRef; |
| 6 | + |
| 7 | +use PhpOffice\PhpSpreadsheet\Shared\StringHelper; |
| 8 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 9 | + |
| 10 | +class TocolTest extends AllSetupTeardown |
| 11 | +{ |
| 12 | + #[DataProvider('providerTocol')] |
| 13 | + public function testTorow(mixed $expectedResult, mixed $ignore = 'omitted', mixed $byColumn = 'omitted'): void |
| 14 | + { |
| 15 | + $this->mightHaveException($expectedResult); |
| 16 | + $sheet = $this->getSheet(); |
| 17 | + $this->setArrayAsArray(); |
| 18 | + if (is_string($ignore) && $ignore !== 'omitted') { |
| 19 | + $ignore = '"' . $ignore . '"'; |
| 20 | + } |
| 21 | + if (is_string($byColumn) && $byColumn !== 'omitted') { |
| 22 | + $byColumn = '"' . $byColumn . '"'; |
| 23 | + } |
| 24 | + $ignore = StringHelper::convertToString($ignore); |
| 25 | + $byColumn = StringHelper::convertToString($byColumn, convertBool: true); |
| 26 | + if ($ignore === 'omitted') { |
| 27 | + $formula = '=TOCOL(A1:D3)'; |
| 28 | + } elseif ($byColumn === 'omitted') { |
| 29 | + $formula = "=TOCOL(A1:D3,$ignore)"; |
| 30 | + } else { |
| 31 | + $formula = "=TOCOL(A1:D3,$ignore,$byColumn)"; |
| 32 | + } |
| 33 | + |
| 34 | + $data = [ |
| 35 | + ['a-one', 'b-one', 'c-one', 'd-one'], |
| 36 | + [null, 'b-two', 'c-two', '=2/0'], |
| 37 | + [' ', 'b-three', 'c-three', 'd-three'], |
| 38 | + ]; |
| 39 | + $sheet->fromArray($data, null, 'A1', true); |
| 40 | + $sheet->setCellValue('A5', $formula); |
| 41 | + $result = $sheet->getCell('A5')->getCalculatedValue(); |
| 42 | + self::assertSame($expectedResult, $result); |
| 43 | + } |
| 44 | + |
| 45 | + public static function providerTocol(): array |
| 46 | + { |
| 47 | + return [ |
| 48 | + 'defaults' => [[['a-one'], ['b-one'], ['c-one'], ['d-one'], [0], ['b-two'], ['c-two'], ['#DIV/0!'], [' '], ['b-three'], ['c-three'], ['d-three']]], |
| 49 | + 'ignore=0' => [[['a-one'], ['b-one'], ['c-one'], ['d-one'], [0], ['b-two'], ['c-two'], ['#DIV/0!'], [' '], ['b-three'], ['c-three'], ['d-three']], 0], |
| 50 | + 'ignore=1 supplied as 1.1' => [[['a-one'], ['b-one'], ['c-one'], ['d-one'], ['b-two'], ['c-two'], ['#DIV/0!'], [' '], ['b-three'], ['c-three'], ['d-three']], 1.1], |
| 51 | + 'ignore=2' => [[['a-one'], ['b-one'], ['c-one'], ['d-one'], [0], ['b-two'], ['c-two'], [' '], ['b-three'], ['c-three'], ['d-three']], 2], |
| 52 | + 'ignore=3' => [[['a-one'], ['b-one'], ['c-one'], ['d-one'], ['b-two'], ['c-two'], [' '], ['b-three'], ['c-three'], ['d-three']], 3], |
| 53 | + 'ignore=4 invalid' => ['#VALUE!', 4], |
| 54 | + 'ignore=string invalid' => ['#VALUE!', 'x'], |
| 55 | + 'by column' => [[['a-one'], [0], [' '], ['b-one'], ['b-two'], ['b-three'], ['c-one'], ['c-two'], ['c-three'], ['d-one'], ['#DIV/0!'], ['d-three']], 0, true], |
| 56 | + 'by column using float rather than bool, ignore=2' => [[['a-one'], [0], [' '], ['b-one'], ['b-two'], ['b-three'], ['c-one'], ['c-two'], ['c-three'], ['d-one'], ['d-three']], 2, 29.7], |
| 57 | + ]; |
| 58 | + } |
| 59 | +} |
0 commit comments