Skip to content

Commit 9646fd3

Browse files
committed
ADDED : Unit Tests
1 parent a87953a commit 9646fd3

File tree

6 files changed

+212
-218
lines changed

6 files changed

+212
-218
lines changed

Classes/PHPWord/Shared/File.php

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -38,26 +38,8 @@ class PHPWord_Shared_File
3838
*/
3939
public static function file_exists($pFilename)
4040
{
41-
// Sick construction, but it seems that
42-
// file_exists returns strange values when
43-
// doing the original file_exists on ZIP archives...
44-
if (strtolower(substr($pFilename, 0, 3)) == 'zip') {
45-
// Open ZIP file and verify if the file exists
46-
$zipFile = substr($pFilename, 6, strpos($pFilename, '#') - 6);
47-
$archiveFile = substr($pFilename, strpos($pFilename, '#') + 1);
48-
49-
$zip = new ZipArchive();
50-
if ($zip->open($zipFile) === true) {
51-
$returnValue = ($zip->getFromName($archiveFile) !== false);
52-
$zip->close();
53-
return $returnValue;
54-
} else {
55-
return false;
56-
}
57-
} else {
58-
// Regular file_exists
59-
return file_exists($pFilename);
60-
}
41+
// Regular file_exists
42+
return file_exists($pFilename);
6143
}
6244

6345
/**

Classes/PHPWord/Shared/String.php

Lines changed: 0 additions & 159 deletions
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,6 @@ class PHPWord_Shared_String
3737
*/
3838
private static $_controlCharacters = array();
3939

40-
/**
41-
* Is mbstring extension avalable?
42-
*
43-
* @var boolean
44-
*/
45-
private static $_isMbstringEnabled;
46-
47-
/**
48-
* Is iconv extension avalable?
49-
*
50-
* @var boolean
51-
*/
52-
private static $_isIconvEnabled;
53-
5440
/**
5541
* Build control characters array
5642
*/
@@ -65,40 +51,6 @@ private static function _buildControlCharacters()
6551
}
6652
}
6753

68-
/**
69-
* Get whether mbstring extension is available
70-
*
71-
* @return boolean
72-
*/
73-
public static function getIsMbstringEnabled()
74-
{
75-
if (isset(self::$_isMbstringEnabled)) {
76-
return self::$_isMbstringEnabled;
77-
}
78-
79-
self::$_isMbstringEnabled = function_exists('mb_convert_encoding') ?
80-
true : false;
81-
82-
return self::$_isMbstringEnabled;
83-
}
84-
85-
/**
86-
* Get whether iconv extension is available
87-
*
88-
* @return boolean
89-
*/
90-
public static function getIsIconvEnabled()
91-
{
92-
if (isset(self::$_isIconvEnabled)) {
93-
return self::$_isIconvEnabled;
94-
}
95-
96-
self::$_isIconvEnabled = function_exists('iconv') ?
97-
true : false;
98-
99-
return self::$_isIconvEnabled;
100-
}
101-
10254
/**
10355
* Convert from OpenXML escaped control character to PHP control character
10456
*
@@ -155,115 +107,4 @@ public static function IsUTF8($value = '')
155107
{
156108
return $value === '' || preg_match('/^./su', $value) === 1;
157109
}
158-
159-
/**
160-
* Formats a numeric value as a string for output in various output writers
161-
*
162-
* @param mixed $value
163-
* @return string
164-
*/
165-
public static function FormatNumber($value)
166-
{
167-
return number_format($value, 2, '.', '');
168-
}
169-
170-
/**
171-
* Converts a UTF-8 string into BIFF8 Unicode string data (8-bit string length)
172-
* Writes the string using uncompressed notation, no rich text, no Asian phonetics
173-
* If mbstring extension is not available, ASCII is assumed, and compressed notation is used
174-
* although this will give wrong results for non-ASCII strings
175-
* see OpenOffice.org's Documentation of the Microsoft Excel File Format, sect. 2.5.3
176-
*
177-
* @param string $value UTF-8 encoded string
178-
* @return string
179-
*/
180-
public static function UTF8toBIFF8UnicodeShort($value)
181-
{
182-
// character count
183-
$ln = self::CountCharacters($value, 'UTF-8');
184-
185-
// option flags
186-
$opt = (self::getIsMbstringEnabled() || self::getIsIconvEnabled()) ?
187-
0x0001 : 0x0000;
188-
189-
// characters
190-
$chars = self::ConvertEncoding($value, 'UTF-16LE', 'UTF-8');
191-
192-
$data = pack('CC', $ln, $opt) . $chars;
193-
return $data;
194-
}
195-
196-
/**
197-
* Converts a UTF-8 string into BIFF8 Unicode string data (16-bit string length)
198-
* Writes the string using uncompressed notation, no rich text, no Asian phonetics
199-
* If mbstring extension is not available, ASCII is assumed, and compressed notation is used
200-
* although this will give wrong results for non-ASCII strings
201-
* see OpenOffice.org's Documentation of the Microsoft Excel File Format, sect. 2.5.3
202-
*
203-
* @param string $value UTF-8 encoded string
204-
* @return string
205-
*/
206-
public static function UTF8toBIFF8UnicodeLong($value)
207-
{
208-
// character count
209-
$ln = self::CountCharacters($value, 'UTF-8');
210-
211-
// option flags
212-
$opt = (self::getIsMbstringEnabled() || self::getIsIconvEnabled()) ?
213-
0x0001 : 0x0000;
214-
215-
// characters
216-
$chars = self::ConvertEncoding($value, 'UTF-16LE', 'UTF-8');
217-
218-
$data = pack('vC', $ln, $opt) . $chars;
219-
return $data;
220-
}
221-
222-
/**
223-
* Convert string from one encoding to another. First try mbstring, then iconv, or no convertion
224-
*
225-
* @param string $value
226-
* @param string $to Encoding to convert to, e.g. 'UTF-8'
227-
* @param string $from Encoding to convert from, e.g. 'UTF-16LE'
228-
* @return string
229-
*/
230-
public static function ConvertEncoding($value, $to, $from)
231-
{
232-
if (self::getIsMbstringEnabled()) {
233-
$value = mb_convert_encoding($value, $to, $from);
234-
return $value;
235-
}
236-
237-
if (self::getIsIconvEnabled()) {
238-
$value = iconv($from, $to, $value);
239-
return $value;
240-
}
241-
242-
// else, no conversion
243-
return $value;
244-
}
245-
246-
/**
247-
* Get character count. First try mbstring, then iconv, finally strlen
248-
*
249-
* @param string $value
250-
* @param string $enc Encoding
251-
* @return int Character count
252-
*/
253-
public static function CountCharacters($value, $enc = 'UTF-8')
254-
{
255-
if (self::getIsMbstringEnabled()) {
256-
$count = mb_strlen($value, $enc);
257-
return $count;
258-
}
259-
260-
if (self::getIsIconvEnabled()) {
261-
$count = iconv_strlen($value, $enc);
262-
return $count;
263-
}
264-
265-
// else strlen
266-
$count = strlen($value);
267-
return $count;
268-
}
269110
}
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
<?php
2+
namespace PHPWord\Tests;
3+
4+
use PHPWord_DocumentProperties;
5+
6+
class DocumentPropertiesTest extends \PHPUnit_Framework_TestCase
7+
{
8+
public function testCreator()
9+
{
10+
$oProperties = new PHPWord_DocumentProperties();
11+
$oProperties->setCreator();
12+
$this->assertEquals('', $oProperties->getCreator());
13+
14+
$oProperties->setCreator('AAA');
15+
$this->assertEquals('AAA', $oProperties->getCreator());
16+
}
17+
18+
public function testLastModifiedBy()
19+
{
20+
$oProperties = new PHPWord_DocumentProperties();
21+
$oProperties->setLastModifiedBy();
22+
$this->assertEquals('', $oProperties->getLastModifiedBy());
23+
24+
$oProperties->setLastModifiedBy('AAA');
25+
$this->assertEquals('AAA', $oProperties->getLastModifiedBy());
26+
}
27+
28+
public function testCreated()
29+
{
30+
$oProperties = new PHPWord_DocumentProperties();
31+
$oProperties->setCreated();
32+
$this->assertEquals(time(), $oProperties->getCreated());
33+
34+
$iTime = time() + 3600;
35+
$oProperties->setCreated($iTime);
36+
$this->assertEquals($iTime, $oProperties->getCreated());
37+
}
38+
39+
public function testModified()
40+
{
41+
$oProperties = new PHPWord_DocumentProperties();
42+
$oProperties->setModified();
43+
$this->assertEquals(time(), $oProperties->getModified());
44+
45+
$iTime = time() + 3600;
46+
$oProperties->setModified($iTime);
47+
$this->assertEquals($iTime, $oProperties->getModified());
48+
}
49+
50+
public function testTitle()
51+
{
52+
$oProperties = new PHPWord_DocumentProperties();
53+
$oProperties->setTitle();
54+
$this->assertEquals('', $oProperties->getTitle());
55+
56+
$oProperties->setTitle('AAA');
57+
$this->assertEquals('AAA', $oProperties->getTitle());
58+
}
59+
60+
public function testDescription()
61+
{
62+
$oProperties = new PHPWord_DocumentProperties();
63+
$oProperties->setDescription();
64+
$this->assertEquals('', $oProperties->getDescription());
65+
66+
$oProperties->setDescription('AAA');
67+
$this->assertEquals('AAA', $oProperties->getDescription());
68+
}
69+
70+
public function testSubject()
71+
{
72+
$oProperties = new PHPWord_DocumentProperties();
73+
$oProperties->setSubject();
74+
$this->assertEquals('', $oProperties->getSubject());
75+
76+
$oProperties->setSubject('AAA');
77+
$this->assertEquals('AAA', $oProperties->getSubject());
78+
}
79+
80+
public function testKeywords()
81+
{
82+
$oProperties = new PHPWord_DocumentProperties();
83+
$oProperties->setKeywords();
84+
$this->assertEquals('', $oProperties->getKeywords());
85+
86+
$oProperties->setKeywords('AAA');
87+
$this->assertEquals('AAA', $oProperties->getKeywords());
88+
}
89+
90+
public function testCategory()
91+
{
92+
$oProperties = new PHPWord_DocumentProperties();
93+
$oProperties->setCategory();
94+
$this->assertEquals('', $oProperties->getCategory());
95+
96+
$oProperties->setCategory('AAA');
97+
$this->assertEquals('AAA', $oProperties->getCategory());
98+
}
99+
100+
public function testCompany()
101+
{
102+
$oProperties = new PHPWord_DocumentProperties();
103+
$oProperties->setCompany();
104+
$this->assertEquals('', $oProperties->getCompany());
105+
106+
$oProperties->setCompany('AAA');
107+
$this->assertEquals('AAA', $oProperties->getCompany());
108+
}
109+
110+
public function testManager()
111+
{
112+
$oProperties = new PHPWord_DocumentProperties();
113+
$oProperties->setManager();
114+
$this->assertEquals('', $oProperties->getManager());
115+
116+
$oProperties->setManager('AAA');
117+
$this->assertEquals('AAA', $oProperties->getManager());
118+
}
119+
120+
public function testCustomProperty()
121+
{
122+
$oProperties = new PHPWord_DocumentProperties();
123+
$oProperties->setCustomProperty('key1', null);
124+
$oProperties->setCustomProperty('key2', true);
125+
$oProperties->setCustomProperty('key3', 3);
126+
$oProperties->setCustomProperty('key4', 4.4);
127+
$oProperties->setCustomProperty('key5', 'value5');
128+
$this->assertEquals(PHPWord_DocumentProperties::PROPERTY_TYPE_STRING, $oProperties->getCustomPropertyType('key1'));
129+
$this->assertEquals(PHPWord_DocumentProperties::PROPERTY_TYPE_BOOLEAN, $oProperties->getCustomPropertyType('key2'));
130+
$this->assertEquals(PHPWord_DocumentProperties::PROPERTY_TYPE_INTEGER, $oProperties->getCustomPropertyType('key3'));
131+
$this->assertEquals(PHPWord_DocumentProperties::PROPERTY_TYPE_FLOAT, $oProperties->getCustomPropertyType('key4'));
132+
$this->assertEquals(PHPWord_DocumentProperties::PROPERTY_TYPE_STRING, $oProperties->getCustomPropertyType('key5'));
133+
$this->assertEquals(null, $oProperties->getCustomPropertyType('key6'));
134+
$this->assertEquals(null, $oProperties->getCustomPropertyValue('key1'));
135+
$this->assertEquals(true, $oProperties->getCustomPropertyValue('key2'));
136+
$this->assertEquals(3, $oProperties->getCustomPropertyValue('key3'));
137+
$this->assertEquals(4.4, $oProperties->getCustomPropertyValue('key4'));
138+
$this->assertEquals('value5', $oProperties->getCustomPropertyValue('key5'));
139+
$this->assertEquals(null, $oProperties->getCustomPropertyValue('key6'));
140+
$this->assertEquals(true, $oProperties->isCustomPropertySet('key5'));
141+
$this->assertEquals(false, $oProperties->isCustomPropertySet('key6'));
142+
$this->assertEquals(array('key1', 'key2', 'key3', 'key4', 'key5'), $oProperties->getCustomProperties());
143+
}
144+
145+
public function testConvertProperty()
146+
{
147+
$this->assertEquals('', PHPWord_DocumentProperties::convertProperty('a', 'empty'));
148+
$this->assertEquals(null, PHPWord_DocumentProperties::convertProperty('a', 'null'));
149+
$this->assertEquals(8, PHPWord_DocumentProperties::convertProperty('8', 'int'));
150+
$this->assertEquals(8, PHPWord_DocumentProperties::convertProperty('8.3', 'uint'));
151+
$this->assertEquals(8.3, PHPWord_DocumentProperties::convertProperty('8.3', 'decimal'));
152+
$this->assertEquals('8.3', PHPWord_DocumentProperties::convertProperty('8.3', 'lpstr'));
153+
$this->assertEquals(strtotime('10/11/2013'), PHPWord_DocumentProperties::convertProperty('10/11/2013', 'date'));
154+
$this->assertEquals(true, PHPWord_DocumentProperties::convertProperty('true', 'bool'));
155+
$this->assertEquals(false, PHPWord_DocumentProperties::convertProperty('1', 'bool'));
156+
$this->assertEquals('1', PHPWord_DocumentProperties::convertProperty('1', 'array'));
157+
$this->assertEquals('1', PHPWord_DocumentProperties::convertProperty('1', ''));
158+
159+
160+
$this->assertEquals(PHPWord_DocumentProperties::PROPERTY_TYPE_INTEGER, PHPWord_DocumentProperties::convertPropertyType('int'));
161+
$this->assertEquals(PHPWord_DocumentProperties::PROPERTY_TYPE_INTEGER, PHPWord_DocumentProperties::convertPropertyType('uint'));
162+
$this->assertEquals(PHPWord_DocumentProperties::PROPERTY_TYPE_FLOAT, PHPWord_DocumentProperties::convertPropertyType('decimal'));
163+
$this->assertEquals(PHPWord_DocumentProperties::PROPERTY_TYPE_STRING, PHPWord_DocumentProperties::convertPropertyType('lpstr'));
164+
$this->assertEquals(PHPWord_DocumentProperties::PROPERTY_TYPE_DATE, PHPWord_DocumentProperties::convertPropertyType('date'));
165+
$this->assertEquals(PHPWord_DocumentProperties::PROPERTY_TYPE_BOOLEAN, PHPWord_DocumentProperties::convertPropertyType('bool'));
166+
$this->assertEquals(PHPWord_DocumentProperties::PROPERTY_TYPE_UNKNOWN, PHPWord_DocumentProperties::convertPropertyType('array'));
167+
$this->assertEquals(PHPWord_DocumentProperties::PROPERTY_TYPE_UNKNOWN, PHPWord_DocumentProperties::convertPropertyType(''));
168+
}
169+
}

0 commit comments

Comments
 (0)