Skip to content

Commit 075ca15

Browse files
committed
ADDED : Basic unit tests
1 parent 49d7897 commit 075ca15

File tree

13 files changed

+222
-57
lines changed

13 files changed

+222
-57
lines changed

.travis.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,11 @@ script:
3636
## PHP Copy/Paste Detector
3737
- php phpcpd.phar --verbose Classes/
3838
## PHP Mess Detector
39-
- phpmd Classes/ text codesize,unusedcode,naming,design
39+
- phpmd Classes/ text unusedcode,naming,design
4040
## PHPLOC
4141
- php phploc.phar Classes/
42+
## PHPUnit
43+
- phpunit -c ./Tests/ --coverage-text
4244

4345
notifications:
4446
email:

test/PHPWord/Tests/AutoloaderTest.php renamed to Tests/PHPWord/AutoloaderTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66

77
class AutoloaderTest extends PHPUnit_Framework_TestCase
88
{
9+
public function testRegister()
10+
{
11+
PHPWord_Autoloader::register();
12+
$this->assertContains(array('PHPWord_Autoloader', 'load'), spl_autoload_functions());
13+
}
14+
915
public function testAutoload()
1016
{
1117
$this->assertNull(PHPWord_Autoloader::load('Foo'), 'PHPWord_Autoloader::load() is trying to load classes outside of the PHPWord namespace');

Tests/PHPWord/IOFactoryTest.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
namespace PHPWord\Tests;
3+
4+
use PHPUnit_Framework_TestCase;
5+
use PHPWord;
6+
use PHPWord_IOFactory;
7+
use PHPWord_Writer_Word2007;
8+
9+
/**
10+
* Class PHPWord_IOFactoryTest
11+
* @package PHPWord\Tests
12+
* @runTestsInSeparateProcesses
13+
*/
14+
class PHPWord_IOFactoryTest extends \PHPUnit_Framework_TestCase {
15+
public function testGetSearchLocations()
16+
{
17+
$this->assertAttributeEquals(PHPWord_IOFactory::getSearchLocations(), '_searchLocations','PHPWord_IOFactory');
18+
}
19+
20+
public function testSetSearchLocationsWithArray()
21+
{
22+
PHPWord_IOFactory::setSearchLocations(array());
23+
$this->assertAttributeEquals(array(), '_searchLocations','PHPWord_IOFactory');
24+
}
25+
26+
/**
27+
* @expectedException Exception
28+
* @expectedExceptionMessage Invalid parameter passed.
29+
*/
30+
public function testSetSearchLocationsWithNotArray()
31+
{
32+
PHPWord_IOFactory::setSearchLocations('String');
33+
}
34+
35+
public function testAddSearchLocation()
36+
{
37+
PHPWord_IOFactory::setSearchLocations(array());
38+
PHPWord_IOFactory::addSearchLocation('type', 'location', 'classname');
39+
$this->assertAttributeEquals(array(array('type' => 'type', 'path' => 'location', 'class' => 'classname')), '_searchLocations','PHPWord_IOFactory');
40+
}
41+
42+
/**
43+
* @expectedException Exception
44+
* @expectedExceptionMessage No IWriter found for type
45+
*/
46+
public function testCreateWriterException(){
47+
$oPHPWord = new PHPWord();
48+
49+
PHPWord_IOFactory::setSearchLocations(array());
50+
PHPWord_IOFactory::createWriter($oPHPWord);
51+
}
52+
53+
public function testCreateWriter(){
54+
$oPHPWord = new PHPWord();
55+
56+
$this->assertEquals(PHPWord_IOFactory::createWriter($oPHPWord, 'Word2007'), new PHPWord_Writer_Word2007($oPHPWord));
57+
}
58+
}
59+

Tests/PHPWord/MediaTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
namespace PHPWord\Tests;
3+
4+
use PHPUnit_Framework_TestCase;
5+
use PHPWord_Media;
6+
7+
class PHPWord_MediaTest extends \PHPUnit_Framework_TestCase {
8+
9+
public function testGetSectionMediaElementsWithNull()
10+
{
11+
$this->assertEquals(PHPWord_Media::getSectionMediaElements(), array());
12+
}
13+
14+
public function testCountSectionMediaElementsWithNull()
15+
{
16+
$this->assertEquals(PHPWord_Media::countSectionMediaElements(), 0);
17+
}
18+
19+
public function testGetHeaderMediaElements()
20+
{
21+
$this->assertAttributeEquals(PHPWord_Media::getHeaderMediaElements(), '_headerMedia','PHPWord_Media');
22+
}
23+
24+
public function testGetFooterMediaElements()
25+
{
26+
$this->assertAttributeEquals(PHPWord_Media::getFooterMediaElements(), '_footerMedia','PHPWord_Media');
27+
}
28+
}
29+

Tests/PHPWord/SectionTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
namespace PHPWord\Tests;
3+
4+
use PHPUnit_Framework_TestCase;
5+
use PHPWord_Section;
6+
7+
class PHPWord_SectionTest extends \PHPUnit_Framework_TestCase {
8+
public function testGetSettings()
9+
{
10+
$oSection = new PHPWord_Section(0);
11+
$this->assertAttributeEquals($oSection->getSettings(), '_settings', new PHPWord_Section(0));
12+
}
13+
14+
public function testGetElementss()
15+
{
16+
$oSection = new PHPWord_Section(0);
17+
$this->assertAttributeEquals($oSection->getElements(), '_elementCollection',new PHPWord_Section(0));
18+
}
19+
20+
public function testGetFooter()
21+
{
22+
$oSection = new PHPWord_Section(0);
23+
$this->assertAttributeEquals($oSection->getFooter(), '_footer',new PHPWord_Section(0));
24+
}
25+
26+
public function testGetHeaders()
27+
{
28+
$oSection = new PHPWord_Section(0);
29+
$this->assertAttributeEquals($oSection->getHeaders(), '_headers',new PHPWord_Section(0));
30+
}
31+
32+
public function testGetElements()
33+
{
34+
$oSection = new PHPWord_Section(0);
35+
$this->assertAttributeEquals($oSection->getElements(), '_elementCollection',new PHPWord_Section(0));
36+
}
37+
}
38+
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
namespace PHPWord\Tests;
3+
4+
use PHPUnit_Framework_TestCase;
5+
use PHPWord;
6+
use PHPWord_Writer_Word2007;
7+
use PHPWord_Writer_Word2007_Base;
8+
9+
/**
10+
* Class PHPWord_Writer_Word2007_BaseTest
11+
* @package PHPWord\Tests
12+
* @runTestsInSeparateProcesses
13+
*/
14+
class PHPWord_Writer_Word2007_BaseTest extends \PHPUnit_Framework_TestCase {
15+
/**
16+
* Executed before each method of the class
17+
*/
18+
public function tearDown()
19+
{
20+
TestHelperDOCX::clear();
21+
}
22+
23+
public function testWriteImage_Position()
24+
{
25+
$PHPWord = new PHPWord();
26+
$section = $PHPWord->createSection();
27+
$section->addImage(
28+
PHPWORD_TESTS_DIR_ROOT . '/_files/images/earth.jpg',
29+
array(
30+
'marginTop' => -1,
31+
'marginLeft' => -1,
32+
'wrappingStyle' => 'behind'
33+
)
34+
);
35+
36+
$doc = TestHelperDOCX::getDocument($PHPWord);
37+
$element = $doc->getElement('/w:document/w:body/w:p/w:r/w:pict/v:shape');
38+
39+
$style = $element->getAttribute('style');
40+
41+
$this->assertRegExp('/z\-index:\-[0-9]*/', $style);
42+
$this->assertRegExp('/position:absolute;/', $style);
43+
}
44+
}
45+

test/PHPWord/TestHelper.php renamed to Tests/_inc/TestHelperDOCX.php

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,42 @@
44
use PHPWord;
55
use DOMDocument;
66

7-
class TestHelper
7+
class TestHelperDOCX
88
{
9+
static protected $file;
10+
911
/**
1012
* @param \PHPWord $PHPWord
11-
* @return \PHPWord\Tests\Doc
13+
* @return \PHPWord\Tests\Xml_Document
1214
*/
1315
public static function getDocument(PHPWord $PHPWord)
1416
{
17+
self::$file = tempnam(sys_get_temp_dir(), 'PHPWord');
18+
if(!is_dir(sys_get_temp_dir().'/PHPWord_Unit_Test/')){
19+
mkdir(sys_get_temp_dir().'/PHPWord_Unit_Test/');
20+
}
21+
1522
$objWriter = \PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
16-
$objWriter->save(__DIR__ . '/Tests/_files/test.docx');
23+
$objWriter->save(self::$file);
1724

1825
$zip = new \ZipArchive;
19-
$res = $zip->open(__DIR__ . '/Tests/_files/test.docx');
26+
$res = $zip->open(self::$file);
2027
if ($res === true) {
21-
$zip->extractTo(__DIR__ . '/Tests/_files/test/');
28+
$zip->extractTo(sys_get_temp_dir().'/PHPWord_Unit_Test/');
2229
$zip->close();
2330
}
2431

25-
return new Doc(__DIR__ . '/Tests/_files/test/');
32+
return new Xml_Document(sys_get_temp_dir().'/PHPWord_Unit_Test/');
2633
}
2734

2835
public static function clear()
2936
{
30-
unlink(__DIR__ . '/Tests/_files/test.docx');
31-
self::deleteDir(__DIR__ . '/Tests/_files/test/');
37+
if(file_exists(self::$file)){
38+
unlink(self::$file);
39+
}
40+
if(is_dir(sys_get_temp_dir().'/PHPWord_Unit_Test/')){
41+
self::deleteDir(sys_get_temp_dir().'/PHPWord_Unit_Test/');
42+
}
3243
}
3344

3445
/**
@@ -50,7 +61,7 @@ public static function deleteDir($dir)
5061
}
5162
}
5263

53-
class Doc
64+
class Xml_Document
5465
{
5566
/** @var string $path */
5667
private $path;

Tests/bootstrap.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
date_default_timezone_set('UTC');
4+
5+
// Constantes
6+
if(!defined('PHPWORD_TESTS_DIR_ROOT')){
7+
define('PHPWORD_TESTS_DIR_ROOT', __DIR__);
8+
}
9+
10+
// Includes
11+
require_once __DIR__ . '/../Classes/PHPWord/Autoloader.php';
12+
PHPWord_Autoloader::Register();
13+
14+
require_once __DIR__ . '/_inc/TestHelperDOCX.php';

0 commit comments

Comments
 (0)