Этот коммит содержится в:
Casey McLaughlin
2014-12-30 13:53:32 -05:00
Коммит 7ea43a97fc
22 изменённых файлов: 1751 добавлений и 0 удалений

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

@@ -0,0 +1,90 @@
<?php
/**
* Created by PhpStorm.
* User: casey
* Date: 12/30/14
* Time: 12:39 PM
*/
use TOC\TocGenerator;
class TocGeneratorTest extends PHPUnit_Framework_TestCase
{
public function testInstantiateSucceeds()
{
$obj = new TocGenerator();
$this->assertInstanceOf('\TOC\TocGenerator', $obj);
}
// ---------------------------------------------------------------
public function testGetItemsMatchesOnlyElementsWithIDs()
{
$obj = new TocGenerator();
$html = "<h1 id='a'>A Header</h1><p>Foobar</p><h2>B Header</h2><h3 id='c'>C Header</h3>";
$this->assertEquals(['a' => 'A Header', 'c' => 'C Header'], $obj->getItems($html, 1, 3));
}
// ---------------------------------------------------------------
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(""));
}
}
/* EOF: TocGeneratorTest.php */

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

@@ -0,0 +1,60 @@
<?php
use TOC\TocMarkupFixer;
/**
* Created by PhpStorm.
* User: casey
* Date: 12/30/14
* Time: 1:24 PM
*/
class TocMarkupFixerTest extends PHPUnit_Framework_TestCase
{
public function testInstantiateSucceeds()
{
$obj = new TocMarkupFixer();
$this->assertInstanceOf('\TOC\TocMarkupFixer', $obj);
}
// ---------------------------------------------------------------
public function testFixAddsIdsOnlyToElementsWithoutThem()
{
$obj = new TocMarkupFixer();
$html = "<h1>No ID</h1><h2>Existing ID</h2><h3>Ignored</h3>";
$this->assertEquals(
'<h1 id="no-id">No ID</h1><h2 id="existing-id">Existing ID</h2><h3>Ignored</h3>',
$obj->fix($html, 1, 2)
);
}
// ---------------------------------------------------------------
public function testFixDoesNotDuplicateIdsWhenFixing()
{
$obj = new TocMarkupFixer();
$html = "<h1>FooBar</h1><h2>FooBar</h2><h3>FooBar</h3>";
$this->assertEquals(
'<h1 id="foobar">FooBar</h1><h2 id="foobar-1">FooBar</h2><h3 id="foobar-2">FooBar</h3>',
$obj->fix($html, 1, 3)
);
}
// ---------------------------------------------------------------
public function testFixUsesTitleAttributeWhenAvailable()
{
$obj = new TocMarkupFixer();
$html = "<h1>No ID</h1><h2 title='b'>Existing ID</h2><h3>Ignored</h3>";
$this->assertEquals(
'<h1 id="no-id">No ID</h1><h2 title=\'b\' id="b">Existing ID</h2><h3>Ignored</h3>',
$obj->fix($html, 1, 2)
);
}
}

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

@@ -0,0 +1,27 @@
<?php
// HEADER HERE
// ---------------------------------------------------------------
/**
* PHP TOC Unit Tests Bootstrap File
*
* @author Casey McLaughlin <caseyamcl@gmail.com>
*/
//Files to ensure exist
$checkFiles['autoload'] = __DIR__.'/../vendor/autoload.php';
$checkFiles[] = __DIR__.'/../vendor/sunra/php-simple-html-dom-parser/README.md';
//Check 'Em
foreach($checkFiles as $file) {
if ( ! file_exists($file)) {
throw new RuntimeException('Install development dependencies to run test suite.');
}
}
//Away we go
$autoload = require_once $checkFiles['autoload'];
/* EOF: bootstrap.php */