ADDED : Basic unit tests

This commit is contained in:
Progi1984 2014-02-24 19:17:06 +01:00
parent 49d7897176
commit 075ca157d7
13 changed files with 222 additions and 57 deletions

View File

@ -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:

View File

@ -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');

View File

@ -0,0 +1,59 @@
<?php
namespace PHPWord\Tests;
use PHPUnit_Framework_TestCase;
use PHPWord;
use PHPWord_IOFactory;
use PHPWord_Writer_Word2007;
/**
* Class PHPWord_IOFactoryTest
* @package PHPWord\Tests
* @runTestsInSeparateProcesses
*/
class PHPWord_IOFactoryTest extends \PHPUnit_Framework_TestCase {
public function testGetSearchLocations()
{
$this->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));
}
}

View File

@ -0,0 +1,29 @@
<?php
namespace PHPWord\Tests;
use PHPUnit_Framework_TestCase;
use PHPWord_Media;
class PHPWord_MediaTest extends \PHPUnit_Framework_TestCase {
public function testGetSectionMediaElementsWithNull()
{
$this->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');
}
}

View File

@ -0,0 +1,38 @@
<?php
namespace PHPWord\Tests;
use PHPUnit_Framework_TestCase;
use PHPWord_Section;
class PHPWord_SectionTest extends \PHPUnit_Framework_TestCase {
public function testGetSettings()
{
$oSection = new PHPWord_Section(0);
$this->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));
}
}

View File

@ -0,0 +1,45 @@
<?php
namespace PHPWord\Tests;
use PHPUnit_Framework_TestCase;
use PHPWord;
use PHPWord_Writer_Word2007;
use PHPWord_Writer_Word2007_Base;
/**
* Class PHPWord_Writer_Word2007_BaseTest
* @package PHPWord\Tests
* @runTestsInSeparateProcesses
*/
class PHPWord_Writer_Word2007_BaseTest extends \PHPUnit_Framework_TestCase {
/**
* Executed before each method of the class
*/
public function tearDown()
{
TestHelperDOCX::clear();
}
public function testWriteImage_Position()
{
$PHPWord = new PHPWord();
$section = $PHPWord->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);
}
}

View File

Before

Width:  |  Height:  |  Size: 37 KiB

After

Width:  |  Height:  |  Size: 37 KiB

View File

Before

Width:  |  Height:  |  Size: 24 KiB

After

Width:  |  Height:  |  Size: 24 KiB

View File

@ -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;

14
Tests/bootstrap.php Executable file
View File

@ -0,0 +1,14 @@
<?php
date_default_timezone_set('UTC');
// Constantes
if(!defined('PHPWORD_TESTS_DIR_ROOT')){
define('PHPWORD_TESTS_DIR_ROOT', __DIR__);
}
// Includes
require_once __DIR__ . '/../Classes/PHPWord/Autoloader.php';
PHPWord_Autoloader::Register();
require_once __DIR__ . '/_inc/TestHelperDOCX.php';

View File

@ -1,6 +1,6 @@
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="test/bootstrap.php"
bootstrap="bootstrap.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
@ -9,8 +9,13 @@
stopOnFailure="false"
syntaxCheck="false">
<testsuites>
<testsuite name="PHPWord Test Suite">
<directory>./test/PHPWord/</directory>
<testsuite name="PHPWord Unit Test Suite">
<directory>./PHPWord/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">../Classes</directory>
</whitelist>
</filter>
</phpunit>

View File

@ -1,36 +0,0 @@
<?php
namespace PHPWord\Tests;
use PHPUnit_Framework_TestCase;
use PHPWord;
class ImageTest extends PHPUnit_Framework_TestCase
{
public function tearDown()
{
TestHelper::clear();
}
public function testImageWrappingStyleBehind()
{
$PHPWord = new PHPWord();
$section = $PHPWord->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);
}
}

View File

@ -1,8 +0,0 @@
<?php
date_default_timezone_set('UTC');
require_once __DIR__ . "/../Classes/PHPWord/Autoloader.php";
PHPWord_Autoloader::Register();
require_once __DIR__ . "/PHPWord/TestHelper.php";