112 lines
3.8 KiB
PHP
Raw Normal View History

<?php
2014-03-27 23:55:06 +07:00
/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
2014-03-27 23:55:06 +07:00
*
* @see https://github.com/PHPOffice/PHPWord
2022-09-16 11:45:45 +02:00
*
2014-05-04 21:03:28 +04:00
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
2014-03-27 23:55:06 +07:00
*/
2022-09-16 14:09:17 +02:00
namespace PhpOffice\PhpWordTests\Writer\ODText\Part;
use PhpOffice\PhpWord\PhpWord;
2015-10-10 19:22:19 +04:00
use PhpOffice\PhpWord\SimpleType\Jc;
2022-09-16 14:09:17 +02:00
use PhpOffice\PhpWordTests\TestHelperDOCX;
/**
2022-09-16 11:45:45 +02:00
* Test class for PhpOffice\PhpWord\Writer\ODText\Part\Content.
2014-03-27 23:55:06 +07:00
*
* @coversDefaultClass \PhpOffice\PhpWord\Writer\ODText\Part\Content
*/
class ContentTest extends \PHPUnit\Framework\TestCase
{
/**
2022-09-16 11:45:45 +02:00
* Executed before each method of the class.
*/
2022-09-16 11:45:45 +02:00
protected function tearDown(): void
{
TestHelperDOCX::clear();
}
2014-04-17 03:47:43 +07:00
/**
2022-09-16 11:45:45 +02:00
* Test write content.
*/
2022-09-16 11:45:45 +02:00
public function testWriteContent(): void
{
$imageSrc = __DIR__ . '/../../../_files/images/PhpWord.png';
$objectSrc = __DIR__ . '/../../../_files/documents/sheet.xls';
$expected = 'Expected';
$phpWord = new PhpWord();
2014-04-17 03:47:43 +07:00
$docProps = $phpWord->getDocInfo();
2014-05-31 17:39:54 +07:00
$docProps->setCustomProperty('Company', 'PHPWord');
$phpWord->setDefaultFontName('Verdana');
2022-09-16 11:45:45 +02:00
$phpWord->addFontStyle('Font', ['size' => 11]);
$phpWord->addParagraphStyle('Paragraph', ['alignment' => Jc::CENTER]);
$phpWord->addTableStyle('tblStyle', ['width' => 100]);
2014-04-17 03:47:43 +07:00
2022-09-16 11:45:45 +02:00
$section = $phpWord->addSection(['colsNum' => 2]);
2016-06-13 20:14:54 +04:00
$section->addText($expected);
$section->addText('Test font style', 'Font');
$section->addText('Test paragraph style', null, 'Paragraph');
$section->addLink('https://github.com/PHPOffice/PHPWord', 'PHPWord on GitHub');
$section->addTitle('Test title', 1);
2014-04-17 03:47:43 +07:00
$section->addTextBreak();
$section->addPageBreak();
2016-06-13 20:14:54 +04:00
$section->addListItem('Test list item');
2022-09-16 11:45:45 +02:00
$section->addImage($imageSrc, ['width' => 50]);
$section->addObject($objectSrc);
$section->addTOC();
2014-04-17 03:47:43 +07:00
$textrun = $section->addTextRun();
2016-06-13 20:14:54 +04:00
$textrun->addText('Test text run');
2014-04-17 03:47:43 +07:00
2022-09-16 11:45:45 +02:00
$table = $section->addTable(['width' => 50]);
2014-04-17 03:47:43 +07:00
$cell = $table->addRow()->addCell();
$cell = $table->addRow()->addCell();
2016-06-13 20:14:54 +04:00
$cell->addText('Test');
$cell->addLink('https://github.com/PHPOffice/PHPWord', 'PHPWord on GitHub');
2014-04-17 03:47:43 +07:00
$cell->addTextBreak();
2016-06-13 20:14:54 +04:00
$cell->addListItem('Test list item');
2014-04-17 03:47:43 +07:00
$cell->addImage($imageSrc);
$cell->addObject($objectSrc);
$textrun = $cell->addTextRun();
2016-06-13 20:14:54 +04:00
$textrun->addText('Test text run');
2017-11-10 23:37:02 +01:00
$section->addPageBreak();
2014-04-17 03:47:43 +07:00
$footer = $section->addFooter();
2016-06-13 20:14:54 +04:00
$footer->addPreserveText('{PAGE}');
2014-04-17 03:47:43 +07:00
2014-05-24 00:11:06 +07:00
$table = $section->addTable('tblStyle')->addRow()->addCell();
$doc = TestHelperDOCX::getDocument($phpWord, 'ODText');
$element = '/office:document-content/office:body/office:text/text:section/text:p[2]';
2022-09-16 11:45:45 +02:00
self::assertEquals($expected, $doc->getElement($element, 'content.xml')->nodeValue);
}
2014-04-17 03:47:43 +07:00
/**
2022-09-16 11:45:45 +02:00
* Test no paragraph style.
2014-04-17 03:47:43 +07:00
*/
2022-09-16 11:45:45 +02:00
public function testWriteNoStyle(): void
2014-04-17 03:47:43 +07:00
{
$phpWord = new PhpWord();
2022-09-16 11:45:45 +02:00
$phpWord->addFontStyle('Font', ['size' => 11]);
2014-04-17 03:47:43 +07:00
$doc = TestHelperDOCX::getDocument($phpWord, 'ODText');
$element = '/office:document-content/office:automatic-styles/style:style';
2022-09-16 11:45:45 +02:00
self::assertTrue($doc->elementExists($element, 'content.xml'));
2014-04-17 03:47:43 +07:00
}
}