PHPWord/tests/PhpWord/Tests/_includes/TestHelperDOCX.php

105 lines
2.7 KiB
PHP
Raw 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
*
* @link https://github.com/PHPOffice/PHPWord
* @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
*/
namespace PhpOffice\PhpWord\Tests;
2013-12-11 16:09:58 -05: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
*
* @param \PhpOffice\PhpWord\PhpWord $phpWord
* @param string $writerName
* @return \PhpOffice\PhpWord\Tests\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(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
}
$xmlWriter = IOFactory::createWriter($phpWord, $writerName);
$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) {
$zip->extractTo(sys_get_temp_dir() . '/PhpWord_Unit_Test/');
2013-12-11 16:09:58 -05:00
$zip->close();
}
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()
{
if (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(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;
} elseif (is_file($dir . "/" . $file)) {
2013-12-11 16:09:58 -05:00
unlink($dir . "/" . $file);
} elseif (is_dir($dir . "/" . $file)) {
2013-12-11 16:09:58 -05:00
self::deleteDir($dir . "/" . $file);
}
}
rmdir($dir);
}
/**
2014-03-30 14:15:23 +07:00
* Get file
*
* @return string
*/
public static function getFile()
{
return self::$file;
}
}