From 075ca157d750430972dd69ce9d10137c11b9e4b9 Mon Sep 17 00:00:00 2001 From: Progi1984 Date: Mon, 24 Feb 2014 19:17:06 +0100 Subject: [PATCH] ADDED : Basic unit tests --- .travis.yml | 4 +- .../PHPWord}/AutoloaderTest.php | 6 ++ Tests/PHPWord/IOFactoryTest.php | 59 ++++++++++++++++++ Tests/PHPWord/MediaTest.php | 29 +++++++++ Tests/PHPWord/SectionTest.php | 38 +++++++++++ Tests/PHPWord/Writer/Word2007/BaseTest.php | 45 +++++++++++++ .../Tests => Tests}/_files/images/earth.jpg | Bin .../Tests => Tests}/_files/images/mars.jpg | Bin .../_inc/TestHelperDOCX.php | 29 ++++++--- Tests/bootstrap.php | 14 +++++ phpunit.xml.dist => Tests/phpunit.xml.dist | 11 +++- test/PHPWord/Tests/ImageTest.php | 36 ----------- test/bootstrap.php | 8 --- 13 files changed, 222 insertions(+), 57 deletions(-) rename {test/PHPWord/Tests => Tests/PHPWord}/AutoloaderTest.php (72%) create mode 100644 Tests/PHPWord/IOFactoryTest.php create mode 100644 Tests/PHPWord/MediaTest.php create mode 100644 Tests/PHPWord/SectionTest.php create mode 100644 Tests/PHPWord/Writer/Word2007/BaseTest.php rename {test/PHPWord/Tests => Tests}/_files/images/earth.jpg (100%) rename {test/PHPWord/Tests => Tests}/_files/images/mars.jpg (100%) rename test/PHPWord/TestHelper.php => Tests/_inc/TestHelperDOCX.php (73%) create mode 100755 Tests/bootstrap.php rename phpunit.xml.dist => Tests/phpunit.xml.dist (58%) delete mode 100755 test/PHPWord/Tests/ImageTest.php delete mode 100755 test/bootstrap.php diff --git a/.travis.yml b/.travis.yml index 155a28e6..fcb6d94e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -36,9 +36,11 @@ script: ## PHP Copy/Paste Detector - php phpcpd.phar --verbose Classes/ ## PHP Mess Detector - - phpmd Classes/ text codesize,unusedcode,naming,design + - phpmd Classes/ text unusedcode,naming,design ## PHPLOC - php phploc.phar Classes/ + ## PHPUnit + - phpunit -c ./Tests/ --coverage-text notifications: email: diff --git a/test/PHPWord/Tests/AutoloaderTest.php b/Tests/PHPWord/AutoloaderTest.php similarity index 72% rename from test/PHPWord/Tests/AutoloaderTest.php rename to Tests/PHPWord/AutoloaderTest.php index cdfcf68e..33872466 100644 --- a/test/PHPWord/Tests/AutoloaderTest.php +++ b/Tests/PHPWord/AutoloaderTest.php @@ -6,6 +6,12 @@ use PHPWord_Autoloader; class AutoloaderTest extends PHPUnit_Framework_TestCase { + public function testRegister() + { + PHPWord_Autoloader::register(); + $this->assertContains(array('PHPWord_Autoloader', 'load'), spl_autoload_functions()); + } + public function testAutoload() { $this->assertNull(PHPWord_Autoloader::load('Foo'), 'PHPWord_Autoloader::load() is trying to load classes outside of the PHPWord namespace'); diff --git a/Tests/PHPWord/IOFactoryTest.php b/Tests/PHPWord/IOFactoryTest.php new file mode 100644 index 00000000..f6f0aba8 --- /dev/null +++ b/Tests/PHPWord/IOFactoryTest.php @@ -0,0 +1,59 @@ +assertAttributeEquals(PHPWord_IOFactory::getSearchLocations(), '_searchLocations','PHPWord_IOFactory'); + } + + public function testSetSearchLocationsWithArray() + { + PHPWord_IOFactory::setSearchLocations(array()); + $this->assertAttributeEquals(array(), '_searchLocations','PHPWord_IOFactory'); + } + + /** + * @expectedException Exception + * @expectedExceptionMessage Invalid parameter passed. + */ + public function testSetSearchLocationsWithNotArray() + { + PHPWord_IOFactory::setSearchLocations('String'); + } + + public function testAddSearchLocation() + { + PHPWord_IOFactory::setSearchLocations(array()); + PHPWord_IOFactory::addSearchLocation('type', 'location', 'classname'); + $this->assertAttributeEquals(array(array('type' => 'type', 'path' => 'location', 'class' => 'classname')), '_searchLocations','PHPWord_IOFactory'); + } + + /** + * @expectedException Exception + * @expectedExceptionMessage No IWriter found for type + */ + public function testCreateWriterException(){ + $oPHPWord = new PHPWord(); + + PHPWord_IOFactory::setSearchLocations(array()); + PHPWord_IOFactory::createWriter($oPHPWord); + } + + public function testCreateWriter(){ + $oPHPWord = new PHPWord(); + + $this->assertEquals(PHPWord_IOFactory::createWriter($oPHPWord, 'Word2007'), new PHPWord_Writer_Word2007($oPHPWord)); + } +} + \ No newline at end of file diff --git a/Tests/PHPWord/MediaTest.php b/Tests/PHPWord/MediaTest.php new file mode 100644 index 00000000..40460dea --- /dev/null +++ b/Tests/PHPWord/MediaTest.php @@ -0,0 +1,29 @@ +assertEquals(PHPWord_Media::getSectionMediaElements(), array()); + } + + public function testCountSectionMediaElementsWithNull() + { + $this->assertEquals(PHPWord_Media::countSectionMediaElements(), 0); + } + + public function testGetHeaderMediaElements() + { + $this->assertAttributeEquals(PHPWord_Media::getHeaderMediaElements(), '_headerMedia','PHPWord_Media'); + } + + public function testGetFooterMediaElements() + { + $this->assertAttributeEquals(PHPWord_Media::getFooterMediaElements(), '_footerMedia','PHPWord_Media'); + } +} + \ No newline at end of file diff --git a/Tests/PHPWord/SectionTest.php b/Tests/PHPWord/SectionTest.php new file mode 100644 index 00000000..0e90961d --- /dev/null +++ b/Tests/PHPWord/SectionTest.php @@ -0,0 +1,38 @@ +assertAttributeEquals($oSection->getSettings(), '_settings', new PHPWord_Section(0)); + } + + public function testGetElementss() + { + $oSection = new PHPWord_Section(0); + $this->assertAttributeEquals($oSection->getElements(), '_elementCollection',new PHPWord_Section(0)); + } + + public function testGetFooter() + { + $oSection = new PHPWord_Section(0); + $this->assertAttributeEquals($oSection->getFooter(), '_footer',new PHPWord_Section(0)); + } + + public function testGetHeaders() + { + $oSection = new PHPWord_Section(0); + $this->assertAttributeEquals($oSection->getHeaders(), '_headers',new PHPWord_Section(0)); + } + + public function testGetElements() + { + $oSection = new PHPWord_Section(0); + $this->assertAttributeEquals($oSection->getElements(), '_elementCollection',new PHPWord_Section(0)); + } +} + \ No newline at end of file diff --git a/Tests/PHPWord/Writer/Word2007/BaseTest.php b/Tests/PHPWord/Writer/Word2007/BaseTest.php new file mode 100644 index 00000000..3627bdd5 --- /dev/null +++ b/Tests/PHPWord/Writer/Word2007/BaseTest.php @@ -0,0 +1,45 @@ +createSection(); + $section->addImage( + PHPWORD_TESTS_DIR_ROOT . '/_files/images/earth.jpg', + array( + 'marginTop' => -1, + 'marginLeft' => -1, + 'wrappingStyle' => 'behind' + ) + ); + + $doc = TestHelperDOCX::getDocument($PHPWord); + $element = $doc->getElement('/w:document/w:body/w:p/w:r/w:pict/v:shape'); + + $style = $element->getAttribute('style'); + + $this->assertRegExp('/z\-index:\-[0-9]*/', $style); + $this->assertRegExp('/position:absolute;/', $style); + } +} + \ No newline at end of file diff --git a/test/PHPWord/Tests/_files/images/earth.jpg b/Tests/_files/images/earth.jpg similarity index 100% rename from test/PHPWord/Tests/_files/images/earth.jpg rename to Tests/_files/images/earth.jpg diff --git a/test/PHPWord/Tests/_files/images/mars.jpg b/Tests/_files/images/mars.jpg similarity index 100% rename from test/PHPWord/Tests/_files/images/mars.jpg rename to Tests/_files/images/mars.jpg diff --git a/test/PHPWord/TestHelper.php b/Tests/_inc/TestHelperDOCX.php similarity index 73% rename from test/PHPWord/TestHelper.php rename to Tests/_inc/TestHelperDOCX.php index db626428..32f1af08 100644 --- a/test/PHPWord/TestHelper.php +++ b/Tests/_inc/TestHelperDOCX.php @@ -4,31 +4,42 @@ namespace PHPWord\Tests; use PHPWord; use DOMDocument; -class TestHelper +class TestHelperDOCX { + static protected $file; + /** * @param \PHPWord $PHPWord - * @return \PHPWord\Tests\Doc + * @return \PHPWord\Tests\Xml_Document */ public static function getDocument(PHPWord $PHPWord) { + self::$file = tempnam(sys_get_temp_dir(), 'PHPWord'); + if(!is_dir(sys_get_temp_dir().'/PHPWord_Unit_Test/')){ + mkdir(sys_get_temp_dir().'/PHPWord_Unit_Test/'); + } + $objWriter = \PHPWord_IOFactory::createWriter($PHPWord, 'Word2007'); - $objWriter->save(__DIR__ . '/Tests/_files/test.docx'); + $objWriter->save(self::$file); $zip = new \ZipArchive; - $res = $zip->open(__DIR__ . '/Tests/_files/test.docx'); + $res = $zip->open(self::$file); if ($res === true) { - $zip->extractTo(__DIR__ . '/Tests/_files/test/'); + $zip->extractTo(sys_get_temp_dir().'/PHPWord_Unit_Test/'); $zip->close(); } - return new Doc(__DIR__ . '/Tests/_files/test/'); + return new Xml_Document(sys_get_temp_dir().'/PHPWord_Unit_Test/'); } public static function clear() { - unlink(__DIR__ . '/Tests/_files/test.docx'); - self::deleteDir(__DIR__ . '/Tests/_files/test/'); + if(file_exists(self::$file)){ + unlink(self::$file); + } + if(is_dir(sys_get_temp_dir().'/PHPWord_Unit_Test/')){ + self::deleteDir(sys_get_temp_dir().'/PHPWord_Unit_Test/'); + } } /** @@ -50,7 +61,7 @@ class TestHelper } } -class Doc +class Xml_Document { /** @var string $path */ private $path; diff --git a/Tests/bootstrap.php b/Tests/bootstrap.php new file mode 100755 index 00000000..554dc38f --- /dev/null +++ b/Tests/bootstrap.php @@ -0,0 +1,14 @@ + - - ./test/PHPWord/ + + ./PHPWord/ + + + ../Classes + + \ No newline at end of file diff --git a/test/PHPWord/Tests/ImageTest.php b/test/PHPWord/Tests/ImageTest.php deleted file mode 100755 index 81b24677..00000000 --- a/test/PHPWord/Tests/ImageTest.php +++ /dev/null @@ -1,36 +0,0 @@ -createSection(12240, 15840, 0, 0, 0, 0); - - $section->addImage( - __DIR__ . '/_files/images/earth.jpg', - array( - 'marginTop' => -1, - 'marginLeft' => -1, - 'wrappingStyle' => 'behind' - ) - ); - - $doc = TestHelper::getDocument($PHPWord); - $element = $doc->getElement('/w:document/w:body/w:p/w:r/w:pict/v:shape'); - - $style = $element->getAttribute('style'); - - $this->assertRegExp('/z\-index:\-[0-9]*/', $style); - $this->assertRegExp('/position:absolute;/', $style); - } -} \ No newline at end of file diff --git a/test/bootstrap.php b/test/bootstrap.php deleted file mode 100755 index b03b68ce..00000000 --- a/test/bootstrap.php +++ /dev/null @@ -1,8 +0,0 @@ -