Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ v0.16.0 (xx dec 2018)
----------------------
### Added
- Add setting Chart Title and Legend visibility @Tom-Magill #1433
- Add ability to pass a Style object in Section constructor @ndench #1416
- Add support for hidden text @Alexmg86 #1527

### Fixed
Expand Down
8 changes: 5 additions & 3 deletions src/PhpWord/Element/Section.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,16 @@ class Section extends AbstractContainer
* Create new instance
*
* @param int $sectionCount
* @param array $style
* @param null|array|\PhpOffice\PhpWord\Style $style
*/
public function __construct($sectionCount, $style = null)
{
$this->sectionId = $sectionCount;
$this->setDocPart($this->container, $this->sectionId);
$this->style = new SectionStyle();
$this->setStyle($style);
if (null === $style) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't you remove this check, and pass true in setNewStyle instead?

$style = new SectionStyle();
}
$this->style = $this->setNewStyle(new SectionStyle(), $style);
}

/**
Expand Down
22 changes: 22 additions & 0 deletions tests/PhpWord/Element/SectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Style;
use PhpOffice\PhpWord\Style\Section as SectionStyle;

/**
* @covers \PhpOffice\PhpWord\Element\Section
Expand All @@ -27,6 +28,27 @@
*/
class SectionTest extends \PHPUnit\Framework\TestCase
{
public function testConstructorWithDefaultStyle()
{
$section = new Section(0);
$this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\Section', $section->getStyle());
}

public function testConstructorWithArrayStyle()
{
$section = new Section(0, array('orientation' => 'landscape'));
$style = $section->getStyle();
$this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\Section', $style);
$this->assertEquals('landscape', $style->getOrientation());
}

public function testConstructorWithObjectStyle()
{
$style = new SectionStyle();
$section = new Section(0, $style);
$this->assertSame($style, $section->getStyle());
}

/**
* @covers ::setStyle
*/
Expand Down
15 changes: 15 additions & 0 deletions tests/PhpWord/Writer/Word2007/ElementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -492,4 +492,19 @@ public function testTitleAndHeading()
$this->assertTrue($doc->elementExists('/w:document/w:body/w:p[2]/w:pPr/w:pStyle'));
$this->assertEquals('Heading1', $doc->getElementAttribute('/w:document/w:body/w:p[2]/w:pPr/w:pStyle', 'w:val'));
}

/**
* Test correct writing of text with ampersant in it
*/
public function testTextWithAmpersant()
{
\PhpOffice\PhpWord\Settings::setOutputEscapingEnabled(true);
$phpWord = new PhpWord();
$section = $phpWord->addSection();
$section->addText('this text contains an & (ampersant)');

$doc = TestHelperDOCX::getDocument($phpWord);
$this->assertTrue($doc->elementExists('/w:document/w:body/w:p/w:r/w:t'));
$this->assertEquals('this text contains an & (ampersant)', $doc->getElement('/w:document/w:body/w:p/w:r/w:t')->nodeValue);
}
}