Failed Conditions
Pull Request — master (#3962)
by Owen
11:35
created

ConcatenateTest::testCONCATENATE()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 24
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
c 1
b 0
f 0
dl 0
loc 24
rs 8.9777
cc 6
nc 6
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\TextData;
6
7
use PhpOffice\PhpSpreadsheet\Cell\DataType;
8
use PhpOffice\PhpSpreadsheet\Spreadsheet;
9
10
class ConcatenateTest extends AllSetupTeardown
11
{
12
    /**
13
     * @dataProvider providerCONCATENATE
14
     */
15
    public function testCONCATENATE(mixed $expectedResult, mixed ...$args): void
16
    {
17
        $this->mightHaveException($expectedResult);
18
        $sheet = $this->getSheet();
19
        $finalArg = '';
20
        $comma = '';
21
        foreach ($args as $arg) {
22
            $finalArg .= $comma;
23
            $comma = ',';
24
            if (is_bool($arg)) {
25
                $finalArg .= $arg ? 'true' : 'false';
26
            } elseif ($arg === 'A2') {
27
                $finalArg .= 'A2';
28
                $sheet->getCell('A2')->setValue('=2/0');
29
            } elseif ($arg === 'A3') {
30
                $finalArg .= 'A3';
31
                $sheet->getCell('A3')->setValue(str_repeat('Ԁ', DataType::MAX_STRING_LENGTH - 5));
32
            } else {
33
                $finalArg .= '"' . (string) $arg . '"';
34
            }
35
        }
36
        $this->setCell('B1', "=CONCATENATE($finalArg)");
37
        $result = $sheet->getCell('B1')->getCalculatedValue();
38
        self::assertEquals($expectedResult, $result);
39
    }
40
41
    public static function providerCONCATENATE(): array
42
    {
43
        return require 'tests/data/Calculation/TextData/CONCATENATE.php';
44
    }
45
46
    public function testConcatenateWithIndexMatch(): void
47
    {
48
        $spreadsheet = new Spreadsheet();
49
        $sheet1 = $spreadsheet->getActiveSheet();
50
        $sheet1->setTitle('Formula');
51
        $sheet1->fromArray(
52
            [
53
                ['Number', 'Formula'],
54
                [52101293, '=CONCATENATE(INDEX(Lookup!B2, MATCH(A2, Lookup!A2, 0)))'],
55
            ]
56
        );
57
        $sheet2 = $spreadsheet->createSheet();
58
        $sheet2->setTitle('Lookup');
59
        $sheet2->fromArray(
60
            [
61
                ['Lookup', 'Match'],
62
                [52101293, 'PHP'],
63
            ]
64
        );
65
        self::assertSame('PHP', $sheet1->getCell('B2')->getCalculatedValue());
66
        $spreadsheet->disconnectWorksheets();
67
    }
68
}
69