Skip to content

Commit b40218d

Browse files
committed
Add some unit tests for Shared & Element (100%!) - @Progi1984
1 parent 59e623b commit b40218d

File tree

6 files changed

+97
-6
lines changed

6 files changed

+97
-6
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ This release marked heavy refactorings on internal code structure with the creat
3535
### Bugfixes
3636

3737
- Footnote: Footnote content doesn't show footnote reference number - @ivanlanin GH-170
38+
- Documentation : Error in a fonction - @theBeerNut GH-195
3839

3940
### Deprecated
4041

@@ -63,6 +64,7 @@ This release marked heavy refactorings on internal code structure with the creat
6364
- Style: New `Style\AbstractStyle` abstract class - @ivanlanin GH-187
6465
- Writer: New 'ODText\Base` class - @ivanlanin GH-187
6566
- General: Rename `Footnote` to `Footnotes` to reflect the nature of collection - @ivanlanin
67+
- General: Add some unit tests for Shared & Element (100%!) - @Progi1984
6668

6769
## 0.9.1 - 27 Mar 2014
6870

src/PhpWord/Shared/ZipArchive.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,7 @@ public function getFromName($filename)
218218
public function getNameIndex($index)
219219
{
220220
$list = $this->zip->listContent();
221-
$listCount = count($list);
222-
if ($index <= $listCount) {
221+
if (isset($list[$index])) {
223222
return $list[$index]['filename'];
224223
} else {
225224
return false;
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
/**
3+
* PHPWord
4+
*
5+
* @link https://github.com/PHPOffice/PHPWord
6+
* @copyright 2014 PHPWord
7+
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
8+
*/
9+
10+
namespace PhpOffice\PhpWord\Tests\Element;
11+
12+
/**
13+
* Test class for PhpOffice\PhpWord\Element\Cell
14+
*
15+
* @runTestsInSeparateProcesses
16+
*/
17+
class AbstractElementTest extends \PHPUnit_Framework_TestCase
18+
{
19+
public function testElementIndex(){
20+
$stub = $this->getMockForAbstractClass('\PhpOffice\PhpWord\Element\AbstractElement');
21+
$ival = rand(0, 100);
22+
$stub->setElementIndex($ival);
23+
$this->assertEquals($stub->getElementIndex(), $ival);
24+
}
25+
public function testElementId(){
26+
$stub = $this->getMockForAbstractClass('\PhpOffice\PhpWord\Element\AbstractElement');
27+
$stub->setElementId();
28+
$this->assertEquals(strlen($stub->getElementId()), 6);
29+
}
30+
}

tests/PhpWord/Tests/Element/ImageTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,11 @@ public function testStyle()
9494
$this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\Image', $oImage->getStyle());
9595
}
9696

97+
public function testStyleWrappingStyle()
98+
{
99+
100+
}
101+
97102
/**
98103
* Get relation Id
99104
*/
@@ -202,4 +207,14 @@ public function testPhpImage()
202207
{
203208
$object = new Image('test.php');
204209
}
210+
211+
/**
212+
* Test PCX Image and Memory
213+
*
214+
* @expectedException \PhpOffice\PhpWord\Exception\UnsupportedImageTypeException
215+
*/
216+
public function testPcxImage()
217+
{
218+
$object = new Image('http://samples.libav.org/image-samples/RACECAR.BMP');
219+
}
205220
}

tests/PhpWord/Tests/Element/TableTest.php

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,23 @@ public function testRow()
8484
*/
8585
public function testCell()
8686
{
87-
$oTable = new Table('section', 1);
88-
$oTable->addRow();
89-
$element = $oTable->addCell();
90-
$this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Cell', $element);
87+
$oTable = new Table('section', 1);
88+
$oTable->addRow();
89+
$element = $oTable->addCell();
90+
$this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Cell', $element);
91+
}
92+
93+
/**
94+
* Add cell
95+
*/
96+
public function testCountColumns()
97+
{
98+
$oTable = new Table('section', 1);
99+
$oTable->addRow();
100+
$element = $oTable->addCell();
101+
$this->assertEquals($oTable->countColumns(), 1);
102+
$element = $oTable->addCell();
103+
$element = $oTable->addCell();
104+
$this->assertEquals($oTable->countColumns(), 3);
91105
}
92106
}

tests/PhpWord/Tests/Shared/ZipArchiveTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,38 @@ public function testAdd()
3232

3333
$this->assertTrue($object->locateName('xls/new.xls'));
3434
$this->assertEquals('Test', $object->getFromName('content/string.txt'));
35+
$this->assertEquals('Test', $object->getFromName('/content/string.txt'));
3536

3637
unlink($zipFile);
3738
}
39+
40+
public function testLocate()
41+
{
42+
$existingFile = __DIR__ . "/../_files/documents/sheet.xls";
43+
$zipFile = __DIR__ . "/../_files/documents/ziptest.zip";
44+
$object = new ZipArchive();
45+
$object->open($zipFile);
46+
$object->addFile($existingFile, 'xls/new.xls');
47+
$object->addFromString('content/string.txt', 'Test');
48+
49+
$this->assertEquals(1, $object->locateName('content/string.txt'));
50+
$this->assertFalse($object->locateName('blablabla'));
51+
52+
unlink($zipFile);
53+
}
54+
55+
public function testNameIndex()
56+
{
57+
$existingFile = __DIR__ . "/../_files/documents/sheet.xls";
58+
$zipFile = __DIR__ . "/../_files/documents/ziptest.zip";
59+
$object = new ZipArchive();
60+
$object->open($zipFile);
61+
$object->addFile($existingFile, 'xls/new.xls');
62+
$object->addFromString('content/string.txt', 'Test');
63+
64+
$this->assertFalse($object->getNameIndex(-1));
65+
$this->assertEquals('content/string.txt', $object->getNameIndex(1));
66+
67+
unlink($zipFile);
68+
}
3869
}

0 commit comments

Comments
 (0)