2014-12-30 18:53:32 +00:00
|
|
|
<?php
|
2015-01-22 22:06:48 +00:00
|
|
|
|
2014-12-30 18:53:32 +00:00
|
|
|
/**
|
2015-01-22 22:06:48 +00:00
|
|
|
* PHP TableOfContents Library
|
|
|
|
*
|
|
|
|
* @license http://opensource.org/licenses/MIT
|
2015-02-04 20:42:05 +00:00
|
|
|
* @link https://github.com/igorvbelousov/toc
|
2015-01-22 22:06:48 +00:00
|
|
|
* @version 1.0
|
2015-02-04 20:42:05 +00:00
|
|
|
* @package igorvbelousov/toc
|
2015-01-22 22:06:48 +00:00
|
|
|
* @author Casey McLaughlin <caseyamcl@gmail.com>
|
2015-02-04 20:42:05 +00:00
|
|
|
* @author Igor V Belousov <igor@belousovv.ru>
|
2015-01-22 22:06:48 +00:00
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*
|
|
|
|
* ------------------------------------------------------------------
|
2014-12-30 18:53:32 +00:00
|
|
|
*/
|
|
|
|
|
2015-01-22 21:52:45 +00:00
|
|
|
namespace TOC;
|
|
|
|
|
|
|
|
use TOC\Util\TOCTestUtils;
|
2014-12-30 18:53:32 +00:00
|
|
|
|
2015-01-22 20:30:01 +00:00
|
|
|
/**
|
|
|
|
* Class TocGeneratorTest
|
|
|
|
*
|
|
|
|
* @author Casey McLaughlin <caseyamcl@gmail.com>
|
|
|
|
*/
|
|
|
|
class TocGeneratorTest extends \PHPUnit_Framework_TestCase
|
2014-12-30 18:53:32 +00:00
|
|
|
{
|
|
|
|
public function testInstantiateSucceeds()
|
|
|
|
{
|
|
|
|
$obj = new TocGenerator();
|
|
|
|
$this->assertInstanceOf('\TOC\TocGenerator', $obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------
|
|
|
|
|
2015-01-22 21:52:45 +00:00
|
|
|
public function testGetMenuTraversesLevelsCorrectly()
|
2014-12-30 18:53:32 +00:00
|
|
|
{
|
|
|
|
$obj = new TocGenerator();
|
|
|
|
|
2015-01-22 20:30:01 +00:00
|
|
|
$html = "
|
|
|
|
<h1 id='a'>A-Header</h1><p>Foobar</p>
|
|
|
|
<h2 id='b'>B-Header</h2>
|
|
|
|
<h2 id='c'>C-Header</h2>
|
|
|
|
<h3 id='d'>D-Header</h3>
|
|
|
|
<h4 id='e'>E-Header</h4>
|
|
|
|
<h2 id='f'>F-Header</h2>
|
|
|
|
<h5 id='g'>G-Header</h5>
|
|
|
|
<h1 id='h'>H-Header</h1><div>Hi</div>
|
|
|
|
";
|
|
|
|
|
|
|
|
$fixture = array_filter(array_map('trim', file(__DIR__ . '/fixtures/testHtmlList.html')));
|
|
|
|
$actual = array_filter(array_map('trim', explode(PHP_EOL, $obj->getHtmlMenu($html, 1, 6))));
|
|
|
|
|
|
|
|
$this->assertEquals($fixture, $actual);
|
2014-12-30 18:53:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------
|
|
|
|
|
2015-01-22 21:52:45 +00:00
|
|
|
public function testGetMenuMatchesOnlyElementsWithIDs()
|
2014-12-30 18:53:32 +00:00
|
|
|
{
|
2015-01-22 20:30:01 +00:00
|
|
|
$html = "
|
|
|
|
<h1 id='a'>A-Header</h1><p>Foobar</p>
|
|
|
|
<h1 id='b'>B-Header</h1>
|
|
|
|
<h1>C-Header</h1>
|
|
|
|
";
|
2014-12-30 18:53:32 +00:00
|
|
|
|
|
|
|
$obj = new TocGenerator();
|
2015-01-22 20:30:01 +00:00
|
|
|
$menu = $obj->getMenu($html, 1);
|
2014-12-30 18:53:32 +00:00
|
|
|
|
2015-01-22 20:30:01 +00:00
|
|
|
$this->assertCount(2, $menu);
|
|
|
|
$this->assertEquals('A-Header', $menu->getFirstChild()->getLabel());
|
|
|
|
$this->assertEquals('B-Header', $menu->getLastChild()->getLabel());
|
2014-12-30 18:53:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------
|
|
|
|
|
2015-01-22 21:52:45 +00:00
|
|
|
public function testGetMenuUsesTitleForDisplayTextWhenAvailableAndPlainTextWhenNot()
|
|
|
|
{
|
|
|
|
$obj = new TocGenerator();
|
|
|
|
|
|
|
|
$html = '<h1 id="a" title="Foo Bar!">A Header</h1>';
|
|
|
|
$html .= '<h2 id="b">B Header</h2>';
|
|
|
|
$html .= '<h3 id="c" title="Baz Biz~">C Header</h3>';
|
|
|
|
|
|
|
|
$menu = $obj->getMenu($html, 1, 3);
|
|
|
|
$arr = TOCTestUtils::flattenMenuItems($menu);
|
|
|
|
|
|
|
|
$this->assertEquals('Foo Bar!', $arr[0]->getLabel());
|
|
|
|
$this->assertEquals('B Header', $arr[1]->getLabel());
|
|
|
|
$this->assertEquals('Baz Biz~', $arr[2]->getLabel());
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------
|
|
|
|
|
|
|
|
public function testGetMenuGetsOnlyHeaderLevelsSpecified()
|
|
|
|
{
|
|
|
|
$obj = new TocGenerator();
|
|
|
|
|
|
|
|
$html = '<h1 id="a" title="Foo Bar!">A Header</h1>';
|
|
|
|
$html .= '<h2 id="b">B Header</h2>';
|
|
|
|
$html .= '<h3 id="c" title="Baz Biz~">C Header</h3>';
|
|
|
|
$html .= '<h4 id="d" title="Bal Baf#">D Header</h4>';
|
|
|
|
$html .= '<h5 id="e" title="Cak Coy%">E Header</h5>';
|
|
|
|
$html .= '<h6 id="f" title="Dar Dul^">F Header</h6>';
|
|
|
|
|
|
|
|
$this->assertCount(1, TOCTestUtils::flattenMenuItems($obj->getMenu($html, 5, 1)));
|
|
|
|
$this->assertCount(2, TOCTestUtils::flattenMenuItems($obj->getMenu($html, 5, 5)));
|
|
|
|
|
|
|
|
// What's up with this?
|
|
|
|
//$this->assertCount(6, TOCTestUtils::flattenMenuItems($obj->getMenu($html, -1, 20)));
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------
|
|
|
|
|
|
|
|
public function testGetMenuReturnsAnEmptyArrayWhenNoContentOrMatches()
|
|
|
|
{
|
|
|
|
$obj = new TocGenerator();
|
|
|
|
$this->assertEquals(0, count($obj->getMenu("<h1>Boo</h1><h2>Bar</h2>")));
|
|
|
|
$this->assertEquals(0, count($obj->getMenu("")));
|
|
|
|
}
|
2014-12-30 18:53:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* EOF: TocGeneratorTest.php */
|