Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 26
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
ExcelExportTest
0.00% covered (danger)
0.00%
0 / 26
0.00% covered (danger)
0.00%
0 / 1
6
0.00% covered (danger)
0.00%
0 / 1
 it_can_create_an_excel_file
0.00% covered (danger)
0.00%
0 / 26
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2namespace Apie\Tests\Export;
3
4use Apie\Export\ExcelExport;
5use PHPUnit\Framework\Attributes\Test;
6use PHPUnit\Framework\TestCase;
7use ZipArchive;
8
9class ExcelExportTest extends TestCase
10{
11    #[Test]
12    public function it_can_create_an_excel_file(): void
13    {
14        $testItem = new ExcelExport();
15        $stream = $testItem->streamFromSheets([
16            'First Sheet' => (function () {
17                yield ['Name', 'Age'];
18                yield ['Alice', 30];
19                yield ['Bob', 25];
20            })(),
21            'Second Sheet' => (function () {
22                yield ['Product', 'Price'];
23                yield ['Book', 12.99];
24                yield ['Pen', 1.99];
25            })(),
26        ], 'test_export.xlsx');
27        $this->assertTrue($stream->isReadable());
28        // $this->assertFalse($stream->isWritable());
29        $contents = $stream->getContents();
30        $this->assertStringStartsWith('PK', $contents); // ZIP files start with
31        if (class_exists(ZipArchive::class)) {
32            $zip = new ZipArchive();
33            $tempFile = tempnam(sys_get_temp_dir(), 'xlsx');
34            file_put_contents($tempFile, $contents);
35            try {
36                $res = $zip->open($tempFile);
37                $this->assertTrue($res === true, 'Failed to open generated XLSX as ZIP archive');
38                $this->assertNotFalse($zip->locateName('xl/worksheets/sheet1.xml'), 'Sheet1.xml not found in XLSX');
39                $this->assertNotFalse($zip->locateName('xl/worksheets/sheet2.xml'), 'Sheet2.xml not found in XLSX');
40                $zip->close();
41            } finally {
42                @unlink($tempFile);
43            }
44        }
45    }
46}