|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * This file is part of PHPWord - A pure PHP library for reading and writing |
| 4 | + * word processing documents. |
| 5 | + * |
| 6 | + * PHPWord is free software distributed under the terms of the GNU Lesser |
| 7 | + * General Public License version 3 as published by the Free Software Foundation. |
| 8 | + * |
| 9 | + * For the full copyright and license information, please read the LICENSE |
| 10 | + * file that was distributed with this source code. For the full list of |
| 11 | + * contributors, visit https://github.com/PHPOffice/PHPWord/contributors. |
| 12 | + * |
| 13 | + * @see https://github.com/PHPOffice/PHPWord |
| 14 | + * |
| 15 | + * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3 |
| 16 | + */ |
| 17 | +declare(strict_types=1); |
| 18 | + |
| 19 | +namespace PhpOffice\PhpWordTests\Writer\Word2007\Element; |
| 20 | + |
| 21 | +use PhpOffice\PhpWord\PhpWord; |
| 22 | +use PhpOffice\PhpWordTests\TestHelperDOCX; |
| 23 | + |
| 24 | +/** |
| 25 | + * Test class for PhpOffice\PhpWord\Writer\Word2007\Element subnamespace. |
| 26 | + */ |
| 27 | +class TitleTest extends \PHPUnit\Framework\TestCase |
| 28 | +{ |
| 29 | + /** |
| 30 | + * Executed after each method of the class. |
| 31 | + */ |
| 32 | + protected function tearDown(): void |
| 33 | + { |
| 34 | + TestHelperDOCX::clear(); |
| 35 | + } |
| 36 | + |
| 37 | + public function testWriteTitleWithStyle(): void |
| 38 | + { |
| 39 | + $phpWord = new PhpWord(); |
| 40 | + $phpWord->addTitleStyle(0, ['size' => 14, 'italic' => true]); |
| 41 | + |
| 42 | + $section = $phpWord->addSection(); |
| 43 | + $section->addTitle('Test Title0', 0); |
| 44 | + |
| 45 | + $doc = TestHelperDOCX::getDocument($phpWord); |
| 46 | + |
| 47 | + self::assertTrue($doc->elementExists('/w:document/w:body/w:p[1]/w:r/w:t')); |
| 48 | + self::assertEquals('Test Title0', $doc->getElement('/w:document/w:body/w:p[1]/w:r/w:t')->textContent); |
| 49 | + self::assertTrue($doc->elementExists('/w:document/w:body/w:p[1]/w:pPr/w:pStyle')); |
| 50 | + self::assertEquals('Title', $doc->getElementAttribute('/w:document/w:body/w:p[1]/w:pPr/w:pStyle', 'w:val')); |
| 51 | + } |
| 52 | + |
| 53 | + public function testWriteTitleWithoutStyle(): void |
| 54 | + { |
| 55 | + $phpWord = new PhpWord(); |
| 56 | + |
| 57 | + $section = $phpWord->addSection(); |
| 58 | + $section->addTitle('Test Title0', 0); |
| 59 | + |
| 60 | + $doc = TestHelperDOCX::getDocument($phpWord); |
| 61 | + |
| 62 | + self::assertTrue($doc->elementExists('/w:document/w:body/w:p[1]/w:r/w:t')); |
| 63 | + self::assertEquals('Test Title0', $doc->getElement('/w:document/w:body/w:p[1]/w:r/w:t')->textContent); |
| 64 | + self::assertFalse($doc->elementExists('/w:document/w:body/w:p[1]/w:pPr')); |
| 65 | + } |
| 66 | + |
| 67 | + public function testWriteHeadingWithStyle(): void |
| 68 | + { |
| 69 | + $phpWord = new PhpWord(); |
| 70 | + $phpWord->addTitleStyle(1, ['bold' => true], ['spaceAfter' => 240]); |
| 71 | + |
| 72 | + $section = $phpWord->addSection(); |
| 73 | + $section->addTitle('TestHeading 1', 1); |
| 74 | + |
| 75 | + $doc = TestHelperDOCX::getDocument($phpWord); |
| 76 | + |
| 77 | + self::assertTrue($doc->elementExists('/w:document/w:body/w:p[1]/w:r/w:t')); |
| 78 | + self::assertEquals('TestHeading 1', $doc->getElement('/w:document/w:body/w:p[1]/w:r/w:t')->textContent); |
| 79 | + self::assertTrue($doc->elementExists('/w:document/w:body/w:p[1]/w:pPr/w:pStyle')); |
| 80 | + self::assertEquals('Heading1', $doc->getElementAttribute('/w:document/w:body/w:p[1]/w:pPr/w:pStyle', 'w:val')); |
| 81 | + } |
| 82 | + |
| 83 | + public function testWriteHeadingWithoutStyle(): void |
| 84 | + { |
| 85 | + $phpWord = new PhpWord(); |
| 86 | + |
| 87 | + $section = $phpWord->addSection(); |
| 88 | + $section->addTitle('TestHeading 1', 1); |
| 89 | + |
| 90 | + $doc = TestHelperDOCX::getDocument($phpWord); |
| 91 | + |
| 92 | + self::assertTrue($doc->elementExists('/w:document/w:body/w:p[1]/w:r/w:t')); |
| 93 | + self::assertEquals('TestHeading 1', $doc->getElement('/w:document/w:body/w:p[1]/w:r/w:t')->textContent); |
| 94 | + self::assertFalse($doc->elementExists('/w:document/w:body/w:p[1]/w:pPr')); |
| 95 | + } |
| 96 | +} |
0 commit comments