2013-12-11 16:09:58 -05:00
|
|
|
<?php
|
2014-03-30 14:15:23 +07:00
|
|
|
/**
|
2014-05-05 13:06:53 +04: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
|
|
|
*
|
|
|
|
|
* @link https://github.com/PHPOffice/PHPWord
|
2014-05-05 12:38:31 +04:00
|
|
|
* @copyright 2010-2014 PHPWord contributors
|
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
|
|
|
*/
|
|
|
|
|
|
2014-03-23 11:59:22 -04:00
|
|
|
namespace PhpOffice\PhpWord\Tests;
|
2013-12-11 16:09:58 -05:00
|
|
|
|
2014-03-19 11:04:48 +04:00
|
|
|
use PhpOffice\PhpWord\IOFactory;
|
2014-05-04 21:03:28 +04:00
|
|
|
use PhpOffice\PhpWord\PhpWord;
|
2013-12-11 16:09:58 -05:00
|
|
|
|
2014-03-30 14:15:23 +07:00
|
|
|
/**
|
|
|
|
|
* Test helper class
|
|
|
|
|
*/
|
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
|
|
|
/**
|
|
|
|
|
* Temporary file name
|
|
|
|
|
*
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
2014-02-24 19:17:06 +01:00
|
|
|
static protected $file;
|
|
|
|
|
|
2013-12-11 16:09:58 -05:00
|
|
|
/**
|
2014-03-30 14:15:23 +07:00
|
|
|
* Get document content
|
|
|
|
|
*
|
2014-03-23 12:07:29 -04:00
|
|
|
* @param \PhpOffice\PhpWord\PhpWord $phpWord
|
|
|
|
|
* @param string $writerName
|
|
|
|
|
* @return \PhpOffice\PhpWord\Tests\XmlDocument
|
2013-12-11 16:09:58 -05:00
|
|
|
*/
|
2014-03-20 16:54:12 +04:00
|
|
|
public static function getDocument(PhpWord $phpWord, $writerName = 'Word2007')
|
2013-12-11 16:09:58 -05:00
|
|
|
{
|
2014-03-20 16:54:12 +04:00
|
|
|
self::$file = tempnam(sys_get_temp_dir(), 'PhpWord');
|
|
|
|
|
if (!is_dir(sys_get_temp_dir() . '/PhpWord_Unit_Test/')) {
|
|
|
|
|
mkdir(sys_get_temp_dir() . '/PhpWord_Unit_Test/');
|
2014-02-24 19:17:06 +01:00
|
|
|
}
|
|
|
|
|
|
2014-03-20 16:54:12 +04:00
|
|
|
$xmlWriter = IOFactory::createWriter($phpWord, $writerName);
|
2014-03-19 11:04:48 +04:00
|
|
|
$xmlWriter->save(self::$file);
|
2013-12-11 16:09:58 -05:00
|
|
|
|
|
|
|
|
$zip = new \ZipArchive;
|
2014-02-24 19:17:06 +01:00
|
|
|
$res = $zip->open(self::$file);
|
2013-12-11 16:09:58 -05:00
|
|
|
if ($res === true) {
|
2014-03-20 16:54:12 +04:00
|
|
|
$zip->extractTo(sys_get_temp_dir() . '/PhpWord_Unit_Test/');
|
2013-12-11 16:09:58 -05:00
|
|
|
$zip->close();
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-20 16:54:12 +04:00
|
|
|
return new XmlDocument(sys_get_temp_dir() . '/PhpWord_Unit_Test/');
|
2013-12-11 16:09:58 -05:00
|
|
|
}
|
|
|
|
|
|
2014-03-30 14:15:23 +07:00
|
|
|
/**
|
|
|
|
|
* Clear document
|
|
|
|
|
*/
|
2013-12-11 16:09:58 -05:00
|
|
|
public static function clear()
|
|
|
|
|
{
|
2014-04-05 22:53:39 +07:00
|
|
|
if (file_exists(self::$file)) {
|
2014-03-11 14:19:33 -04:00
|
|
|
unlink(self::$file);
|
2014-02-24 19:17:06 +01:00
|
|
|
}
|
2014-03-20 16:54:12 +04:00
|
|
|
if (is_dir(sys_get_temp_dir() . '/PhpWord_Unit_Test/')) {
|
|
|
|
|
self::deleteDir(sys_get_temp_dir() . '/PhpWord_Unit_Test/');
|
2014-02-24 19:17:06 +01:00
|
|
|
}
|
2013-12-11 16:09:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2014-03-30 14:15:23 +07:00
|
|
|
* Delete directory
|
|
|
|
|
*
|
2013-12-11 16:09:58 -05:00
|
|
|
* @param string $dir
|
|
|
|
|
*/
|
|
|
|
|
public static function deleteDir($dir)
|
|
|
|
|
{
|
|
|
|
|
foreach (scandir($dir) as $file) {
|
|
|
|
|
if ($file === '.' || $file === '..') {
|
|
|
|
|
continue;
|
2014-03-12 13:30:02 +07:00
|
|
|
} elseif (is_file($dir . "/" . $file)) {
|
2013-12-11 16:09:58 -05:00
|
|
|
unlink($dir . "/" . $file);
|
2014-03-12 13:30:02 +07:00
|
|
|
} elseif (is_dir($dir . "/" . $file)) {
|
2013-12-11 16:09:58 -05:00
|
|
|
self::deleteDir($dir . "/" . $file);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rmdir($dir);
|
|
|
|
|
}
|
2014-03-12 11:22:41 -04:00
|
|
|
|
|
|
|
|
/**
|
2014-03-30 14:15:23 +07:00
|
|
|
* Get file
|
|
|
|
|
*
|
2014-03-12 11:22:41 -04:00
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public static function getFile()
|
|
|
|
|
{
|
|
|
|
|
return self::$file;
|
|
|
|
|
}
|
2014-03-24 00:26:10 +07:00
|
|
|
}
|