PHPWord/tests/PhpWord/Writer/ODTextTest.php

147 lines
4.4 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
*/
2015-11-15 13:33:05 +04:00
namespace PhpOffice\PhpWord\Writer;
use PhpOffice\PhpWord\PhpWord;
2015-10-10 19:22:19 +04:00
use PhpOffice\PhpWord\SimpleType\Jc;
/**
2022-09-16 11:45:45 +02:00
* Test class for PhpOffice\PhpWord\Writer\ODText.
2014-03-27 23:55:06 +07:00
*
* @runTestsInSeparateProcesses
*/
class ODTextTest extends \PHPUnit\Framework\TestCase
{
/**
2022-09-16 11:45:45 +02:00
* Construct.
*/
2022-09-16 11:45:45 +02:00
public function testConstruct(): void
{
$object = new ODText(new PhpWord());
2022-09-16 11:45:45 +02:00
self::assertInstanceOf('PhpOffice\\PhpWord\\PhpWord', $object->getPhpWord());
2022-09-16 11:45:45 +02:00
self::assertEquals('./', $object->getDiskCachingDirectory());
foreach (['Content', 'Manifest', 'Meta', 'Mimetype', 'Styles'] as $part) {
self::assertInstanceOf(
"PhpOffice\\PhpWord\\Writer\\ODText\\Part\\{$part}",
$object->getWriterPart($part)
);
2022-09-16 11:45:45 +02:00
self::assertInstanceOf(
'PhpOffice\\PhpWord\\Writer\\ODText',
$object->getWriterPart($part)->getParentWriter()
);
}
}
/**
2022-09-16 11:45:45 +02:00
* Construct with null.
*/
2022-09-16 11:45:45 +02:00
public function testConstructWithNull(): void
{
$this->expectException(\PhpOffice\PhpWord\Exception\Exception::class);
$this->expectExceptionMessage('No PhpWord assigned.');
$object = new ODText();
$object->getPhpWord();
}
/**
2022-09-16 11:45:45 +02:00
* Save.
*/
2022-09-16 11:45:45 +02:00
public function testSave(): void
{
$imageSrc = __DIR__ . '/../_files/images/PhpWord.png';
$objectSrc = __DIR__ . '/../_files/documents/sheet.xls';
$file = __DIR__ . '/../_files/temp.odt';
$phpWord = new PhpWord();
2022-09-16 11:45:45 +02:00
$phpWord->addFontStyle('Font', ['size' => 11]);
$phpWord->addParagraphStyle('Paragraph', ['alignment' => Jc::CENTER]);
$section = $phpWord->addSection();
2016-06-13 20:14:54 +04:00
$section->addText('Test 1', 'Font');
$section->addTextBreak();
2016-06-13 20:14:54 +04:00
$section->addText('Test 2', null, 'Paragraph');
$section->addLink('https://github.com/PHPOffice/PHPWord');
2016-06-13 20:14:54 +04:00
$section->addTitle('Test', 1);
$section->addPageBreak();
2016-06-13 20:14:54 +04:00
$section->addTable()->addRow()->addCell()->addText('Test');
$section->addListItem('Test');
$section->addImage($imageSrc);
$section->addObject($objectSrc);
$section->addTOC();
$section = $phpWord->addSection();
$textrun = $section->addTextRun();
2016-06-13 20:14:54 +04:00
$textrun->addText('Test 3');
$writer = new ODText($phpWord);
$writer->save($file);
2022-09-16 11:45:45 +02:00
self::assertFileExists($file);
unlink($file);
}
/**
2022-09-16 11:45:45 +02:00
* Save php output.
2014-03-29 22:26:00 +07:00
*
* @todo Haven't got any method to test this
*/
2022-09-16 11:45:45 +02:00
public function testSavePhpOutput(): void
{
2022-09-16 11:45:45 +02:00
$this->setOutputCallback(function (): void {
});
$phpWord = new PhpWord();
$section = $phpWord->addSection();
2016-06-13 20:14:54 +04:00
$section->addText('Test');
$writer = new ODText($phpWord);
$writer->save('php://output');
2022-09-16 11:45:45 +02:00
self::assertNotNull($this->getActualOutput());
}
/**
2022-09-16 11:45:45 +02:00
* Get writer part return null value.
*/
2022-09-16 11:45:45 +02:00
public function testGetWriterPartNull(): void
{
$object = new ODText();
2022-09-16 11:45:45 +02:00
self::assertNull($object->getWriterPart('foo'));
}
/**
2022-09-16 11:45:45 +02:00
* Set/get use disk caching.
*/
2022-09-16 11:45:45 +02:00
public function testSetGetUseDiskCaching(): void
{
$object = new ODText();
$object->setUseDiskCaching(true, PHPWORD_TESTS_BASE_DIR);
2022-09-16 11:45:45 +02:00
self::assertTrue($object->isUseDiskCaching());
self::assertEquals(PHPWORD_TESTS_BASE_DIR, $object->getDiskCachingDirectory());
}
/**
2022-09-16 11:45:45 +02:00
* Use disk caching exception.
*/
2022-09-16 11:45:45 +02:00
public function testSetUseDiskCachingException(): void
{
$this->expectException(\PhpOffice\PhpWord\Exception\Exception::class);
2022-09-16 11:45:45 +02:00
$dir = implode(DIRECTORY_SEPARATOR, [PHPWORD_TESTS_BASE_DIR, 'foo']);
2014-03-23 21:29:07 +07:00
$object = new ODText();
$object->setUseDiskCaching(true, $dir);
}
}