PHPWord/tests/PhpWordTests/TestHelperDOCX.php

119 lines
3.1 KiB
PHP
Raw Permalink Normal View History

2013-12-11 16:09:58 -05:00
<?php
2014-03-30 14:15:23 +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-30 14:15:23 +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-30 14:15:23 +07:00
*/
2022-09-16 14:09:17 +02:00
namespace PhpOffice\PhpWordTests;
2013-12-11 16:09:58 -05:00
use PhpOffice\PhpWord\Exception\CreateTemporaryFileException;
2022-09-16 14:09:17 +02:00
use PhpOffice\PhpWord\IOFactory;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Settings;
2022-09-16 11:45:45 +02:00
use ZipArchive;
2013-12-11 16:09:58 -05:00
2014-03-30 14:15:23 +07:00
/**
2022-09-16 11:45:45 +02:00
* Test helper class.
2014-03-30 14:15:23 +07:00
*/
2014-02-24 19:17:06 +01:00
class TestHelperDOCX
2013-12-11 16:09:58 -05:00
{
2014-03-30 14:15:23 +07:00
/**
2022-09-16 11:45:45 +02:00
* Temporary file name.
2014-03-30 14:15:23 +07:00
*
* @var string
*/
protected static $file;
2014-02-24 19:17:06 +01:00
2013-12-11 16:09:58 -05:00
/**
2022-09-16 11:45:45 +02:00
* Get document content.
2014-03-30 14:15:23 +07:00
*
* @since 0.12.0 Throws CreateTemporaryFileException.
*
* @param string $writerName
*
2022-09-16 14:09:17 +02:00
* @return \PhpOffice\PhpWordTests\XmlDocument
2013-12-11 16:09:58 -05:00
*/
public static function getDocument(PhpWord $phpWord, $writerName = 'Word2007')
2013-12-11 16:09:58 -05:00
{
self::$file = tempnam(Settings::getTempDir(), 'PhpWord');
if (false === self::$file) {
throw new CreateTemporaryFileException();
}
if (!is_dir(Settings::getTempDir() . '/PhpWord_Unit_Test/')) {
mkdir(Settings::getTempDir() . '/PhpWord_Unit_Test/');
2014-02-24 19:17:06 +01:00
}
$xmlWriter = IOFactory::createWriter($phpWord, $writerName);
$xmlWriter->save(self::$file);
2013-12-11 16:09:58 -05:00
2022-09-16 11:45:45 +02:00
$zip = new ZipArchive();
2014-02-24 19:17:06 +01:00
$res = $zip->open(self::$file);
if (true === $res) {
$zip->extractTo(Settings::getTempDir() . '/PhpWord_Unit_Test/');
2013-12-11 16:09:58 -05:00
$zip->close();
}
$doc = new XmlDocument(Settings::getTempDir() . '/PhpWord_Unit_Test/');
if ($writerName === 'ODText') {
$doc->setDefaultFile('content.xml');
}
return $doc;
2013-12-11 16:09:58 -05:00
}
2014-03-30 14:15:23 +07:00
/**
2022-09-16 11:45:45 +02:00
* Clear document.
2014-03-30 14:15:23 +07:00
*/
2022-09-16 11:45:45 +02:00
public static function clear(): void
2013-12-11 16:09:58 -05:00
{
if (self::$file && file_exists(self::$file)) {
2014-03-11 14:19:33 -04:00
unlink(self::$file);
2014-02-24 19:17:06 +01:00
}
if (is_dir(Settings::getTempDir() . '/PhpWord_Unit_Test/')) {
self::deleteDir(Settings::getTempDir() . '/PhpWord_Unit_Test/');
2014-02-24 19:17:06 +01:00
}
2013-12-11 16:09:58 -05:00
}
/**
2022-09-16 11:45:45 +02:00
* Delete directory.
2014-03-30 14:15:23 +07:00
*
2013-12-11 16:09:58 -05:00
* @param string $dir
*/
2022-09-16 11:45:45 +02:00
public static function deleteDir($dir): void
2013-12-11 16:09:58 -05:00
{
foreach (scandir($dir) as $file) {
if ('.' === $file || '..' === $file) {
2013-12-11 16:09:58 -05:00
continue;
} elseif (is_file($dir . '/' . $file)) {
unlink($dir . '/' . $file);
} elseif (is_dir($dir . '/' . $file)) {
self::deleteDir($dir . '/' . $file);
2013-12-11 16:09:58 -05:00
}
}
rmdir($dir);
}
/**
2022-09-16 11:45:45 +02:00
* Get file.
2014-03-30 14:15:23 +07:00
*
* @return string
*/
public static function getFile()
{
return self::$file;
}
}