PHPWord/tests/PhpWordTests/Writer/Word2007Test.php

196 lines
5.7 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;
use PhpOffice\PhpWord\PhpWord;
2015-10-10 19:22:19 +04:00
use PhpOffice\PhpWord\SimpleType\Jc;
2022-09-16 14:14:57 +02:00
use PhpOffice\PhpWord\Writer\Word2007;
use PhpOffice\PhpWordTests\AbstractWebServerEmbeddedTest;
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\Word2007.
2014-03-27 23:55:06 +07:00
*
* @runTestsInSeparateProcesses
*/
class Word2007Test extends AbstractWebServerEmbeddedTest
{
2014-03-29 22:26:00 +07:00
/**
2022-09-16 11:45:45 +02:00
* Tear down after each test.
2014-03-29 22:26:00 +07:00
*/
2022-09-16 11:45:45 +02:00
protected function tearDown(): void
2014-03-12 10:09:42 -04:00
{
TestHelperDOCX::clear();
}
/**
2022-09-16 11:45:45 +02:00
* Construct.
*/
2022-09-16 11:45:45 +02:00
public function testConstruct(): void
{
$object = new Word2007(new PhpWord());
2022-09-16 11:45:45 +02:00
$writerParts = [
2014-04-09 21:35:55 +07:00
'ContentTypes' => 'ContentTypes',
2022-09-16 11:45:45 +02:00
'Rels' => 'Rels',
'DocPropsApp' => 'DocPropsApp',
'Document' => 'Document',
'Styles' => 'Styles',
'Numbering' => 'Numbering',
'Settings' => 'Settings',
'WebSettings' => 'WebSettings',
'Header' => 'Header',
'Footer' => 'Footer',
'Footnotes' => 'Footnotes',
'Endnotes' => 'Footnotes',
];
2014-04-09 21:35:55 +07:00
foreach ($writerParts as $part => $type) {
2022-09-16 11:45:45 +02:00
self::assertInstanceOf(
"PhpOffice\\PhpWord\\Writer\\Word2007\\Part\\{$type}",
$object->getWriterPart($part)
);
2022-09-16 11:45:45 +02:00
self::assertInstanceOf(
'PhpOffice\\PhpWord\\Writer\\Word2007',
$object->getWriterPart($part)->getParentWriter()
);
}
}
/**
2022-09-16 11:45:45 +02:00
* Save.
*/
2022-09-16 11:45:45 +02:00
public function testSave(): void
{
2014-03-29 22:26:00 +07:00
$localImage = __DIR__ . '/../_files/images/earth.jpg';
$remoteImage = self::getRemoteGifImageUrl();
$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-04 20:06:37 +04:00
$section->addText('Test 1', 'Font', 'Paragraph');
$section->addTextBreak();
2016-06-04 20:06:37 +04:00
$section->addText('Test 2');
$section = $phpWord->addSection();
$textrun = $section->addTextRun();
2016-06-04 20:06:37 +04:00
$textrun->addText('Test 3');
$footnote = $textrun->addFootnote();
$footnote->addLink('https://github.com/PHPOffice/PHPWord');
$header = $section->addHeader();
2014-03-29 22:26:00 +07:00
$header->addImage($localImage);
$footer = $section->addFooter();
2014-03-29 22:26:00 +07:00
$footer->addImage($remoteImage);
$writer = new Word2007($phpWord);
$file = __DIR__ . '/../_files/temp.docx';
$writer->save($file);
2014-03-29 22:26:00 +07:00
2022-09-16 11:45:45 +02:00
self::assertFileExists($file);
2014-03-29 22:26:00 +07:00
unlink($file);
}
2014-03-12 10:09:42 -04:00
2014-03-16 07:40:02 +07:00
/**
2022-09-16 11:45:45 +02:00
* Save using disk caching.
2014-03-29 22:26:00 +07:00
*/
2022-09-16 11:45:45 +02:00
public function testSaveUseDiskCaching(): void
2014-03-29 22:26:00 +07:00
{
$phpWord = new PhpWord();
$section = $phpWord->addSection();
2016-06-04 20:06:37 +04:00
$section->addText('Test');
$footnote = $section->addFootnote();
2016-06-04 20:06:37 +04:00
$footnote->addText('Test');
2014-03-29 22:26:00 +07:00
$writer = new Word2007($phpWord);
$writer->setUseDiskCaching(true);
$file = __DIR__ . '/../_files/temp.docx';
2014-03-29 22:26:00 +07:00
$writer->save($file);
2022-09-16 11:45:45 +02:00
self::assertFileExists($file);
2014-03-29 22:26:00 +07:00
unlink($file);
}
/**
2022-09-16 11:45:45 +02:00
* Check content types.
2014-03-12 10:09:42 -04:00
*/
2022-09-16 11:45:45 +02:00
public function testCheckContentTypes(): void
2014-03-12 10:09:42 -04:00
{
2022-09-16 11:45:45 +02:00
$images = [
'mars_noext_jpg' => '1.jpg',
'mars.jpg' => '2.jpg',
'mario.gif' => '3.gif',
'firefox.png' => '4.png',
'duke_nukem.bmp' => '5.bmp',
2014-03-13 01:58:00 +07:00
'angela_merkel.tif' => '6.tif',
2022-09-16 11:45:45 +02:00
];
$phpWord = new PhpWord();
$section = $phpWord->addSection();
2014-03-13 01:58:00 +07:00
foreach ($images as $source => $target) {
2014-03-23 12:37:31 -04:00
$section->addImage(__DIR__ . "/../_files/images/{$source}");
2014-03-13 01:58:00 +07:00
}
2014-03-12 10:09:42 -04:00
$doc = TestHelperDOCX::getDocument($phpWord);
$mediaPath = $doc->getPath() . '/word/media';
2014-03-12 10:09:42 -04:00
2014-03-13 01:58:00 +07:00
foreach ($images as $source => $target) {
2022-09-16 11:45:45 +02:00
self::assertFileEquals(
2014-03-23 12:37:31 -04:00
__DIR__ . "/../_files/images/{$source}",
2014-03-13 01:58:00 +07:00
$mediaPath . "/section_image{$target}"
);
}
2014-03-12 10:09:42 -04:00
}
/**
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 Word2007();
2022-09-16 11:45:45 +02:00
self::assertNull($object->getWriterPart());
2014-03-29 22:26:00 +07:00
}
/**
2022-09-16 11:45:45 +02:00
* Set/get use disk caching.
2014-03-29 22:26:00 +07:00
*/
2022-09-16 11:45:45 +02:00
public function testSetGetUseDiskCaching(): void
2014-03-29 22:26:00 +07:00
{
2022-09-16 11:45:45 +02:00
$this->setOutputCallback(function (): void {
});
2014-03-29 22:26:00 +07:00
$phpWord = new PhpWord();
$phpWord->addSection();
2014-03-29 22:26:00 +07:00
$object = new Word2007($phpWord);
$object->setUseDiskCaching(true, PHPWORD_TESTS_BASE_DIR);
2014-03-29 22:26:00 +07:00
$writer = new Word2007($phpWord);
$writer->save('php://output');
2022-09-16 11:45:45 +02:00
self::assertTrue($object->isUseDiskCaching());
}
/**
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']);
$object = new Word2007();
$object->setUseDiskCaching(true, $dir);
}
2014-03-13 01:58:00 +07:00
}