Skip to content

Commit 45a9283

Browse files
committed
Merge pull request #100 from ivanlanin/develop
Some unit tests for Shared Drawing, File, and String
2 parents 8fb7da4 + e36b2c5 commit 45a9283

File tree

7 files changed

+160
-5
lines changed

7 files changed

+160
-5
lines changed

Classes/PHPWord/Shared/XMLWriter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class PHPWord_Shared_XMLWriter
5757
private $_tempFileName = '';
5858

5959
/**
60-
* Create a new PHPPowerPoint_Shared_XMLWriter instance
60+
* Create a new PHPWord_Shared_XMLWriter instance
6161
*
6262
* @param int $pTemporaryStorage Temporary storage location
6363
* @param string $pTemporaryStorageFolder Temporary storage folder

Classes/PHPWord/Writer/ODText.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ public function getPHPWord()
212212
*
213213
* @param PHPWord $pPHPWord PHPWord object
214214
* @throws Exception
215-
* @return PHPWord_Writer_PowerPoint2007
215+
* @return PHPWord_Writer_ODText
216216
*/
217217
public function setPHPWord(PHPWord $pPHPWord = null)
218218
{
@@ -261,7 +261,7 @@ public function getUseDiskCaching()
261261
* @param boolean $pValue
262262
* @param string $pDirectory Disk caching directory
263263
* @throws Exception Exception when directory does not exist
264-
* @return PHPWord_Writer_PowerPoint2007
264+
* @return PHPWord_Writer_ODText
265265
*/
266266
public function setUseDiskCaching($pValue = false, $pDirectory = null)
267267
{

Classes/PHPWord/Writer/RTF.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public function getPHPWord()
117117
*
118118
* @param PHPWord $pPHPWord PHPWord object
119119
* @throws Exception
120-
* @return PHPWord_Writer_PowerPoint2007
120+
* @return PHPWord_Writer_RTF
121121
*/
122122
public function setPHPWord(PHPWord $pPHPWord = null)
123123
{

Classes/PHPWord/Writer/Word2007/ContentTypes.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ private function _writeDefaultContentType(PHPWord_Shared_XMLWriter $objWriter =
172172
/**
173173
* Write Override content type
174174
*
175-
* @param PHPPowerPoint_Shared_XMLWriter $objWriter XML Writer
175+
* @param PHPWord_Shared_XMLWriter $objWriter XML Writer
176176
* @param string $pPartname Part name
177177
* @param string $pContentType Content type
178178
* @throws Exception

Tests/PHPWord/Shared/DrawingTest.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
namespace PHPWord\Tests\Shared;
3+
4+
use PHPUnit_Framework_TestCase;
5+
use PHPWord_Shared_Drawing;
6+
7+
/**
8+
* Class DrawingTest
9+
*
10+
* @package PHPWord\Tests
11+
* @runTestsInSeparateProcesses
12+
*/
13+
class DrawingTest extends \PHPUnit_Framework_TestCase
14+
{
15+
16+
/**
17+
* Test unit conversion functions with various numbers
18+
*/
19+
public function testUnitConversions()
20+
{
21+
$values[] = 0; // zero value
22+
$values[] = rand(1, 100) / 100; // fraction number
23+
$values[] = rand(1, 100); // integer
24+
25+
foreach ($values as $value) {
26+
$result = PHPWord_Shared_Drawing::pixelsToEMU($value);
27+
$this->assertEquals(round($value * 9525), $result);
28+
29+
$result = PHPWord_Shared_Drawing::EMUToPixels($value);
30+
$this->assertEquals(round($value / 9525), $result);
31+
32+
$result = PHPWord_Shared_Drawing::pixelsToPoints($value);
33+
$this->assertEquals($value * 0.67777777, $result);
34+
35+
$result = PHPWord_Shared_Drawing::pointsToPixels($value);
36+
$this->assertEquals($value * 1.333333333, $result);
37+
38+
$result = PHPWord_Shared_Drawing::degreesToAngle($value);
39+
$this->assertEquals((int)round($value * 60000), $result);
40+
41+
$result = PHPWord_Shared_Drawing::angleToDegrees($value);
42+
$this->assertEquals(round($value / 60000), $result);
43+
44+
$result = PHPWord_Shared_Drawing::pixelsToCentimeters($value);
45+
$this->assertEquals($value * 0.028, $result);
46+
47+
$result = PHPWord_Shared_Drawing::centimetersToPixels($value);
48+
$this->assertEquals($value / 0.028, $result);
49+
}
50+
}
51+
52+
/**
53+
* Test htmlToRGB()
54+
*/
55+
public function testHtmlToRGB()
56+
{
57+
// Prepare test values [ original, expected ]
58+
$values[] = array('#FF99DD', array(255, 153, 221)); // With #
59+
$values[] = array('FF99DD', array(255, 153, 221)); // 6 characters
60+
$values[] = array('F9D', array(255, 153, 221)); // 3 characters
61+
$values[] = array('0F9D', false); // 4 characters
62+
// Conduct test
63+
foreach ($values as $value) {
64+
$result = PHPWord_Shared_Drawing::htmlToRGB($value[0]);
65+
$this->assertEquals($value[1], $result);
66+
}
67+
}
68+
69+
}

Tests/PHPWord/Shared/FileTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
namespace PHPWord\Tests\Shared;
3+
4+
use PHPUnit_Framework_TestCase;
5+
use PHPWord_Shared_File;
6+
7+
/**
8+
* Class FileTest
9+
*
10+
* @package PHPWord\Tests
11+
* @runTestsInSeparateProcesses
12+
*/
13+
class FileTest extends \PHPUnit_Framework_TestCase
14+
{
15+
16+
/**
17+
* Test file_exists()
18+
*/
19+
public function testFile_exists()
20+
{
21+
$dir = join(DIRECTORY_SEPARATOR,
22+
array(PHPWORD_TESTS_DIR_ROOT, '_files', 'templates')
23+
);
24+
chdir($dir);
25+
$this->assertTrue(PHPWord_Shared_File::file_exists('blank.docx'));
26+
}
27+
28+
/**
29+
* Test realpath()
30+
*/
31+
public function testRealpath()
32+
{
33+
$dir = join(DIRECTORY_SEPARATOR,
34+
array(PHPWORD_TESTS_DIR_ROOT, '_files', 'templates'));
35+
chdir($dir);
36+
$file = 'blank.docx';
37+
$expected = $dir . DIRECTORY_SEPARATOR . $file;
38+
$this->assertEquals($expected, PHPWord_Shared_File::realpath($file));
39+
}
40+
41+
}

Tests/PHPWord/Shared/StringTest.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
namespace PHPWord\Tests\Shared;
3+
4+
use PHPUnit_Framework_TestCase;
5+
use PHPWord_Shared_String;
6+
7+
/**
8+
* Class StringTest
9+
*
10+
* @package PHPWord\Tests
11+
* @runTestsInSeparateProcesses
12+
*/
13+
class StringTest extends \PHPUnit_Framework_TestCase
14+
{
15+
16+
/**
17+
* Test getIsMbstringEnabled() and getIsIconvEnabled()
18+
*/
19+
public function testGetIsMbstringAndIconvEnabled()
20+
{
21+
$features = array(
22+
'mbstring' => 'mb_convert_encoding',
23+
'iconv' => 'iconv',
24+
);
25+
foreach ($features as $key => $val) {
26+
$expected = function_exists($val);
27+
$get = "getIs{$key}Enabled";
28+
$firstResult = PHPWord_Shared_String::$get();
29+
$this->assertEquals($expected, $firstResult);
30+
$secondResult = PHPWord_Shared_String::$get();
31+
$this->assertEquals($firstResult, $secondResult);
32+
}
33+
}
34+
35+
/**
36+
* Test FormatNumber()
37+
*/
38+
public function testFormatNumber()
39+
{
40+
$expected = '1022.12';
41+
$returned = PHPWord_Shared_String::FormatNumber('1022.1234');
42+
$this->assertEquals($expected, $returned);
43+
}
44+
45+
}

0 commit comments

Comments
 (0)