canRead($filename)); } public function providerCanReadVerySmallFile() { $padding = str_repeat('a', 2048); return [ [true, ' ' . $padding . ' '], [true, ' ' . $padding . ''], [true, ''], [false, ''], ]; } /** * @dataProvider providerCanReadVerySmallFile * * @param bool $expected * @param string $content */ public function testCanReadVerySmallFile($expected, $content) { $filename = $this->createHtml($content); $reader = new Html(); $actual = $reader->canRead($filename); self::assertSame($expected, $actual); unlink($filename); } public function testBackgroundColorInRanding() { $html = '
Blue background
'; $filename = $this->createHtml($html); $spreadsheet = $this->loadHtmlIntoSpreadsheet($filename); $firstSheet = $spreadsheet->getSheet(0); $style = $firstSheet->getCell('A1')->getStyle(); self::assertEquals('FFFFFF', $style->getFont()->getColor()->getRGB()); unlink($filename); } public function testCanApplyInlineBordersStyles() { $html = '
Thin border Border bottom Border top Border left Border right
'; $filename = $this->createHtml($html); $spreadsheet = $this->loadHtmlIntoSpreadsheet($filename); $firstSheet = $spreadsheet->getSheet(0); $style = $firstSheet->getCell('A1')->getStyle(); $borders = $style->getBorders(); /** @var Border $border */ foreach ([$borders->getTop(), $borders->getBottom(), $borders->getLeft(), $borders->getRight()] as $border) { self::assertEquals('333333', $border->getColor()->getRGB()); self::assertEquals(Border::BORDER_THIN, $border->getBorderStyle()); } $style = $firstSheet->getCell('B1')->getStyle(); $border = $style->getBorders()->getBottom(); self::assertEquals('333333', $border->getColor()->getRGB()); self::assertEquals(Border::BORDER_THIN, $border->getBorderStyle()); $style = $firstSheet->getCell('C1')->getStyle(); $border = $style->getBorders()->getTop(); self::assertEquals('333333', $border->getColor()->getRGB()); self::assertEquals(Border::BORDER_THIN, $border->getBorderStyle()); $style = $firstSheet->getCell('D1')->getStyle(); $border = $style->getBorders()->getLeft(); self::assertEquals('333333', $border->getColor()->getRGB()); self::assertEquals(Border::BORDER_THIN, $border->getBorderStyle()); $style = $firstSheet->getCell('E1')->getStyle(); $border = $style->getBorders()->getRight(); self::assertEquals('333333', $border->getColor()->getRGB()); self::assertEquals(Border::BORDER_THIN, $border->getBorderStyle()); unlink($filename); } public function testCanApplyInlineFontStyles() { $html = '
16px Times New Roman Bold Italic Underline Line through
'; $filename = $this->createHtml($html); $spreadsheet = $this->loadHtmlIntoSpreadsheet($filename); $firstSheet = $spreadsheet->getSheet(0); $style = $firstSheet->getCell('A1')->getStyle(); self::assertEquals(16, $style->getFont()->getSize()); $style = $firstSheet->getCell('B1')->getStyle(); self::assertEquals('Times New Roman', $style->getFont()->getName()); $style = $firstSheet->getCell('C1')->getStyle(); self::assertTrue($style->getFont()->getBold()); $style = $firstSheet->getCell('D1')->getStyle(); self::assertTrue($style->getFont()->getItalic()); $style = $firstSheet->getCell('E1')->getStyle(); self::assertEquals(Font::UNDERLINE_SINGLE, $style->getFont()->getUnderline()); $style = $firstSheet->getCell('F1')->getStyle(); self::assertTrue($style->getFont()->getStrikethrough()); unlink($filename); } public function testCanApplyInlineWidth() { $html = '
50px 100px
'; $filename = $this->createHtml($html); $spreadsheet = $this->loadHtmlIntoSpreadsheet($filename); $firstSheet = $spreadsheet->getSheet(0); $dimension = $firstSheet->getColumnDimension('A'); self::assertEquals(50, $dimension->getWidth()); $dimension = $firstSheet->getColumnDimension('B'); self::assertEquals(100, $dimension->getWidth()); unlink($filename); } public function testCanApplyInlineHeight() { $html = '
1
2
'; $filename = $this->createHtml($html); $spreadsheet = $this->loadHtmlIntoSpreadsheet($filename); $firstSheet = $spreadsheet->getSheet(0); $dimension = $firstSheet->getRowDimension(1); self::assertEquals(50, $dimension->getRowHeight()); $dimension = $firstSheet->getRowDimension(2); self::assertEquals(100, $dimension->getRowHeight()); unlink($filename); } public function testCanApplyAlignment() { $html = '
Center align Center valign Center align Center valign Text indent Wraptext
'; $filename = $this->createHtml($html); $spreadsheet = $this->loadHtmlIntoSpreadsheet($filename); $firstSheet = $spreadsheet->getSheet(0); $style = $firstSheet->getCell('A1')->getStyle(); self::assertEquals(Alignment::HORIZONTAL_CENTER, $style->getAlignment()->getHorizontal()); $style = $firstSheet->getCell('B1')->getStyle(); self::assertEquals(Alignment::VERTICAL_CENTER, $style->getAlignment()->getVertical()); $style = $firstSheet->getCell('C1')->getStyle(); self::assertEquals(Alignment::HORIZONTAL_CENTER, $style->getAlignment()->getHorizontal()); $style = $firstSheet->getCell('D1')->getStyle(); self::assertEquals(Alignment::VERTICAL_CENTER, $style->getAlignment()->getVertical()); $style = $firstSheet->getCell('E1')->getStyle(); self::assertEquals(10, $style->getAlignment()->getIndent()); $style = $firstSheet->getCell('F1')->getStyle(); self::assertTrue($style->getAlignment()->getWrapText()); unlink($filename); } public function testCanApplyInlineDataFormat() { $html = '
2019-02-02 12:34:00
'; $filename = $this->createHtml($html); $spreadsheet = $this->loadHtmlIntoSpreadsheet($filename); $firstSheet = $spreadsheet->getSheet(0); $style = $firstSheet->getCell('A1')->getStyle(); self::assertEquals('mmm-yy', $style->getNumberFormat()->getFormatCode()); unlink($filename); } public function testCanInsertImage() { $imagePath = realpath(__DIR__ . '/../../data/Reader/HTML/image.jpg'); $html = '
'; $filename = $this->createHtml($html); $spreadsheet = $this->loadHtmlIntoSpreadsheet($filename); $firstSheet = $spreadsheet->getSheet(0); /** @var Drawing $drawing */ $drawing = $firstSheet->getDrawingCollection()[0]; self::assertEquals($imagePath, $drawing->getPath()); self::assertEquals('A1', $drawing->getCoordinates()); unlink($filename); } public function testCanApplyCellWrapping() { $html = '
Hello World
Hello
World
Hello
World
'; $filename = $this->createHtml($html); $spreadsheet = $this->loadHtmlIntoSpreadsheet($filename); $firstSheet = $spreadsheet->getSheet(0); $cellStyle = $firstSheet->getStyle('A1'); self::assertFalse($cellStyle->getAlignment()->getWrapText()); $cellStyle = $firstSheet->getStyle('A2'); self::assertTrue($cellStyle->getAlignment()->getWrapText()); $cellValue = $firstSheet->getCell('A2')->getValue(); $this->assertStringContainsString("\n", $cellValue); $cellStyle = $firstSheet->getStyle('A3'); self::assertTrue($cellStyle->getAlignment()->getWrapText()); $cellValue = $firstSheet->getCell('A3')->getValue(); $this->assertStringContainsString("\n", $cellValue); unlink($filename); } public function testCanLoadFromString() { $html = '
Hello World
Hello
World
Hello
World
'; $spreadsheet = (new Html())->loadFromString($html); $firstSheet = $spreadsheet->getSheet(0); $cellStyle = $firstSheet->getStyle('A1'); self::assertFalse($cellStyle->getAlignment()->getWrapText()); $cellStyle = $firstSheet->getStyle('A2'); self::assertTrue($cellStyle->getAlignment()->getWrapText()); $cellValue = $firstSheet->getCell('A2')->getValue(); $this->assertStringContainsString("\n", $cellValue); $cellStyle = $firstSheet->getStyle('A3'); self::assertTrue($cellStyle->getAlignment()->getWrapText()); $cellValue = $firstSheet->getCell('A3')->getValue(); $this->assertStringContainsString("\n", $cellValue); } public function testCanLoadFromStringIntoExistingSpreadsheet() { $html = '
Hello World
Hello
World
Hello
World
'; $reader = new Html(); $spreadsheet = $reader->loadFromString($html); $firstSheet = $spreadsheet->getSheet(0); $cellStyle = $firstSheet->getStyle('A1'); self::assertFalse($cellStyle->getAlignment()->getWrapText()); $cellStyle = $firstSheet->getStyle('A2'); self::assertTrue($cellStyle->getAlignment()->getWrapText()); $cellValue = $firstSheet->getCell('A2')->getValue(); $this->assertStringContainsString("\n", $cellValue); $cellStyle = $firstSheet->getStyle('A3'); self::assertTrue($cellStyle->getAlignment()->getWrapText()); $cellValue = $firstSheet->getCell('A3')->getValue(); $this->assertStringContainsString("\n", $cellValue); $reader->setSheetIndex(1); $html = '
Goodbye World
'; self::assertEquals(1, $spreadsheet->getSheetCount()); $spreadsheet = $reader->loadFromString($html, $spreadsheet); self::assertEquals(2, $spreadsheet->getSheetCount()); } /** * @param string $html * * @return string */ private function createHtml($html) { $filename = tempnam(sys_get_temp_dir(), 'html'); file_put_contents($filename, $html); return $filename; } /** * @param $filename * * @return \PhpOffice\PhpSpreadsheet\Spreadsheet */ private function loadHtmlIntoSpreadsheet($filename) { return (new Html())->load($filename); } public function testRowspanInRendering() { $filename = './data/Reader/HTML/rowspan.html'; $reader = new Html(); $spreadsheet = $reader->load($filename); $actual = $spreadsheet->getActiveSheet()->getMergeCells(); self::assertSame(['A2:C2' => 'A2:C2'], $actual); } public function testTextIndentUseRowspan() { $html = '
1 Center Align Row
2 Text Indent
'; $filename = $this->createHtml($html); $spreadsheet = $this->loadHtmlIntoSpreadsheet($filename); $firstSheet = $spreadsheet->getSheet(0); $style = $firstSheet->getCell('C2')->getStyle(); self::assertEquals(10, $style->getAlignment()->getIndent()); unlink($filename); } }