Passed
Pull Request — master (#4268)
by Owen
13:38
created

StreamTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 29
dl 0
loc 42
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A providerFormats() 0 14 1
A testAllWritersCanWriteToStream() 0 24 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpOffice\PhpSpreadsheetTests\Functional;
6
7
use PhpOffice\PhpSpreadsheet\IOFactory;
8
use PhpOffice\PhpSpreadsheet\Spreadsheet;
9
use PHPUnit\Framework\TestCase;
10
11
class StreamTest extends TestCase
12
{
13
    public static function providerFormats(): array
14
    {
15
        $providerFormats = [
16
            ['Xls'],
17
            ['Xlsx'],
18
            ['Ods'],
19
            ['Csv'],
20
            ['Html'],
21
            ['Mpdf'],
22
            ['Dompdf'],
23
            ['Tcpdf'],
24
        ];
25
26
        return $providerFormats;
27
    }
28
29
    #[\PHPUnit\Framework\Attributes\DataProvider('providerFormats')]
30
    public function testAllWritersCanWriteToStream(string $format): void
31
    {
32
        $spreadsheet = new Spreadsheet();
33
        $spreadsheet->getActiveSheet()->setCellValue('A1', 'foo');
34
        $writer = IOFactory::createWriter($spreadsheet, $format);
35
36
        $stream = fopen('php://memory', 'wb+');
37
        $stat = ($stream === false) ? false : fstat($stream);
38
        if ($stream === false || $stat === false) {
39
            self::fail('fopen or fstat failed');
40
        } else {
41
            self::assertSame(0, $stat['size']);
42
43
            $writer->save($stream);
44
45
            self::assertIsResource($stream, 'should not close the stream for further usage out of PhpSpreadsheet');
46
            $stat = fstat($stream);
47
            if ($stat === false) {
48
                self::fail('fstat failed');
49
            } else {
50
                self::assertGreaterThan(0, $stat['size'], 'something should have been written to the stream');
51
            }
52
            self::assertGreaterThan(0, ftell($stream), 'should not be rewinded, because not all streams support it');
53
        }
54
    }
55
}
56