1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Writer\Html; |
6
|
|
|
|
7
|
|
|
use PhpOffice\PhpSpreadsheet\RichText\RichText; |
8
|
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet; |
9
|
|
|
use PhpOffice\PhpSpreadsheet\Writer\Html as HtmlWriter; |
10
|
|
|
use PHPUnit\Framework\TestCase; |
11
|
|
|
|
12
|
|
|
class RichTextTest extends TestCase |
13
|
|
|
{ |
14
|
|
|
public function testRichText(): void |
15
|
|
|
{ |
16
|
|
|
$spreadsheet = new Spreadsheet(); |
17
|
|
|
$sheet = $spreadsheet->getActiveSheet(); |
18
|
|
|
$rtf = new RichText(); |
19
|
|
|
$rtf->createText('~Cell Style~'); |
20
|
|
|
$rtf->createTextRun('~RTF Style~')->getFont()?->setItalic(true); |
21
|
|
|
$rtf->createText('~No Style~'); |
22
|
|
|
$sheet->getCell('A1')->setValue($rtf); |
23
|
|
|
$sheet->getStyle('A1')->getFont()->setBold(true); |
24
|
|
|
|
25
|
|
|
$fontStyle = $sheet->getStyle('A1')->getFont(); |
26
|
|
|
self::assertTrue($fontStyle->getBold()); |
27
|
|
|
self::assertFalse($fontStyle->getItalic()); |
28
|
|
|
|
29
|
|
|
$a1Value = $sheet->getCell('A1')->getValue(); |
30
|
|
|
self::assertInstanceOf(RichText::class, $a1Value); |
31
|
|
|
$elements = $a1Value->getRichTextElements(); |
32
|
|
|
self::assertCount(3, $elements); |
33
|
|
|
self::assertNull($elements[0]->getFont()); |
34
|
|
|
$fontStyle = $elements[1]->getFont(); |
35
|
|
|
self::assertNotNull($fontStyle); |
36
|
|
|
self::assertFalse($fontStyle->getBold()); |
37
|
|
|
self::assertTrue($fontStyle->getItalic()); |
38
|
|
|
self::assertNull($elements[0]->getFont()); |
39
|
|
|
|
40
|
|
|
$writer = new HtmlWriter($spreadsheet); |
41
|
|
|
$html = $writer->generateHtmlAll(); |
42
|
|
|
self::assertStringContainsString('td.style1, th.style1 { vertical-align:bottom; border-bottom:none #000000; border-top:none #000000; border-left:none #000000; border-right:none #000000; font-weight:bold; color:#000000; font-family:\'Calibri\'; font-size:11pt }', $html, 'cell style'); |
43
|
|
|
|
44
|
|
|
self::assertStringContainsString('<td class="column0 style1 inlineStr"><span style="font-weight:bold; text-decoration:normal; font-style:normal; color:#000000; font-family:\'Calibri\'; font-size:11pt">~Cell Style~</span>', $html, 'cell style and first text element'); |
45
|
|
|
|
46
|
|
|
self::assertStringContainsString('<span style="font-weight:normal; text-decoration:normal; font-style:italic; color:#000000; font-family:\'Calibri\'; font-size:11pt">~RTF Style~</span>', $html, 'second text element'); |
47
|
|
|
|
48
|
|
|
self::assertStringContainsString('<span style="font-weight:bold; text-decoration:normal; font-style:normal; color:#000000; font-family:\'Calibri\'; font-size:11pt">~No Style~</span></td>', $html, 'third text element'); |
49
|
|
|
|
50
|
|
|
$spreadsheet->disconnectWorksheets(); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|