Passed
Push — master ( c1968e...6edc55 )
by Adrien
15:06
created

Issue642Test::testCharOutsideBMP()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
c 1
b 0
f 0
dl 0
loc 19
rs 9.7
cc 1
nc 1
nop 0
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheetTests\Writer\Xls;
4
5
use PhpOffice\PhpSpreadsheet\Shared\File;
6
use PhpOffice\PhpSpreadsheet\Spreadsheet;
7
use PhpOffice\PhpSpreadsheet\Writer\Xls;
8
use PHPUnit\Framework\TestCase;
9
10
class Issue642Test extends TestCase
11
{
12
    public function testCharOutsideBMP(): void
13
    {
14
        $spreadsheet = new Spreadsheet();
15
        $sheet = $spreadsheet->getActiveSheet();
16
        $stringUtf8 = "Hello\u{1f600}goodbye";
17
        self::assertSame(13, mb_strlen($stringUtf8));
18
        $stringUtf16 = (string) iconv('UTF-8', 'UTF-16LE', $stringUtf8);
19
        self::assertSame(28, strlen($stringUtf16)); // each character requires 2 bytes except for non-BMP which requires 4
20
        $sheet->getCell('A1')->setValue($stringUtf8);
21
        $outputFilename = File::temporaryFilename();
22
        $writer = new Xls($spreadsheet);
23
        $writer->save($outputFilename);
24
        $spreadsheet->disconnectWorksheets();
25
        $contents = (string) file_get_contents($outputFilename);
26
        unlink($outputFilename);
27
        $expected = "\x00\x0e\x00\x01" . $stringUtf16; // length is 14 (0e), not 13
28
        self::assertStringContainsString($expected, $contents);
29
        $unexpected = "\x00\x0d\x00\x01" . $stringUtf16; // length is 14 (0e), not 13
30
        self::assertStringNotContainsString($unexpected, $contents);
31
    }
32
}
33