TestWithSpreadsheet::readSpreadsheet()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 14
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ApplicationTest\Traits;
6
7
use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
8
use PhpOffice\PhpSpreadsheet\Spreadsheet;
9
use ZipArchive;
10
11
trait TestWithSpreadsheet
12
{
13
    abstract public static function assertTrue($condition, string $message = ''): void;
14
15
    private function readSpreadsheet(string $filename): Spreadsheet
16
    {
17
        // Assert that it is a valid ZIP file to prevent PhpSpreadsheet from hanging
18
        $zip = new ZipArchive();
19
        $res = $zip->open($filename, ZipArchive::CHECKCONS);
20
        self::assertTrue($res, 'exported Excel should be a valid ZIP file');
21
        $zip->close();
22
23
        // Re-read it
24
        $reader = new Xlsx();
25
        $spreadsheet = $reader->load($filename);
26
        unlink($filename);
27
28
        return $spreadsheet;
29
    }
30
}
31