From 6389b362d79141fc0d926615f49574099acb9e6b Mon Sep 17 00:00:00 2001 From: Nathan Dench Date: Thu, 28 Jun 2018 19:47:42 +1000 Subject: [PATCH 1/5] Add ability to pass a Style object to a Section --- src/PhpWord/Element/Section.php | 6 ++++-- tests/PhpWord/Element/SectionTest.php | 22 ++++++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/PhpWord/Element/Section.php b/src/PhpWord/Element/Section.php index d612fc0172..7ace82b383 100644 --- a/src/PhpWord/Element/Section.php +++ b/src/PhpWord/Element/Section.php @@ -65,8 +65,10 @@ 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) { + $style = new SectionStyle(); + } + $this->style = $this->setNewStyle(new SectionStyle(), $style); } /** diff --git a/tests/PhpWord/Element/SectionTest.php b/tests/PhpWord/Element/SectionTest.php index 265307d765..a6829bb908 100644 --- a/tests/PhpWord/Element/SectionTest.php +++ b/tests/PhpWord/Element/SectionTest.php @@ -19,6 +19,7 @@ use PhpOffice\PhpWord\PhpWord; use PhpOffice\PhpWord\Style; +use PhpOffice\PhpWord\Style\Section as SectionStyle; /** * @covers \PhpOffice\PhpWord\Element\Section @@ -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->assertSame('landscape', $style->getOrientation()); + } + + public function testConstorWithObjectStyle() + { + $style = new SectionStyle(); + $section = new Section(0, $style); + $this->assertSame($style, $section->getStyle()); + } + /** * @covers ::setStyle */ From e05b6dc3fdb83fa55f1c48b2c8ea560a51bf4cb4 Mon Sep 17 00:00:00 2001 From: Nathan Dench Date: Fri, 29 Jun 2018 09:20:30 +1000 Subject: [PATCH 2/5] Fix typo --- tests/PhpWord/Element/SectionTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/PhpWord/Element/SectionTest.php b/tests/PhpWord/Element/SectionTest.php index a6829bb908..8b5ab6bd41 100644 --- a/tests/PhpWord/Element/SectionTest.php +++ b/tests/PhpWord/Element/SectionTest.php @@ -42,7 +42,7 @@ public function testConstructorWithArrayStyle() $this->assertSame('landscape', $style->getOrientation()); } - public function testConstorWithObjectStyle() + public function testConstructorWithObjectStyle() { $style = new SectionStyle(); $section = new Section(0, $style); From eba479978a601c4cc13cba1bff10d51388f6349c Mon Sep 17 00:00:00 2001 From: Nathan Dench Date: Thu, 28 Jun 2018 19:47:42 +1000 Subject: [PATCH 3/5] Add ability to pass a Style object to a Section --- src/PhpWord/Element/Section.php | 6 ++++-- tests/PhpWord/Element/SectionTest.php | 22 ++++++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/PhpWord/Element/Section.php b/src/PhpWord/Element/Section.php index d612fc0172..7ace82b383 100644 --- a/src/PhpWord/Element/Section.php +++ b/src/PhpWord/Element/Section.php @@ -65,8 +65,10 @@ 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) { + $style = new SectionStyle(); + } + $this->style = $this->setNewStyle(new SectionStyle(), $style); } /** diff --git a/tests/PhpWord/Element/SectionTest.php b/tests/PhpWord/Element/SectionTest.php index 265307d765..a6829bb908 100644 --- a/tests/PhpWord/Element/SectionTest.php +++ b/tests/PhpWord/Element/SectionTest.php @@ -19,6 +19,7 @@ use PhpOffice\PhpWord\PhpWord; use PhpOffice\PhpWord\Style; +use PhpOffice\PhpWord\Style\Section as SectionStyle; /** * @covers \PhpOffice\PhpWord\Element\Section @@ -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->assertSame('landscape', $style->getOrientation()); + } + + public function testConstorWithObjectStyle() + { + $style = new SectionStyle(); + $section = new Section(0, $style); + $this->assertSame($style, $section->getStyle()); + } + /** * @covers ::setStyle */ From 862b658ee98b912a38fd3cd2b796809f95c15d7d Mon Sep 17 00:00:00 2001 From: Nathan Dench Date: Fri, 29 Jun 2018 09:20:30 +1000 Subject: [PATCH 4/5] Fix typo --- tests/PhpWord/Element/SectionTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/PhpWord/Element/SectionTest.php b/tests/PhpWord/Element/SectionTest.php index a6829bb908..8b5ab6bd41 100644 --- a/tests/PhpWord/Element/SectionTest.php +++ b/tests/PhpWord/Element/SectionTest.php @@ -42,7 +42,7 @@ public function testConstructorWithArrayStyle() $this->assertSame('landscape', $style->getOrientation()); } - public function testConstorWithObjectStyle() + public function testConstructorWithObjectStyle() { $style = new SectionStyle(); $section = new Section(0, $style); From b984649d97253fc2d285ef724e9daa8af1f9df80 Mon Sep 17 00:00:00 2001 From: troosan Date: Sat, 8 Dec 2018 23:09:29 +0100 Subject: [PATCH 5/5] update changelog add test --- CHANGELOG.md | 1 + src/PhpWord/Element/Section.php | 2 +- tests/PhpWord/Element/SectionTest.php | 2 +- tests/PhpWord/Writer/Word2007/ElementTest.php | 15 +++++++++++++++ 4 files changed, 18 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 735f6d4dee..e475b4e4ba 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 ### Fixed - Fix regex in `cloneBlock` function @nicoder #1269 diff --git a/src/PhpWord/Element/Section.php b/src/PhpWord/Element/Section.php index 7ace82b383..b495ef7bca 100644 --- a/src/PhpWord/Element/Section.php +++ b/src/PhpWord/Element/Section.php @@ -59,7 +59,7 @@ 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) { diff --git a/tests/PhpWord/Element/SectionTest.php b/tests/PhpWord/Element/SectionTest.php index 8b5ab6bd41..83d1214e45 100644 --- a/tests/PhpWord/Element/SectionTest.php +++ b/tests/PhpWord/Element/SectionTest.php @@ -39,7 +39,7 @@ public function testConstructorWithArrayStyle() $section = new Section(0, array('orientation' => 'landscape')); $style = $section->getStyle(); $this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\Section', $style); - $this->assertSame('landscape', $style->getOrientation()); + $this->assertEquals('landscape', $style->getOrientation()); } public function testConstructorWithObjectStyle() diff --git a/tests/PhpWord/Writer/Word2007/ElementTest.php b/tests/PhpWord/Writer/Word2007/ElementTest.php index 25c62eccea..dc75a33574 100644 --- a/tests/PhpWord/Writer/Word2007/ElementTest.php +++ b/tests/PhpWord/Writer/Word2007/ElementTest.php @@ -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); + } }