PHPWord/test/common/TestHelperDOCX.php

72 lines
1.8 KiB
PHP
Raw Normal View History

2013-12-11 16:09:58 -05:00
<?php
namespace PhpWord\Tests;
2013-12-11 16:09:58 -05:00
use PhpOffice\PhpWord\PhpWord;
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
{
/** @var string $file */
2014-02-24 19:17:06 +01:00
static protected $file;
2013-12-11 16:09:58 -05:00
/**
* @param \PhpOffice\PhpWord\PhpWord $phpWord
* @return \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
}
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
}
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;
} 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);
}
/**
* @return string
*/
public static function getFile()
{
return self::$file;
}
2014-03-13 01:58:00 +07:00
}