/** * Returns the content of the "<fonts>" section. * * @return string */ protected function getFontsSectionContent() { $content = '<fonts count="' . count($this->styleIdToStyleMappingTable) . '">'; /** @var \Box\Spout\Writer\Style\Style $style */ foreach ($this->getRegisteredStyles() as $style) { $content .= '<font>'; $content .= '<sz val="' . $style->getFontSize() . '"/>'; $content .= '<color rgb="' . Color::toARGB($style->getFontColor()) . '"/>'; $content .= '<name val="' . $style->getFontName() . '"/>'; if ($style->isFontBold()) { $content .= '<b/>'; } if ($style->isFontItalic()) { $content .= '<i/>'; } if ($style->isFontUnderline()) { $content .= '<u/>'; } if ($style->isFontStrikethrough()) { $content .= '<strike/>'; } $content .= '</font>'; } $content .= '</fonts>'; return $content; }
/** * @return void */ public function testAddRowWithStyleShouldListAllUsedFontsInCreatedStylesXmlFile() { $fileName = 'test_add_row_with_style_should_list_all_used_fonts.xlsx'; $dataRows = [['xlsx--11', 'xlsx--12'], ['xlsx--21', 'xlsx--22']]; $style = (new StyleBuilder())->setFontBold()->setFontItalic()->setFontUnderline()->setFontStrikethrough()->build(); $style2 = (new StyleBuilder())->setFontSize(15)->setFontColor(Color::RED)->setFontName('Cambria')->build(); $this->writeToXLSXFileWithMultipleStyles($dataRows, $fileName, [$style, $style2]); $fontsDomElement = $this->getXmlSectionFromStylesXmlFile($fileName, 'fonts'); $this->assertEquals(3, $fontsDomElement->getAttribute('count'), 'There should be 3 fonts, including the default one.'); $fontElements = $fontsDomElement->getElementsByTagName('font'); $this->assertEquals(3, $fontElements->length, 'There should be 3 associated "font" elements, including the default one.'); // First font should be the default one $defaultFontElement = $fontElements->item(0); $this->assertChildrenNumEquals(3, $defaultFontElement, 'The default font should only have 3 properties.'); $this->assertFirstChildHasAttributeEquals((string) Writer::DEFAULT_FONT_SIZE, $defaultFontElement, 'sz', 'val'); $this->assertFirstChildHasAttributeEquals(Color::toARGB(Style::DEFAULT_FONT_COLOR), $defaultFontElement, 'color', 'rgb'); $this->assertFirstChildHasAttributeEquals(Writer::DEFAULT_FONT_NAME, $defaultFontElement, 'name', 'val'); // Second font should contain data from the first created style $secondFontElement = $fontElements->item(1); $this->assertChildrenNumEquals(7, $secondFontElement, 'The font should only have 7 properties (4 custom styles + 3 default styles).'); $this->assertChildExists($secondFontElement, 'b'); $this->assertChildExists($secondFontElement, 'i'); $this->assertChildExists($secondFontElement, 'u'); $this->assertChildExists($secondFontElement, 'strike'); $this->assertFirstChildHasAttributeEquals((string) Writer::DEFAULT_FONT_SIZE, $secondFontElement, 'sz', 'val'); $this->assertFirstChildHasAttributeEquals(Color::toARGB(Style::DEFAULT_FONT_COLOR), $secondFontElement, 'color', 'rgb'); $this->assertFirstChildHasAttributeEquals(Writer::DEFAULT_FONT_NAME, $secondFontElement, 'name', 'val'); // Third font should contain data from the second created style $thirdFontElement = $fontElements->item(2); $this->assertChildrenNumEquals(3, $thirdFontElement, 'The font should only have 3 properties.'); $this->assertFirstChildHasAttributeEquals('15', $thirdFontElement, 'sz', 'val'); $this->assertFirstChildHasAttributeEquals(Color::toARGB(Color::RED), $thirdFontElement, 'color', 'rgb'); $this->assertFirstChildHasAttributeEquals('Cambria', $thirdFontElement, 'name', 'val'); }