From f0aa87d02e8885825878781cd044c04c25e381ea Mon Sep 17 00:00:00 2001 From: Matt Butcher Date: Sat, 6 Apr 2013 13:35:50 -0500 Subject: Converted example.php into a number of unit tests. --- test/HTML5/SerializerTest.php | 109 ++++++++++++++++++++++++++++++++++++++++-- test/HTML5/TestCase.php | 7 +++ 2 files changed, 112 insertions(+), 4 deletions(-) (limited to 'test/HTML5') diff --git a/test/HTML5/SerializerTest.php b/test/HTML5/SerializerTest.php index 5eb44eb..ee8804e 100644 --- a/test/HTML5/SerializerTest.php +++ b/test/HTML5/SerializerTest.php @@ -1,4 +1,5 @@ saveHTML(); + + return $out; + } + + public function testSaveHTML() { $html = 'test'; $dom = \HTML5::parse($html); + $this->assertTrue($dom instanceof \DOMDocument, "Canary"); + + $ser = new \HTML5\Serializer($dom, FALSE); + $out = $ser->saveHTML(); + + $this->assertTrue(count($out) >= count($html), 'Byte counts'); + $this->assertRegExp('//', $out, 'Has DOCTYPE.'); + $this->assertRegExp('/test<\/body>/', $out, 'Has body text.'); + } + + public function testSave() { + $html = 'test'; + + $dom = \HTML5::parse($html); $this->assertTrue($dom instanceof \DOMDocument, "Canary"); - $ser = new \HTML5\Serializer($dom); + $ser = new \HTML5\Serializer($dom, FALSE); + $out = fopen("php://temp", "w"); + $ser->save($out); - $out = $ser->saveHTML(); + rewind($out); + $res = stream_get_contents($out); + $this->assertTrue(count($res) >= count($html)); + } + + public function testElements() { + // Should have content. + $res = $this->cycle('
FOO
'); + $this->assertRegExp('|
FOO
|', $res); + + // Should be empty + $res = $this->cycle(''); + $this->assertRegExp('||', $res); - $this->assertTrue(count($out) >= count($html)); + // Should have no closing tag. + $res = $this->cycle('
'); + $this->assertRegExp('|
|', $res); + + } + + public function testAttributes() { + $res = $this->cycle('
FOO
'); + $this->assertRegExp('|
FOO
|', $res); + + // XXX: Note that spec does NOT require attrs in the same order. + $res = $this->cycle('
FOO
'); + $this->assertRegExp('|
FOO
|', $res); + + $res = $this->cycle('
FOO
'); + $this->assertRegExp('|
FOO
|', $res); + } + + public function testPCData() { + $res = $this->cycle('This is a test.'); + $this->assertRegExp('|This is a test.|', $res); + + $res = $this->cycle('This + is + a + test.'); + + // Check that newlines are there, but don't count spaces. + $this->assertRegExp('|This\n\s*is\n\s*a\n\s*test.|', $res); + + $res = $this->cycle('This is a test.'); + $this->assertRegExp('|This is a test.|', $res); + } + + public function testUnescaped() { + $res = $this->cycle(''); + $this->assertRegExp('|2 < 1|', $res); + + $res = $this->cycle(''); + $this->assertRegExp('|div>div>div|', $res); + } + + public function testEntities() { + $res = $this->cycle('Apples & bananas.'); + $this->assertRegExp('|Apples & bananas.|', $res); + } + + public function testComment() { + $res = $this->cycle('ab'); + $this->assertRegExp('||', $res); + } + // FAILS because the parser converts CDATA to a comment. Issue #2. + public function testCDATA() { + $res = $this->cycle('a a test. ]]>b'); + $this->assertRegExp('| a test. ]]>|', $res); } } diff --git a/test/HTML5/TestCase.php b/test/HTML5/TestCase.php index b5c18e0..8d3148e 100644 --- a/test/HTML5/TestCase.php +++ b/test/HTML5/TestCase.php @@ -5,8 +5,15 @@ require_once 'PHPUnit/Autoload.php'; require_once __DIR__ . '/../../vendor/autoload.php'; class TestCase extends \PHPUnit_Framework_TestCase { + const DOC_OPEN = 'test'; + const DOC_CLOSE = ''; public function testFoo() { // Placeholder. Why is PHPUnit emitting warnings about no tests? } + + protected function wrap($fragment) { + return self::DOC_OPEN . $fragment . self::DOC_CLOSE; + } + } -- cgit v1.2.3