TestWithSpreadsheet   A
last analyzed

Complexity

Total Complexity 1

Size/Duplication

Total Lines 18
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 1
eloc 9
dl 0
loc 18
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A readSpreadsheet() 0 14 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