Этот коммит содержится в:
Casey McLaughlin 2015-01-22 16:52:45 -05:00
родитель 5968cfef3e
Коммит 742bb52ff0
4 изменённых файлов: 94 добавлений и 65 удалений

Просмотреть файл

@ -97,7 +97,7 @@ class TocGenerator
$level = array_search(strtolower($tagName), $tagsToMatch) + 1; $level = array_search(strtolower($tagName), $tagsToMatch) + 1;
// Determine parent item which to add child // Determine parent item which to add child
if ($level == 0) { if ($level == 1) {
$parent = $menu; $parent = $menu;
} }
elseif ($level == $lastElem->getLevel()) { elseif ($level == $lastElem->getLevel()) {

Просмотреть файл

@ -1,5 +1,6 @@
<?php <?php
use TOC\MarkupFixer;
namespace TOC;
/** /**
* Created by PhpStorm. * Created by PhpStorm.
@ -8,7 +9,7 @@ use TOC\MarkupFixer;
* Time: 1:24 PM * Time: 1:24 PM
*/ */
class MarkupFixerTest extends PHPUnit_Framework_TestCase class MarkupFixerTest extends \PHPUnit_Framework_TestCase
{ {
public function testInstantiateSucceeds() public function testInstantiateSucceeds()
{ {

Просмотреть файл

@ -6,8 +6,9 @@
* Time: 12:39 PM * Time: 12:39 PM
*/ */
use Knp\Menu\Renderer\ListRenderer; namespace TOC;
use TOC\TocGenerator;
use TOC\Util\TOCTestUtils;
/** /**
* Class TocGeneratorTest * Class TocGeneratorTest
@ -24,7 +25,7 @@ class TocGeneratorTest extends \PHPUnit_Framework_TestCase
// --------------------------------------------------------------- // ---------------------------------------------------------------
public function testGetItemsTraversesLevelsCorrectly() public function testGetMenuTraversesLevelsCorrectly()
{ {
$obj = new TocGenerator(); $obj = new TocGenerator();
@ -47,7 +48,7 @@ class TocGeneratorTest extends \PHPUnit_Framework_TestCase
// --------------------------------------------------------------- // ---------------------------------------------------------------
public function testGetItemsMatchesOnlyElementsWithIDs() public function testGetMenuMatchesOnlyElementsWithIDs()
{ {
$html = " $html = "
<h1 id='a'>A-Header</h1><p>Foobar</p> <h1 id='a'>A-Header</h1><p>Foobar</p>
@ -64,64 +65,51 @@ class TocGeneratorTest extends \PHPUnit_Framework_TestCase
} }
// --------------------------------------------------------------- // ---------------------------------------------------------------
//
// public function testGetItemsUsesTitleForDisplayTextWhenAvailableAndPlainTextWhenNot()
// {
// $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>';
//
// $this->assertEquals(
// ['a' => 'Foo Bar!', 'b' => 'B Header', 'c' => 'Baz Biz~'],
// $obj->getItems($html, 1, 3)
// );
// }
//
// // ---------------------------------------------------------------
//
// public function testGetItemsGetsOnlyHeaderLevelsSpecified()
// {
// $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, $obj->getItems($html, 5, 1));
// $this->assertCount(2, $obj->getItems($html, 5, 5));
// $this->assertCount(6, $obj->getItems($html, -1, 20));
// }
//
// // ---------------------------------------------------------------
//
// public function testGetHtmlItemsReturnsExpectedListItems()
// {
// $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>';
//
// $this->assertEquals(
// "<li><a title='Go to Foo Bar!' href='#a'>Foo Bar!</a></li><li><a title='Go to B Header' href='#b'>B Header</a></li><li><a title='Go to Baz Biz~' href='#c'>Baz Biz~</a></li>",
// $obj->getHtmlItems($html, 1, 3)
// );
// }
//
// // ---------------------------------------------------------------
//
// public function testGetItemsReturnsAnEmptyArrayWhenNoContentOrMatches()
// {
// $obj = new TocGenerator();
// $this->assertEquals([], $obj->getItems("<h1>Boo</h1><h2>Bar</h2>"));
// $this->assertEquals([], $obj->getItems(""));
// }
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("")));
}
} }
/* EOF: TocGeneratorTest.php */ /* EOF: TocGeneratorTest.php */

40
tests/Util/TOCTestUtils.php Обычный файл
Просмотреть файл

@ -0,0 +1,40 @@
<?php
/**
* toc
*
* @license ${LICENSE_LINK}
* @link ${PROJECT_URL_LINK}
* @version ${VERSION}
* @package ${PACKAGE_NAME}
* @author Casey McLaughlin <caseyamcl@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* ------------------------------------------------------------------
*/
namespace TOC\Util;
use Knp\Menu\ItemInterface;
class TOCTestUtils
{
/**
* Get a flattened array containing references to all of the items
*
* @param ItemInterface $item The menu item
* @param bool $isTop Is the initial menu item starting at the top-level?
* @return array
*/
public static function flattenMenuItems(ItemInterface $item, $isTop = true)
{
$arr = $isTop ? [] : [$item];
foreach ($item->getChildren() as $child) {
$arr = array_merge($arr, self::flattenMenuItems($child, false));
}
return $arr;
}
}