-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
Copy pathTestHelperDOCX.php
120 lines (106 loc) · 3.07 KB
/
TestHelperDOCX.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php
/**
* 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.
*
* @see https://github.com/PHPOffice/PHPWord
*
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\PhpWordTests;
use PhpOffice\PhpWord\Exception\CreateTemporaryFileException;
use PhpOffice\PhpWord\IOFactory;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Settings;
use ZipArchive;
/**
* Test helper class.
*/
class TestHelperDOCX
{
/**
* Temporary file name.
*
* @var string
*/
protected static $file;
/**
* Get document content.
*
* @since 0.12.0 Throws CreateTemporaryFileException.
*
* @param string $writerName
*
* @return XmlDocument
*/
public static function getDocument(PhpWord $phpWord, $writerName = 'Word2007')
{
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/');
}
$xmlWriter = IOFactory::createWriter($phpWord, $writerName);
$xmlWriter->save(self::$file);
$zip = new ZipArchive();
$res = $zip->open(self::$file);
if (true === $res) {
$zip->extractTo(Settings::getTempDir() . '/PhpWord_Unit_Test/');
$zip->close();
}
$doc = new XmlDocument(Settings::getTempDir() . '/PhpWord_Unit_Test/');
if ($writerName === 'ODText') {
$doc->setDefaultFile('content.xml');
}
return $doc;
}
/**
* Clear document.
*/
public static function clear(): void
{
if (self::$file && file_exists(self::$file)) {
unlink(self::$file);
self::$file = '';
}
if (is_dir(Settings::getTempDir() . '/PhpWord_Unit_Test/')) {
self::deleteDir(Settings::getTempDir() . '/PhpWord_Unit_Test/');
}
}
/**
* Delete directory.
*
* @param string $dir
*/
public static function deleteDir($dir): void
{
foreach (scandir($dir) as $file) {
if ('.' === $file || '..' === $file) {
continue;
} elseif (is_file($dir . '/' . $file)) {
unlink($dir . '/' . $file);
} elseif (is_dir($dir . '/' . $file)) {
self::deleteDir($dir . '/' . $file);
}
}
@rmdir($dir);
}
/**
* Get file.
*
* @return string
*/
public static function getFile()
{
return self::$file;
}
}