PHPWord/tests/PhpWord/Tests/TOCTest.php

115 lines
3.0 KiB
PHP
Raw Normal View History

2014-03-12 16:24:36 +07:00
<?php
2014-03-27 23:55:06 +07:00
/**
* 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;
2014-03-12 16:24:36 +07:00
use PhpOffice\PhpWord\TOC;
2014-03-12 16:24:36 +07:00
/**
2014-03-27 23:55:06 +07:00
* Test class for PhpOffice\PhpWord\TOC
*
2014-03-13 01:58:00 +07:00
* @runTestsInSeparateProcesses
2014-03-12 16:24:36 +07:00
*/
class TOCTest extends \PHPUnit_Framework_TestCase
2014-03-12 16:24:36 +07:00
{
/**
2014-03-28 13:50:53 +07:00
* Construct with font and TOC style in array format
2014-03-12 16:24:36 +07:00
*/
2014-03-28 13:50:53 +07:00
public function testConstructWithStyleArray()
2014-03-12 16:24:36 +07:00
{
$expected = array(
'tabPos' => 9062,
'tabLeader' => \PhpOffice\PhpWord\Style\TOC::TABLEADER_DOT,
'indent' => 200,
2014-03-12 16:24:36 +07:00
);
2014-04-09 20:44:32 +07:00
$object = new TOC(array('_size' => 11), array('_tabPos' => $expected['tabPos']));
2014-03-12 16:24:36 +07:00
$tocStyle = $object->getStyleTOC();
$this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\TOC', $tocStyle);
$this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\Font', $object->getStyleFont());
2014-03-12 16:24:36 +07:00
foreach ($expected as $key => $value) {
$method = "get{$key}";
$this->assertEquals($value, $tocStyle->$method());
}
}
/**
2014-03-28 13:50:53 +07:00
* Construct with named font style
*/
public function testConstructWithStyleName()
{
$object = new TOC('Font Style');
$tocStyle = $object->getStyleTOC();
$this->assertEquals('Font Style', $object->getStyleFont());
}
/**
* Add and get title
2014-03-12 16:24:36 +07:00
*/
public function testAddAndGetTitle()
{
$titleCount = 3;
$anchor = '_Toc' . (252634154 + $titleCount);
2014-03-13 01:58:00 +07:00
$bookmark = $titleCount - 1;
2014-03-12 16:24:36 +07:00
$titles = array(
'Heading 1' => 1,
'Heading 2' => 2,
'Heading 3' => 3,
);
2014-04-06 01:10:04 +07:00
$toc = new TOC();
2014-03-12 16:24:36 +07:00
foreach ($titles as $text => $depth) {
2014-04-06 01:10:04 +07:00
$response = $toc->addTitle($text, $depth);
2014-03-12 16:24:36 +07:00
}
$this->assertEquals($anchor, $response[0]);
$this->assertEquals($bookmark, $response[1]);
$i = 0;
2014-04-06 01:10:04 +07:00
$savedTitles = $toc->getTitles();
2014-03-12 16:24:36 +07:00
foreach ($titles as $text => $depth) {
$this->assertEquals($text, $savedTitles[$i]['text']);
$this->assertEquals($depth, $savedTitles[$i]['depth']);
$i++;
}
TOC::resetTitles();
2014-04-06 11:52:16 +07:00
$this->assertEquals(0, count($toc->getTitles()));
2014-03-12 16:24:36 +07:00
}
/**
* Set/get minDepth and maxDepth
*/
public function testSetGetMinMaxDepth()
{
$toc = new TOC();
$titles = array(
'Heading 1' => 1,
'Heading 2' => 2,
'Heading 3' => 3,
'Heading 4' => 4,
);
foreach ($titles as $text => $depth) {
$toc->addTitle($text, $depth);
}
$this->assertEquals(1, $toc->getMinDepth());
$this->assertEquals(9, $toc->getMaxDepth());
$toc->setMinDepth(2);
$toc->setMaxDepth(3);
$toc->getTitles();
$this->assertEquals(2, $toc->getMinDepth());
$this->assertEquals(3, $toc->getMaxDepth());
}
2014-03-12 16:24:36 +07:00
}