2013-12-11 16:09:58 -05:00
|
|
|
<?php
|
2014-03-23 11:59:22 -04:00
|
|
|
namespace PhpOffice\PhpWord\Tests;
|
2013-12-11 16:09:58 -05:00
|
|
|
|
2014-03-22 10:06:08 +04:00
|
|
|
use PhpOffice\PhpWord\PhpWord;
|
2014-03-19 11:04:48 +04:00
|
|
|
use PhpOffice\PhpWord\IOFactory;
|
2013-12-11 16:09:58 -05:00
|
|
|
|
2014-02-24 19:17:06 +01:00
|
|
|
class TestHelperDOCX
|
2013-12-11 16:09:58 -05:00
|
|
|
{
|
2014-03-12 13:30:02 +07:00
|
|
|
/** @var string $file */
|
2014-02-24 19:17:06 +01:00
|
|
|
static protected $file;
|
|
|
|
|
|
2013-12-11 16:09:58 -05:00
|
|
|
/**
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function clear()
|
|
|
|
|
{
|
2014-03-11 14:19:33 -04:00
|
|
|
if (file_exists(self::$file)) {
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @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
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public static function getFile()
|
|
|
|
|
{
|
|
|
|
|
return self::$file;
|
|
|
|
|
}
|
2014-03-23 12:07:29 -04:00
|
|
|
}
|