Passed
Pull Request — master (#4340)
by Owen
14:48
created

Issue4269Test::testDefault()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpOffice\PhpSpreadsheetTests\Writer\Xlsx;
6
7
use PhpOffice\PhpSpreadsheet\Shared\File;
8
use PhpOffice\PhpSpreadsheet\Spreadsheet;
9
use PhpOffice\PhpSpreadsheet\Writer\Xlsx as XlsxWriter;
10
use PHPUnit\Framework\Attributes\DataProvider;
11
use PHPUnit\Framework\TestCase;
12
13
class Issue4269Test extends TestCase
14
{
15
    private string $outputFile = '';
16
17
    private static bool $alwaysFalse = false;
18
19
    protected function tearDown(): void
20
    {
21
        if ($this->outputFile !== '') {
22
            unlink($this->outputFile);
23
            $this->outputFile = '';
24
        }
25
    }
26
27
    #[DataProvider('validationProvider')]
28
    public function testWriteArrayFormulaTextJoin(
29
        bool $preCalculateFormulas,
30
        ?bool $forceFullCalc,
31
        string $expected
32
    ): void {
33
        $spreadsheet = new Spreadsheet();
34
        $sheet = $spreadsheet->getActiveSheet();
35
        $sheet->setCellValue('A1', '=A2*2');
36
        $sheet->setCellValue('A2', 0);
37
38
        $writer = new XlsxWriter($spreadsheet);
39
        $writer->setPreCalculateFormulas($preCalculateFormulas);
40
        $writer->setForceFullCalc($forceFullCalc);
41
        $this->outputFile = File::temporaryFilename();
42
        $writer->save($this->outputFile);
43
        $spreadsheet->disconnectWorksheets();
44
45
        $file = 'zip://';
46
        $file .= $this->outputFile;
47
        $file .= '#xl/workbook.xml';
48
        $data = file_get_contents($file);
49
        if ($data === false) {
50
            self::fail('Unable to read file');
51
        } else {
52
            self::assertStringContainsString($expected, $data);
53
        }
54
    }
55
56
    public static function validationProvider(): array
57
    {
58
        return [
59
            'normal case' => [true, null, 'calcMode="auto" calcCompleted="1" fullCalcOnLoad="0" forceFullCalc="0"'],
60
            'issue 456' => [false, null, 'calcMode="auto" calcCompleted="0" fullCalcOnLoad="1" forceFullCalc="1"'],
61
            'better choice for no precalc' => [false, false, 'calcMode="auto" calcCompleted="0" fullCalcOnLoad="1" forceFullCalc="0"'],
62
            'unlikely use case' => [true, true, 'calcMode="auto" calcCompleted="1" fullCalcOnLoad="0" forceFullCalc="1"'],
63
        ];
64
    }
65
66
    public function testDefault(): void
67
    {
68
        self::assertSame(self::$alwaysFalse, XlsxWriter::DEFAULT_FORCE_FULL_CALC);
69
    }
70
}
71