ADDED : Unit Tests
This commit is contained in:
parent
a87953a46c
commit
9646fd3f3e
@ -38,26 +38,8 @@ class PHPWord_Shared_File
|
||||
*/
|
||||
public static function file_exists($pFilename)
|
||||
{
|
||||
// Sick construction, but it seems that
|
||||
// file_exists returns strange values when
|
||||
// doing the original file_exists on ZIP archives...
|
||||
if (strtolower(substr($pFilename, 0, 3)) == 'zip') {
|
||||
// Open ZIP file and verify if the file exists
|
||||
$zipFile = substr($pFilename, 6, strpos($pFilename, '#') - 6);
|
||||
$archiveFile = substr($pFilename, strpos($pFilename, '#') + 1);
|
||||
|
||||
$zip = new ZipArchive();
|
||||
if ($zip->open($zipFile) === true) {
|
||||
$returnValue = ($zip->getFromName($archiveFile) !== false);
|
||||
$zip->close();
|
||||
return $returnValue;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
// Regular file_exists
|
||||
return file_exists($pFilename);
|
||||
}
|
||||
// Regular file_exists
|
||||
return file_exists($pFilename);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -37,20 +37,6 @@ class PHPWord_Shared_String
|
||||
*/
|
||||
private static $_controlCharacters = array();
|
||||
|
||||
/**
|
||||
* Is mbstring extension avalable?
|
||||
*
|
||||
* @var boolean
|
||||
*/
|
||||
private static $_isMbstringEnabled;
|
||||
|
||||
/**
|
||||
* Is iconv extension avalable?
|
||||
*
|
||||
* @var boolean
|
||||
*/
|
||||
private static $_isIconvEnabled;
|
||||
|
||||
/**
|
||||
* Build control characters array
|
||||
*/
|
||||
@ -65,40 +51,6 @@ class PHPWord_Shared_String
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get whether mbstring extension is available
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public static function getIsMbstringEnabled()
|
||||
{
|
||||
if (isset(self::$_isMbstringEnabled)) {
|
||||
return self::$_isMbstringEnabled;
|
||||
}
|
||||
|
||||
self::$_isMbstringEnabled = function_exists('mb_convert_encoding') ?
|
||||
true : false;
|
||||
|
||||
return self::$_isMbstringEnabled;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get whether iconv extension is available
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public static function getIsIconvEnabled()
|
||||
{
|
||||
if (isset(self::$_isIconvEnabled)) {
|
||||
return self::$_isIconvEnabled;
|
||||
}
|
||||
|
||||
self::$_isIconvEnabled = function_exists('iconv') ?
|
||||
true : false;
|
||||
|
||||
return self::$_isIconvEnabled;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert from OpenXML escaped control character to PHP control character
|
||||
*
|
||||
@ -155,115 +107,4 @@ class PHPWord_Shared_String
|
||||
{
|
||||
return $value === '' || preg_match('/^./su', $value) === 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Formats a numeric value as a string for output in various output writers
|
||||
*
|
||||
* @param mixed $value
|
||||
* @return string
|
||||
*/
|
||||
public static function FormatNumber($value)
|
||||
{
|
||||
return number_format($value, 2, '.', '');
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a UTF-8 string into BIFF8 Unicode string data (8-bit string length)
|
||||
* Writes the string using uncompressed notation, no rich text, no Asian phonetics
|
||||
* If mbstring extension is not available, ASCII is assumed, and compressed notation is used
|
||||
* although this will give wrong results for non-ASCII strings
|
||||
* see OpenOffice.org's Documentation of the Microsoft Excel File Format, sect. 2.5.3
|
||||
*
|
||||
* @param string $value UTF-8 encoded string
|
||||
* @return string
|
||||
*/
|
||||
public static function UTF8toBIFF8UnicodeShort($value)
|
||||
{
|
||||
// character count
|
||||
$ln = self::CountCharacters($value, 'UTF-8');
|
||||
|
||||
// option flags
|
||||
$opt = (self::getIsMbstringEnabled() || self::getIsIconvEnabled()) ?
|
||||
0x0001 : 0x0000;
|
||||
|
||||
// characters
|
||||
$chars = self::ConvertEncoding($value, 'UTF-16LE', 'UTF-8');
|
||||
|
||||
$data = pack('CC', $ln, $opt) . $chars;
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a UTF-8 string into BIFF8 Unicode string data (16-bit string length)
|
||||
* Writes the string using uncompressed notation, no rich text, no Asian phonetics
|
||||
* If mbstring extension is not available, ASCII is assumed, and compressed notation is used
|
||||
* although this will give wrong results for non-ASCII strings
|
||||
* see OpenOffice.org's Documentation of the Microsoft Excel File Format, sect. 2.5.3
|
||||
*
|
||||
* @param string $value UTF-8 encoded string
|
||||
* @return string
|
||||
*/
|
||||
public static function UTF8toBIFF8UnicodeLong($value)
|
||||
{
|
||||
// character count
|
||||
$ln = self::CountCharacters($value, 'UTF-8');
|
||||
|
||||
// option flags
|
||||
$opt = (self::getIsMbstringEnabled() || self::getIsIconvEnabled()) ?
|
||||
0x0001 : 0x0000;
|
||||
|
||||
// characters
|
||||
$chars = self::ConvertEncoding($value, 'UTF-16LE', 'UTF-8');
|
||||
|
||||
$data = pack('vC', $ln, $opt) . $chars;
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert string from one encoding to another. First try mbstring, then iconv, or no convertion
|
||||
*
|
||||
* @param string $value
|
||||
* @param string $to Encoding to convert to, e.g. 'UTF-8'
|
||||
* @param string $from Encoding to convert from, e.g. 'UTF-16LE'
|
||||
* @return string
|
||||
*/
|
||||
public static function ConvertEncoding($value, $to, $from)
|
||||
{
|
||||
if (self::getIsMbstringEnabled()) {
|
||||
$value = mb_convert_encoding($value, $to, $from);
|
||||
return $value;
|
||||
}
|
||||
|
||||
if (self::getIsIconvEnabled()) {
|
||||
$value = iconv($from, $to, $value);
|
||||
return $value;
|
||||
}
|
||||
|
||||
// else, no conversion
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get character count. First try mbstring, then iconv, finally strlen
|
||||
*
|
||||
* @param string $value
|
||||
* @param string $enc Encoding
|
||||
* @return int Character count
|
||||
*/
|
||||
public static function CountCharacters($value, $enc = 'UTF-8')
|
||||
{
|
||||
if (self::getIsMbstringEnabled()) {
|
||||
$count = mb_strlen($value, $enc);
|
||||
return $count;
|
||||
}
|
||||
|
||||
if (self::getIsIconvEnabled()) {
|
||||
$count = iconv_strlen($value, $enc);
|
||||
return $count;
|
||||
}
|
||||
|
||||
// else strlen
|
||||
$count = strlen($value);
|
||||
return $count;
|
||||
}
|
||||
}
|
||||
|
||||
169
Tests/PHPWord/DocumentPropertiesTest.php
Normal file
169
Tests/PHPWord/DocumentPropertiesTest.php
Normal file
@ -0,0 +1,169 @@
|
||||
<?php
|
||||
namespace PHPWord\Tests;
|
||||
|
||||
use PHPWord_DocumentProperties;
|
||||
|
||||
class DocumentPropertiesTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function testCreator()
|
||||
{
|
||||
$oProperties = new PHPWord_DocumentProperties();
|
||||
$oProperties->setCreator();
|
||||
$this->assertEquals('', $oProperties->getCreator());
|
||||
|
||||
$oProperties->setCreator('AAA');
|
||||
$this->assertEquals('AAA', $oProperties->getCreator());
|
||||
}
|
||||
|
||||
public function testLastModifiedBy()
|
||||
{
|
||||
$oProperties = new PHPWord_DocumentProperties();
|
||||
$oProperties->setLastModifiedBy();
|
||||
$this->assertEquals('', $oProperties->getLastModifiedBy());
|
||||
|
||||
$oProperties->setLastModifiedBy('AAA');
|
||||
$this->assertEquals('AAA', $oProperties->getLastModifiedBy());
|
||||
}
|
||||
|
||||
public function testCreated()
|
||||
{
|
||||
$oProperties = new PHPWord_DocumentProperties();
|
||||
$oProperties->setCreated();
|
||||
$this->assertEquals(time(), $oProperties->getCreated());
|
||||
|
||||
$iTime = time() + 3600;
|
||||
$oProperties->setCreated($iTime);
|
||||
$this->assertEquals($iTime, $oProperties->getCreated());
|
||||
}
|
||||
|
||||
public function testModified()
|
||||
{
|
||||
$oProperties = new PHPWord_DocumentProperties();
|
||||
$oProperties->setModified();
|
||||
$this->assertEquals(time(), $oProperties->getModified());
|
||||
|
||||
$iTime = time() + 3600;
|
||||
$oProperties->setModified($iTime);
|
||||
$this->assertEquals($iTime, $oProperties->getModified());
|
||||
}
|
||||
|
||||
public function testTitle()
|
||||
{
|
||||
$oProperties = new PHPWord_DocumentProperties();
|
||||
$oProperties->setTitle();
|
||||
$this->assertEquals('', $oProperties->getTitle());
|
||||
|
||||
$oProperties->setTitle('AAA');
|
||||
$this->assertEquals('AAA', $oProperties->getTitle());
|
||||
}
|
||||
|
||||
public function testDescription()
|
||||
{
|
||||
$oProperties = new PHPWord_DocumentProperties();
|
||||
$oProperties->setDescription();
|
||||
$this->assertEquals('', $oProperties->getDescription());
|
||||
|
||||
$oProperties->setDescription('AAA');
|
||||
$this->assertEquals('AAA', $oProperties->getDescription());
|
||||
}
|
||||
|
||||
public function testSubject()
|
||||
{
|
||||
$oProperties = new PHPWord_DocumentProperties();
|
||||
$oProperties->setSubject();
|
||||
$this->assertEquals('', $oProperties->getSubject());
|
||||
|
||||
$oProperties->setSubject('AAA');
|
||||
$this->assertEquals('AAA', $oProperties->getSubject());
|
||||
}
|
||||
|
||||
public function testKeywords()
|
||||
{
|
||||
$oProperties = new PHPWord_DocumentProperties();
|
||||
$oProperties->setKeywords();
|
||||
$this->assertEquals('', $oProperties->getKeywords());
|
||||
|
||||
$oProperties->setKeywords('AAA');
|
||||
$this->assertEquals('AAA', $oProperties->getKeywords());
|
||||
}
|
||||
|
||||
public function testCategory()
|
||||
{
|
||||
$oProperties = new PHPWord_DocumentProperties();
|
||||
$oProperties->setCategory();
|
||||
$this->assertEquals('', $oProperties->getCategory());
|
||||
|
||||
$oProperties->setCategory('AAA');
|
||||
$this->assertEquals('AAA', $oProperties->getCategory());
|
||||
}
|
||||
|
||||
public function testCompany()
|
||||
{
|
||||
$oProperties = new PHPWord_DocumentProperties();
|
||||
$oProperties->setCompany();
|
||||
$this->assertEquals('', $oProperties->getCompany());
|
||||
|
||||
$oProperties->setCompany('AAA');
|
||||
$this->assertEquals('AAA', $oProperties->getCompany());
|
||||
}
|
||||
|
||||
public function testManager()
|
||||
{
|
||||
$oProperties = new PHPWord_DocumentProperties();
|
||||
$oProperties->setManager();
|
||||
$this->assertEquals('', $oProperties->getManager());
|
||||
|
||||
$oProperties->setManager('AAA');
|
||||
$this->assertEquals('AAA', $oProperties->getManager());
|
||||
}
|
||||
|
||||
public function testCustomProperty()
|
||||
{
|
||||
$oProperties = new PHPWord_DocumentProperties();
|
||||
$oProperties->setCustomProperty('key1', null);
|
||||
$oProperties->setCustomProperty('key2', true);
|
||||
$oProperties->setCustomProperty('key3', 3);
|
||||
$oProperties->setCustomProperty('key4', 4.4);
|
||||
$oProperties->setCustomProperty('key5', 'value5');
|
||||
$this->assertEquals(PHPWord_DocumentProperties::PROPERTY_TYPE_STRING, $oProperties->getCustomPropertyType('key1'));
|
||||
$this->assertEquals(PHPWord_DocumentProperties::PROPERTY_TYPE_BOOLEAN, $oProperties->getCustomPropertyType('key2'));
|
||||
$this->assertEquals(PHPWord_DocumentProperties::PROPERTY_TYPE_INTEGER, $oProperties->getCustomPropertyType('key3'));
|
||||
$this->assertEquals(PHPWord_DocumentProperties::PROPERTY_TYPE_FLOAT, $oProperties->getCustomPropertyType('key4'));
|
||||
$this->assertEquals(PHPWord_DocumentProperties::PROPERTY_TYPE_STRING, $oProperties->getCustomPropertyType('key5'));
|
||||
$this->assertEquals(null, $oProperties->getCustomPropertyType('key6'));
|
||||
$this->assertEquals(null, $oProperties->getCustomPropertyValue('key1'));
|
||||
$this->assertEquals(true, $oProperties->getCustomPropertyValue('key2'));
|
||||
$this->assertEquals(3, $oProperties->getCustomPropertyValue('key3'));
|
||||
$this->assertEquals(4.4, $oProperties->getCustomPropertyValue('key4'));
|
||||
$this->assertEquals('value5', $oProperties->getCustomPropertyValue('key5'));
|
||||
$this->assertEquals(null, $oProperties->getCustomPropertyValue('key6'));
|
||||
$this->assertEquals(true, $oProperties->isCustomPropertySet('key5'));
|
||||
$this->assertEquals(false, $oProperties->isCustomPropertySet('key6'));
|
||||
$this->assertEquals(array('key1', 'key2', 'key3', 'key4', 'key5'), $oProperties->getCustomProperties());
|
||||
}
|
||||
|
||||
public function testConvertProperty()
|
||||
{
|
||||
$this->assertEquals('', PHPWord_DocumentProperties::convertProperty('a', 'empty'));
|
||||
$this->assertEquals(null, PHPWord_DocumentProperties::convertProperty('a', 'null'));
|
||||
$this->assertEquals(8, PHPWord_DocumentProperties::convertProperty('8', 'int'));
|
||||
$this->assertEquals(8, PHPWord_DocumentProperties::convertProperty('8.3', 'uint'));
|
||||
$this->assertEquals(8.3, PHPWord_DocumentProperties::convertProperty('8.3', 'decimal'));
|
||||
$this->assertEquals('8.3', PHPWord_DocumentProperties::convertProperty('8.3', 'lpstr'));
|
||||
$this->assertEquals(strtotime('10/11/2013'), PHPWord_DocumentProperties::convertProperty('10/11/2013', 'date'));
|
||||
$this->assertEquals(true, PHPWord_DocumentProperties::convertProperty('true', 'bool'));
|
||||
$this->assertEquals(false, PHPWord_DocumentProperties::convertProperty('1', 'bool'));
|
||||
$this->assertEquals('1', PHPWord_DocumentProperties::convertProperty('1', 'array'));
|
||||
$this->assertEquals('1', PHPWord_DocumentProperties::convertProperty('1', ''));
|
||||
|
||||
|
||||
$this->assertEquals(PHPWord_DocumentProperties::PROPERTY_TYPE_INTEGER, PHPWord_DocumentProperties::convertPropertyType('int'));
|
||||
$this->assertEquals(PHPWord_DocumentProperties::PROPERTY_TYPE_INTEGER, PHPWord_DocumentProperties::convertPropertyType('uint'));
|
||||
$this->assertEquals(PHPWord_DocumentProperties::PROPERTY_TYPE_FLOAT, PHPWord_DocumentProperties::convertPropertyType('decimal'));
|
||||
$this->assertEquals(PHPWord_DocumentProperties::PROPERTY_TYPE_STRING, PHPWord_DocumentProperties::convertPropertyType('lpstr'));
|
||||
$this->assertEquals(PHPWord_DocumentProperties::PROPERTY_TYPE_DATE, PHPWord_DocumentProperties::convertPropertyType('date'));
|
||||
$this->assertEquals(PHPWord_DocumentProperties::PROPERTY_TYPE_BOOLEAN, PHPWord_DocumentProperties::convertPropertyType('bool'));
|
||||
$this->assertEquals(PHPWord_DocumentProperties::PROPERTY_TYPE_UNKNOWN, PHPWord_DocumentProperties::convertPropertyType('array'));
|
||||
$this->assertEquals(PHPWord_DocumentProperties::PROPERTY_TYPE_UNKNOWN, PHPWord_DocumentProperties::convertPropertyType(''));
|
||||
}
|
||||
}
|
||||
@ -16,12 +16,24 @@ class FileTest extends \PHPUnit_Framework_TestCase
|
||||
*/
|
||||
public function testFileExists()
|
||||
{
|
||||
$dir = join(
|
||||
DIRECTORY_SEPARATOR,
|
||||
array(PHPWORD_TESTS_DIR_ROOT, '_files', 'templates')
|
||||
);
|
||||
chdir($dir);
|
||||
$this->assertTrue(PHPWord_Shared_File::file_exists('blank.docx'));
|
||||
$dir = join(
|
||||
DIRECTORY_SEPARATOR,
|
||||
array(PHPWORD_TESTS_DIR_ROOT, '_files', 'templates')
|
||||
);
|
||||
chdir($dir);
|
||||
$this->assertTrue(PHPWord_Shared_File::file_exists('blank.docx'));
|
||||
}
|
||||
/**
|
||||
* Test file_exists()
|
||||
*/
|
||||
public function testNoFileExists()
|
||||
{
|
||||
$dir = join(
|
||||
DIRECTORY_SEPARATOR,
|
||||
array(PHPWORD_TESTS_DIR_ROOT, '_files', 'templates')
|
||||
);
|
||||
chdir($dir);
|
||||
$this->assertFalse(PHPWord_Shared_File::file_exists('404.docx'));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -29,13 +41,13 @@ class FileTest extends \PHPUnit_Framework_TestCase
|
||||
*/
|
||||
public function testRealpath()
|
||||
{
|
||||
$dir = join(
|
||||
DIRECTORY_SEPARATOR,
|
||||
array(PHPWORD_TESTS_DIR_ROOT, '_files', 'templates')
|
||||
);
|
||||
chdir($dir);
|
||||
$file = 'blank.docx';
|
||||
$expected = $dir . DIRECTORY_SEPARATOR . $file;
|
||||
$this->assertEquals($expected, PHPWord_Shared_File::realpath($file));
|
||||
$dir = join(
|
||||
DIRECTORY_SEPARATOR,
|
||||
array(PHPWORD_TESTS_DIR_ROOT, '_files', 'templates')
|
||||
);
|
||||
chdir($dir);
|
||||
$file = 'blank.docx';
|
||||
$expected = $dir . DIRECTORY_SEPARATOR . $file;
|
||||
$this->assertEquals($expected, PHPWord_Shared_File::realpath($file));
|
||||
}
|
||||
}
|
||||
|
||||
@ -11,32 +11,22 @@ use PHPWord_Shared_String;
|
||||
*/
|
||||
class StringTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* Test getIsMbstringEnabled() and getIsIconvEnabled()
|
||||
*/
|
||||
public function testGetIsMbstringAndIconvEnabled()
|
||||
public function testIsUTF8()
|
||||
{
|
||||
$features = array(
|
||||
'mbstring' => 'mb_convert_encoding',
|
||||
'iconv' => 'iconv',
|
||||
);
|
||||
foreach ($features as $key => $val) {
|
||||
$expected = function_exists($val);
|
||||
$get = "getIs{$key}Enabled";
|
||||
$firstResult = PHPWord_Shared_String::$get();
|
||||
$this->assertEquals($expected, $firstResult);
|
||||
$secondResult = PHPWord_Shared_String::$get();
|
||||
$this->assertEquals($firstResult, $secondResult);
|
||||
}
|
||||
$this->assertTrue(PHPWord_Shared_String::IsUTF8(''));
|
||||
$this->assertTrue(PHPWord_Shared_String::IsUTF8('éééé'));
|
||||
$this->assertFalse(PHPWord_Shared_String::IsUTF8(utf8_decode('éééé')));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test FormatNumber()
|
||||
*/
|
||||
public function testFormatNumber()
|
||||
{
|
||||
$expected = '1022.12';
|
||||
$returned = PHPWord_Shared_String::FormatNumber('1022.1234');
|
||||
$this->assertEquals($expected, $returned);
|
||||
}
|
||||
public function testControlCharacterOOXML2PHP()
|
||||
{
|
||||
$this->assertEquals('', PHPWord_Shared_String::ControlCharacterOOXML2PHP(''));
|
||||
$this->assertEquals(chr(0x08), PHPWord_Shared_String::ControlCharacterOOXML2PHP('_x0008_'));
|
||||
}
|
||||
|
||||
public function testControlCharacterPHP2OOXML()
|
||||
{
|
||||
$this->assertEquals('', PHPWord_Shared_String::ControlCharacterPHP2OOXML(''));
|
||||
$this->assertEquals('_x0008_', PHPWord_Shared_String::ControlCharacterPHP2OOXML(chr(0x08)));
|
||||
}
|
||||
}
|
||||
|
||||
BIN
Tests/_files/documents/reader.docx.zip
Normal file
BIN
Tests/_files/documents/reader.docx.zip
Normal file
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user