Skip to content

Commit 530a71c

Browse files
committed
Unit test for TOC
1 parent 042f7a0 commit 530a71c

File tree

3 files changed

+74
-1
lines changed

3 files changed

+74
-1
lines changed

Classes/PHPWord/Section/TextRun.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,8 @@ public function addImage($imageSrc, $style = null)
136136
*
137137
* @param int $count
138138
*/
139-
public function addTextBreak($count = 1) {
139+
public function addTextBreak($count = 1)
140+
{
140141
for ($i=1; $i<=$count; $i++) {
141142
$this->_elementCollection[] = new PHPWord_Section_TextBreak();
142143
}

Tests/PHPWord/TOCTest.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
namespace PHPWord\Tests;
3+
4+
use PHPUnit_Framework_TestCase;
5+
use PHPWord_TOC;
6+
use PHPWord_Style_TOC;
7+
8+
/**
9+
* @covers PHPWord_TOC
10+
*/
11+
class TOCTest extends PHPUnit_Framework_TestCase
12+
{
13+
/**
14+
* @covers PHPWord_TOC::__construct
15+
* @covers PHPWord_TOC::getStyleTOC
16+
* @covers PHPWord_TOC::getStyleFont
17+
*/
18+
public function testConstruct()
19+
{
20+
$expected = array(
21+
'tabPos' => 9062,
22+
'tabLeader' => PHPWord_Style_TOC::TABLEADER_DOT,
23+
'indent' => 200,
24+
);
25+
$object = new PHPWord_TOC(
26+
array('size' => 11),
27+
array('tabPos' => $expected['tabPos'])
28+
);
29+
$tocStyle = $object->getStyleTOC();
30+
31+
$this->assertInstanceOf('PHPWord_Style_TOC', $tocStyle);
32+
$this->assertInstanceOf('PHPWord_Style_Font', $object->getStyleFont());
33+
34+
foreach ($expected as $key => $value) {
35+
$method = "get{$key}";
36+
$this->assertEquals($value, $tocStyle->$method());
37+
}
38+
}
39+
40+
/**
41+
* @covers PHPWord_TOC::addTitle
42+
* @covers PHPWord_TOC::getTitles
43+
*/
44+
public function testAddAndGetTitle()
45+
{
46+
// Prepare variables
47+
$titleCount = 3;
48+
$anchor = '_Toc' . (252634154 + $titleCount);
49+
$bookmark = $titleCount - 1; // zero based
50+
$titles = array(
51+
'Heading 1' => 1,
52+
'Heading 2' => 2,
53+
'Heading 3' => 3,
54+
);
55+
56+
// @covers PHPWord_TOC::addTitle
57+
foreach ($titles as $text => $depth) {
58+
$response = PHPWord_TOC::addTitle($text, $depth);
59+
}
60+
$this->assertEquals($anchor, $response[0]);
61+
$this->assertEquals($bookmark, $response[1]);
62+
63+
// @covers PHPWord_TOC::getTitles
64+
$i = 0;
65+
$savedTitles = PHPWord_TOC::getTitles();
66+
foreach ($titles as $text => $depth) {
67+
$this->assertEquals($text, $savedTitles[$i]['text']);
68+
$this->assertEquals($depth, $savedTitles[$i]['depth']);
69+
$i++;
70+
}
71+
}
72+
}

0 commit comments

Comments
 (0)