Code formatting and some more tests

This commit is contained in:
Ivan Lanin 2014-04-12 10:23:31 +07:00
parent b40218da45
commit 47956b019c
9 changed files with 124 additions and 51 deletions

View File

@ -128,7 +128,7 @@ class Word2007 extends AbstractReader implements ReaderInterface
if ($zip->open($filename) === true) { if ($zip->open($filename) === true) {
for ($i = 0; $i < $zip->numFiles; $i++) { for ($i = 0; $i < $zip->numFiles; $i++) {
$xmlFile = $zip->getNameIndex($i); $xmlFile = $zip->getNameIndex($i);
if ((substr($xmlFile, 0, strlen($wordRelsPath))) == $wordRelsPath) { if ((substr($xmlFile, 0, strlen($wordRelsPath))) == $wordRelsPath && (substr($xmlFile, -1)) != '/') {
$docPart = str_replace('.xml.rels', '', str_replace($wordRelsPath, '', $xmlFile)); $docPart = str_replace('.xml.rels', '', str_replace($wordRelsPath, '', $xmlFile));
$this->rels[$docPart] = $this->getRels($filename, $xmlFile, 'word/'); $this->rels[$docPart] = $this->getRels($filename, $xmlFile, 'word/');
} }

View File

@ -100,6 +100,8 @@ class ListItem extends AbstractStyle
/** /**
* Set numbering style name * Set numbering style name
*
* @param string $value
*/ */
public function setNumStyle($value) public function setNumStyle($value)
{ {

View File

@ -16,15 +16,23 @@ namespace PhpOffice\PhpWord\Tests\Element;
*/ */
class AbstractElementTest extends \PHPUnit_Framework_TestCase class AbstractElementTest extends \PHPUnit_Framework_TestCase
{ {
public function testElementIndex(){ /**
$stub = $this->getMockForAbstractClass('\PhpOffice\PhpWord\Element\AbstractElement'); * Test set/get element index
$ival = rand(0, 100); */
$stub->setElementIndex($ival); public function testElementIndex()
$this->assertEquals($stub->getElementIndex(), $ival); {
$stub = $this->getMockForAbstractClass('\PhpOffice\PhpWord\Element\AbstractElement');
$ival = rand(0, 100);
$stub->setElementIndex($ival);
$this->assertEquals($stub->getElementIndex(), $ival);
} }
public function testElementId(){ /**
$stub = $this->getMockForAbstractClass('\PhpOffice\PhpWord\Element\AbstractElement'); * Test set/get element unique Id
$stub->setElementId(); */
$this->assertEquals(strlen($stub->getElementId()), 6); public function testElementId()
{
$stub = $this->getMockForAbstractClass('\PhpOffice\PhpWord\Element\AbstractElement');
$stub->setElementId();
$this->assertEquals(strlen($stub->getElementId()), 6);
} }
} }

View File

@ -94,6 +94,9 @@ class ImageTest extends \PHPUnit_Framework_TestCase
$this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\Image', $oImage->getStyle()); $this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\Image', $oImage->getStyle());
} }
/**
* Test set wrapping style
*/
public function testStyleWrappingStyle() public function testStyleWrappingStyle()
{ {
@ -215,6 +218,6 @@ class ImageTest extends \PHPUnit_Framework_TestCase
*/ */
public function testPcxImage() public function testPcxImage()
{ {
$object = new Image('http://samples.libav.org/image-samples/RACECAR.BMP'); $object = new Image('http://samples.libav.org/image-samples/RACECAR.BMP');
} }
} }

View File

@ -48,11 +48,11 @@ class TableTest extends \PHPUnit_Framework_TestCase
*/ */
public function testStyleArray() public function testStyleArray()
{ {
$oTable = new Table( $oTable = new Table('section', 1, array(
'section', 'borderSize' => 6,
1, 'borderColor' => '006699',
array('borderSize' => 6, 'borderColor' => '006699', 'cellMargin' => 80) 'cellMargin' => 80
); ));
$this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\Table', $oTable->getStyle()); $this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\Table', $oTable->getStyle());
} }
@ -63,7 +63,7 @@ class TableTest extends \PHPUnit_Framework_TestCase
public function testWidth() public function testWidth()
{ {
$oTable = new Table('section', 1); $oTable = new Table('section', 1);
$iVal = rand(1, 1000); $iVal = rand(1, 1000);
$oTable->setWidth($iVal); $oTable->setWidth($iVal);
$this->assertEquals($oTable->getWidth(), $iVal); $this->assertEquals($oTable->getWidth(), $iVal);
} }
@ -73,7 +73,7 @@ class TableTest extends \PHPUnit_Framework_TestCase
*/ */
public function testRow() public function testRow()
{ {
$oTable = new Table('section', 1); $oTable = new Table('section', 1);
$element = $oTable->addRow(); $element = $oTable->addRow();
$this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Row', $element); $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Row', $element);
$this->assertCount(1, $oTable->getRows()); $this->assertCount(1, $oTable->getRows());
@ -84,10 +84,10 @@ class TableTest extends \PHPUnit_Framework_TestCase
*/ */
public function testCell() public function testCell()
{ {
$oTable = new Table('section', 1); $oTable = new Table('section', 1);
$oTable->addRow(); $oTable->addRow();
$element = $oTable->addCell(); $element = $oTable->addCell();
$this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Cell', $element); $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Cell', $element);
} }
/** /**
@ -95,12 +95,12 @@ class TableTest extends \PHPUnit_Framework_TestCase
*/ */
public function testCountColumns() public function testCountColumns()
{ {
$oTable = new Table('section', 1); $oTable = new Table('section', 1);
$oTable->addRow(); $oTable->addRow();
$element = $oTable->addCell(); $element = $oTable->addCell();
$this->assertEquals($oTable->countColumns(), 1); $this->assertEquals($oTable->countColumns(), 1);
$element = $oTable->addCell(); $element = $oTable->addCell();
$element = $oTable->addCell(); $element = $oTable->addCell();
$this->assertEquals($oTable->countColumns(), 3); $this->assertEquals($oTable->countColumns(), 3);
} }
} }

View File

@ -0,0 +1,54 @@
<?php
/**
* PHPWord
*
* @link https://github.com/PHPOffice/PHPWord
* @copyright 2014 PHPWord
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
*/
namespace PhpOffice\PhpWord\Tests\Shared;
use PhpOffice\PhpWord\Shared\XMLReader;
/**
* Test class for PhpOffice\PhpWord\Shared\XMLReader
*
* @runTestsInSeparateProcesses
* @since 0.10.0
*/
class XMLReaderTest extends \PHPUnit_Framework_TestCase
{
/**
* Test get DOMDocument from ZipArchive returns false
*/
public function testGetDomFromZipReturnsFalse()
{
$filename = __DIR__ . "/../_files/documents/reader.docx.zip";
$object = new XMLReader();
$this->assertFalse($object->getDomFromZip($filename, 'yadayadaya'));
}
/**
* Test get elements returns empty
*/
public function testGetElementsReturnsEmpty()
{
$object = new XMLReader();
$this->assertEquals(array(), $object->getElements('w:document'));
}
/**
* Test get element returns null
*/
public function testGetElementReturnsNull()
{
$filename = __DIR__ . "/../_files/documents/reader.docx.zip";
$object = new XMLReader();
$object->getDomFromZip($filename, '[Content_Types].xml');
$element = $object->getElements('*')->item(0);
$this->assertNull($object->getElement('yadayadaya', $element));
}
}

View File

@ -24,8 +24,8 @@ class ZipArchiveTest extends \PHPUnit_Framework_TestCase
public function testAdd() public function testAdd()
{ {
$existingFile = __DIR__ . "/../_files/documents/sheet.xls"; $existingFile = __DIR__ . "/../_files/documents/sheet.xls";
$zipFile = __DIR__ . "/../_files/documents/ziptest.zip"; $zipFile = __DIR__ . "/../_files/documents/ziptest.zip";
$object = new ZipArchive(); $object = new ZipArchive();
$object->open($zipFile); $object->open($zipFile);
$object->addFile($existingFile, 'xls/new.xls'); $object->addFile($existingFile, 'xls/new.xls');
$object->addFromString('content/string.txt', 'Test'); $object->addFromString('content/string.txt', 'Test');
@ -37,33 +37,39 @@ class ZipArchiveTest extends \PHPUnit_Framework_TestCase
unlink($zipFile); unlink($zipFile);
} }
/**
* Test find if a given name exists in the archive
*/
public function testLocate() public function testLocate()
{ {
$existingFile = __DIR__ . "/../_files/documents/sheet.xls"; $existingFile = __DIR__ . "/../_files/documents/sheet.xls";
$zipFile = __DIR__ . "/../_files/documents/ziptest.zip"; $zipFile = __DIR__ . "/../_files/documents/ziptest.zip";
$object = new ZipArchive(); $object = new ZipArchive();
$object->open($zipFile); $object->open($zipFile);
$object->addFile($existingFile, 'xls/new.xls'); $object->addFile($existingFile, 'xls/new.xls');
$object->addFromString('content/string.txt', 'Test'); $object->addFromString('content/string.txt', 'Test');
$this->assertEquals(1, $object->locateName('content/string.txt')); $this->assertEquals(1, $object->locateName('content/string.txt'));
$this->assertFalse($object->locateName('blablabla')); $this->assertFalse($object->locateName('blablabla'));
unlink($zipFile); unlink($zipFile);
} }
/**
* Test returns the name of an entry using its index
*/
public function testNameIndex() public function testNameIndex()
{ {
$existingFile = __DIR__ . "/../_files/documents/sheet.xls"; $existingFile = __DIR__ . "/../_files/documents/sheet.xls";
$zipFile = __DIR__ . "/../_files/documents/ziptest.zip"; $zipFile = __DIR__ . "/../_files/documents/ziptest.zip";
$object = new ZipArchive(); $object = new ZipArchive();
$object->open($zipFile); $object->open($zipFile);
$object->addFile($existingFile, 'xls/new.xls'); $object->addFile($existingFile, 'xls/new.xls');
$object->addFromString('content/string.txt', 'Test'); $object->addFromString('content/string.txt', 'Test');
$this->assertFalse($object->getNameIndex(-1)); $this->assertFalse($object->getNameIndex(-1));
$this->assertEquals('content/string.txt', $object->getNameIndex(1)); $this->assertEquals('content/string.txt', $object->getNameIndex(1));
unlink($zipFile); unlink($zipFile);
} }
} }

View File

@ -32,7 +32,7 @@ class ImageTest extends \PHPUnit_Framework_TestCase
'align' => 'left', 'align' => 'left',
'marginTop' => 240, 'marginTop' => 240,
'marginLeft' => 240, 'marginLeft' => 240,
'wrappingStyle' => 'inline', 'wrappingStyle' => 'inline'
); );
foreach ($properties as $key => $value) { foreach ($properties as $key => $value) {
$set = "set{$key}"; $set = "set{$key}";
@ -54,7 +54,7 @@ class ImageTest extends \PHPUnit_Framework_TestCase
'height' => 200, 'height' => 200,
'align' => 'left', 'align' => 'left',
'marginTop' => 240, 'marginTop' => 240,
'marginLeft' => 240, 'marginLeft' => 240
); );
foreach ($properties as $key => $value) { foreach ($properties as $key => $value) {
$get = "get{$key}"; $get = "get{$key}";