From e49c45913b9cff66e3ca25a17e98604e18388e1e Mon Sep 17 00:00:00 2001 From: Asmir Mustafic Date: Wed, 11 Jun 2014 10:34:56 +0200 Subject: PSR-2 code style --- test/HTML5/Serializer/OutputRulesTest.php | 604 +++++++++++++++++------------- 1 file changed, 335 insertions(+), 269 deletions(-) (limited to 'test/HTML5/Serializer/OutputRulesTest.php') diff --git a/test/HTML5/Serializer/OutputRulesTest.php b/test/HTML5/Serializer/OutputRulesTest.php index daae43a..27c66c4 100644 --- a/test/HTML5/Serializer/OutputRulesTest.php +++ b/test/HTML5/Serializer/OutputRulesTest.php @@ -3,11 +3,11 @@ namespace Masterminds\HTML5\Tests\Serializer; use Masterminds\HTML5\Serializer\OutputRules; use Masterminds\HTML5\Serializer\Traverser; -use Masterminds\HTML5\Parser; -class OutputRulesTest extends \Masterminds\HTML5\Tests\TestCase { +class OutputRulesTest extends \Masterminds\HTML5\Tests\TestCase +{ - protected $markup = ' + protected $markup = ' @@ -17,69 +17,82 @@ class OutputRulesTest extends \Masterminds\HTML5\Tests\TestCase {

This is a test.

'; - public function setUp() - { - $this->html5 = $this->getInstance(); - } - /** - * Using reflection we make a protected method accessible for testing. - * - * @param string $name - * The name of the method on the Traverser class to test. - * - * @return \ReflectionMethod - * \ReflectionMethod for the specified method - */ - function getProtectedMethod($name) { - $class = new \ReflectionClass('\Masterminds\HTML5\Serializer\OutputRules'); - $method = $class->getMethod($name); - $method->setAccessible(true); - return $method; - } - - function getTraverserProtectedProperty($name) { - $class = new \ReflectionClass('\Masterminds\HTML5\Serializer\Traverser'); - $property = $class->getProperty($name); - $property->setAccessible(true); - return $property; - } - - function getOutputRules($options = array()) { - $options = $options + $this->html5->getOptions(); - $stream = fopen('php://temp', 'w'); - $dom = $this->html5->loadHTML($this->markup); - $r = new OutputRules($stream, $options); - $t = new Traverser($dom, $stream, $r, $options); - - return array($r, $stream); - } - - function testDocument() { - $dom = $this->html5->loadHTML('foo'); - - $stream = fopen('php://temp', 'w'); - $r = new OutputRules($stream, $this->html5->getOptions()); - $t = new Traverser($dom, $stream, $r, $this->html5->getOptions()); - - $r->document($dom); - $expected = '' . PHP_EOL . 'foo' . PHP_EOL; - $this->assertEquals($expected, stream_get_contents($stream, -1, 0)); - } - - function testDoctype() { - $dom = $this->html5->loadHTML('foo'); - - $stream = fopen('php://temp', 'w'); - $r = new OutputRules($stream, $this->html5->getOptions()); - $t = new Traverser($dom, $stream, $r, $this->html5->getOptions()); - - $m = $this->getProtectedMethod('doctype'); - $m->invoke($r, 'foo'); - $this->assertEquals("" . PHP_EOL, stream_get_contents($stream, -1, 0)); - } - - function testElement() { - $dom = $this->html5->loadHTML(' + + public function setUp() + { + $this->html5 = $this->getInstance(); + } + + /** + * Using reflection we make a protected method accessible for testing. + * + * @param string $name + * The name of the method on the Traverser class to test. + * + * @return \ReflectionMethod \ReflectionMethod for the specified method + */ + public function getProtectedMethod($name) + { + $class = new \ReflectionClass('\Masterminds\HTML5\Serializer\OutputRules'); + $method = $class->getMethod($name); + $method->setAccessible(true); + + return $method; + } + + public function getTraverserProtectedProperty($name) + { + $class = new \ReflectionClass('\Masterminds\HTML5\Serializer\Traverser'); + $property = $class->getProperty($name); + $property->setAccessible(true); + + return $property; + } + + public function getOutputRules($options = array()) + { + $options = $options + $this->html5->getOptions(); + $stream = fopen('php://temp', 'w'); + $dom = $this->html5->loadHTML($this->markup); + $r = new OutputRules($stream, $options); + $t = new Traverser($dom, $stream, $r, $options); + + return array( + $r, + $stream + ); + } + + public function testDocument() + { + $dom = $this->html5->loadHTML('foo'); + + $stream = fopen('php://temp', 'w'); + $r = new OutputRules($stream, $this->html5->getOptions()); + $t = new Traverser($dom, $stream, $r, $this->html5->getOptions()); + + $r->document($dom); + $expected = '' . PHP_EOL . 'foo' . PHP_EOL; + $this->assertEquals($expected, stream_get_contents($stream, - 1, 0)); + } + + public function testDoctype() + { + $dom = $this->html5->loadHTML('foo'); + + $stream = fopen('php://temp', 'w'); + $r = new OutputRules($stream, $this->html5->getOptions()); + $t = new Traverser($dom, $stream, $r, $this->html5->getOptions()); + + $m = $this->getProtectedMethod('doctype'); + $m->invoke($r, 'foo'); + $this->assertEquals("" . PHP_EOL, stream_get_contents($stream, - 1, 0)); + } + + public function testElement() + { + $dom = $this->html5->loadHTML( + '
foo bar baz
@@ -91,23 +104,25 @@ class OutputRulesTest extends \Masterminds\HTML5\Tests\TestCase { '); - $stream = fopen('php://temp', 'w'); - $r = new OutputRules($stream, $this->html5->getOptions()); - $t = new Traverser($dom, $stream, $r, $this->html5->getOptions()); + $stream = fopen('php://temp', 'w'); + $r = new OutputRules($stream, $this->html5->getOptions()); + $t = new Traverser($dom, $stream, $r, $this->html5->getOptions()); - $list = $dom->getElementsByTagName('div'); - $r->element($list->item(0)); - $this->assertEquals('
foo bar baz
', stream_get_contents($stream, -1, 0)); - } + $list = $dom->getElementsByTagName('div'); + $r->element($list->item(0)); + $this->assertEquals('
foo bar baz
', stream_get_contents($stream, - 1, 0)); + } - function testElementWithScript() { - $dom = $this->html5->loadHTML(' + public function testElementWithScript() + { + $dom = $this->html5->loadHTML( + ' ', stream_get_contents($stream, -1, 0)); - } + ', stream_get_contents($stream, - 1, 0)); + } - function testElementWithStyle() { - $dom = $this->html5->loadHTML(' + public function testElementWithStyle() + { + $dom = $this->html5->loadHTML( + ' ', stream_get_contents($stream, -1, 0)); - } + ', stream_get_contents($stream, - 1, 0)); + } - function testOpenTag() { - $dom = $this->html5->loadHTML(' + public function testOpenTag() + { + $dom = $this->html5->loadHTML('
foo bar baz
'); - $stream = fopen('php://temp', 'w'); - $r = new OutputRules($stream, $this->html5->getOptions()); - $t = new Traverser($dom, $stream, $r, $this->html5->getOptions()); + $stream = fopen('php://temp', 'w'); + $r = new OutputRules($stream, $this->html5->getOptions()); + $t = new Traverser($dom, $stream, $r, $this->html5->getOptions()); - $list = $dom->getElementsByTagName('div'); - $m = $this->getProtectedMethod('openTag'); - $m->invoke($r, $list->item(0)); - $this->assertEquals('
', stream_get_contents($stream, -1, 0)); - } + $list = $dom->getElementsByTagName('div'); + $m = $this->getProtectedMethod('openTag'); + $m->invoke($r, $list->item(0)); + $this->assertEquals('
', stream_get_contents($stream, - 1, 0)); + } - function testCData() { - $dom = $this->html5->loadHTML(' + public function testCData() + { + $dom = $this->html5->loadHTML('
'); - $stream = fopen('php://temp', 'w'); - $r = new OutputRules($stream, $this->html5->getOptions()); - $t = new Traverser($dom, $stream, $r, $this->html5->getOptions()); + $stream = fopen('php://temp', 'w'); + $r = new OutputRules($stream, $this->html5->getOptions()); + $t = new Traverser($dom, $stream, $r, $this->html5->getOptions()); - $list = $dom->getElementsByTagName('div'); - $r->cdata($list->item(0)->childNodes->item(0)); - $this->assertEquals('', stream_get_contents($stream, -1, 0)); + $list = $dom->getElementsByTagName('div'); + $r->cdata($list->item(0)->childNodes->item(0)); + $this->assertEquals('', stream_get_contents($stream, - 1, 0)); - $dom = $this->html5->loadHTML(' + $dom = $this->html5->loadHTML('
'); + $dom->getElementById('foo')->appendChild(new \DOMCdataSection("]]>Foo<[![CDATA test ]]>")); - $dom->getElementById('foo')->appendChild(new \DOMCdataSection("]]>Foo<[![CDATA test ]]>")); - - $stream = fopen('php://temp', 'w'); - $r = new OutputRules($stream, $this->html5->getOptions()); - $t = new Traverser($dom, $stream, $r, $this->html5->getOptions()); - $list = $dom->getElementsByTagName('div'); - $r->cdata($list->item(0)->childNodes->item(0)); + $stream = fopen('php://temp', 'w'); + $r = new OutputRules($stream, $this->html5->getOptions()); + $t = new Traverser($dom, $stream, $r, $this->html5->getOptions()); + $list = $dom->getElementsByTagName('div'); + $r->cdata($list->item(0)->childNodes->item(0)); - $this->assertEquals('Foo<[![CDATA test ]]]]>]]>', stream_get_contents($stream, -1, 0)); - } + $this->assertEquals('Foo<[![CDATA test ]]]]>]]>', stream_get_contents($stream, - 1, 0)); + } - function testComment() { - $dom = $this->html5->loadHTML(' + public function testComment() + { + $dom = $this->html5->loadHTML('
'); - $stream = fopen('php://temp', 'w'); - $r = new OutputRules($stream, $this->html5->getOptions()); - $t = new Traverser($dom, $stream, $r, $this->html5->getOptions()); - - $list = $dom->getElementsByTagName('div'); - $r->comment($list->item(0)->childNodes->item(0)); - $this->assertEquals('', stream_get_contents($stream, -1, 0)); + $stream = fopen('php://temp', 'w'); + $r = new OutputRules($stream, $this->html5->getOptions()); + $t = new Traverser($dom, $stream, $r, $this->html5->getOptions()); + $list = $dom->getElementsByTagName('div'); + $r->comment($list->item(0)->childNodes->item(0)); + $this->assertEquals('', stream_get_contents($stream, - 1, 0)); - $dom = $this->html5->loadHTML(' + $dom = $this->html5->loadHTML('
'); - $dom->getElementById('foo')->appendChild(new \DOMComment(' --> Foo -->')); + $dom->getElementById('foo')->appendChild(new \DOMComment(' --> Foo -->')); - $stream = fopen('php://temp', 'w'); - $r = new OutputRules($stream, $this->html5->getOptions()); - $t = new Traverser($dom, $stream, $r, $this->html5->getOptions()); + $stream = fopen('php://temp', 'w'); + $r = new OutputRules($stream, $this->html5->getOptions()); + $t = new Traverser($dom, $stream, $r, $this->html5->getOptions()); - $list = $dom->getElementsByTagName('div'); - $r->comment($list->item(0)->childNodes->item(0)); + $list = $dom->getElementsByTagName('div'); + $r->comment($list->item(0)->childNodes->item(0)); - // Could not find more definitive guidelines on what this should be. Went with - // what the HTML5 spec says and what \DOMDocument::saveXML() produces. - $this->assertEquals(' --> Foo -->-->', stream_get_contents($stream, -1, 0)); - } + // Could not find more definitive guidelines on what this should be. Went with + // what the HTML5 spec says and what \DOMDocument::saveXML() produces. + $this->assertEquals(' --> Foo -->-->', stream_get_contents($stream, - 1, 0)); + } - function testText() { - $dom = $this->html5->loadHTML(' + public function testText() + { + $dom = $this->html5->loadHTML(' '); - $stream = fopen('php://temp', 'w'); - $r = new OutputRules($stream, $this->html5->getOptions()); - $t = new Traverser($dom, $stream, $r, $this->html5->getOptions()); + $stream = fopen('php://temp', 'w'); + $r = new OutputRules($stream, $this->html5->getOptions()); + $t = new Traverser($dom, $stream, $r, $this->html5->getOptions()); - $list = $dom->getElementsByTagName('script'); - $r->text($list->item(0)->childNodes->item(0)); - $this->assertEquals('baz();', stream_get_contents($stream, -1, 0)); + $list = $dom->getElementsByTagName('script'); + $r->text($list->item(0)->childNodes->item(0)); + $this->assertEquals('baz();', stream_get_contents($stream, - 1, 0)); - $dom = $this->html5->loadHTML(' + $dom = $this->html5->loadHTML(' '); - $dom->getElementById('foo')->appendChild(new \DOMText('')); - - $stream = fopen('php://temp', 'w'); - $r = new OutputRules($stream, $this->html5->getOptions()); - $t = new Traverser($dom, $stream, $r, $this->html5->getOptions()); - - $item = $dom->getElementById('foo'); - $r->text($item->firstChild); - $this->assertEquals('<script>alert("hi");</script>', stream_get_contents($stream, -1, 0)); - } - - function testNl() { - list($o, $s) = $this->getOutputRules(); - - $m = $this->getProtectedMethod('nl'); - $m->invoke($o); - $this->assertEquals(PHP_EOL, stream_get_contents($s, -1, 0)); - } - - function testWr() { - list($o, $s) = $this->getOutputRules(); - - $m = $this->getProtectedMethod('wr'); - $m->invoke($o, 'foo'); - $this->assertEquals('foo', stream_get_contents($s, -1, 0)); - } - - function getEncData(){ - return array( - array(FALSE, '&\'<>"', '&\'<>"', '&'<>"'), - array(FALSE, 'This + is. a < test', 'This + is. a < test', 'This + is. a < test'), - array(FALSE, '.+#', '.+#', '.+#'), - - array(TRUE, '.+#\'', '.+#\'', '.+#''), - array(TRUE, '&".<', '&".<', '&".<'), - array(TRUE, '&\'<>"', '&\'<>"', '&'<>"'), - array(TRUE, "\xc2\xa0\"'", ' "\'', ' "''), - ); - } - - /** - * Test basic encoding of text. - * @dataProvider getEncData - */ - function testEnc($isAttribute, $test, $expected, $expectedEncoded) { - - list($o, $s) = $this->getOutputRules(); - $m = $this->getProtectedMethod('enc'); - - $this->assertEquals($expected, $m->invoke($o, $test, $isAttribute)); - - list($o, $s) = $this->getOutputRules(array('encode_entities' => TRUE)); - $m = $this->getProtectedMethod('enc'); - $this->assertEquals($expectedEncoded, $m->invoke($o, $test, $isAttribute)); - } - - /** - * Test basic encoding of text. - * @dataProvider getEncData - */ - function testEscape($isAttribute, $test, $expected, $expectedEncoded) { - - list($o, $s) = $this->getOutputRules(); - $m = $this->getProtectedMethod('escape'); - - $this->assertEquals($expected, $m->invoke($o, $test, $isAttribute)); - } - - function testAttrs() { - $dom = $this->html5->loadHTML(' + $dom->getElementById('foo')->appendChild(new \DOMText('')); + + $stream = fopen('php://temp', 'w'); + $r = new OutputRules($stream, $this->html5->getOptions()); + $t = new Traverser($dom, $stream, $r, $this->html5->getOptions()); + + $item = $dom->getElementById('foo'); + $r->text($item->firstChild); + $this->assertEquals('<script>alert("hi");</script>', stream_get_contents($stream, - 1, 0)); + } + + public function testNl() + { + list ($o, $s) = $this->getOutputRules(); + + $m = $this->getProtectedMethod('nl'); + $m->invoke($o); + $this->assertEquals(PHP_EOL, stream_get_contents($s, - 1, 0)); + } + + public function testWr() + { + list ($o, $s) = $this->getOutputRules(); + + $m = $this->getProtectedMethod('wr'); + $m->invoke($o, 'foo'); + $this->assertEquals('foo', stream_get_contents($s, - 1, 0)); + } + + public function getEncData() + { + return array( + array( + FALSE, + '&\'<>"', + '&\'<>"', + '&'<>"' + ), + array( + FALSE, + 'This + is. a < test', + 'This + is. a < test', + 'This + is. a < test' + ), + array( + FALSE, + '.+#', + '.+#', + '.+#' + ), + + array( + TRUE, + '.+#\'', + '.+#\'', + '.+#'' + ), + array( + TRUE, + '&".<', + '&".<', + '&".<' + ), + array( + TRUE, + '&\'<>"', + '&\'<>"', + '&'<>"' + ), + array( + TRUE, + "\xc2\xa0\"'", + ' "\'', + ' "'' + ) + ); + } + + /** + * Test basic encoding of text. + * @dataProvider getEncData + */ + public function testEnc($isAttribute, $test, $expected, $expectedEncoded) + { + list ($o, $s) = $this->getOutputRules(); + $m = $this->getProtectedMethod('enc'); + + $this->assertEquals($expected, $m->invoke($o, $test, $isAttribute)); + + list ($o, $s) = $this->getOutputRules(array( + 'encode_entities' => TRUE + )); + $m = $this->getProtectedMethod('enc'); + $this->assertEquals($expectedEncoded, $m->invoke($o, $test, $isAttribute)); + } + + /** + * Test basic encoding of text. + * @dataProvider getEncData + */ + public function testEscape($isAttribute, $test, $expected, $expectedEncoded) + { + list ($o, $s) = $this->getOutputRules(); + $m = $this->getProtectedMethod('escape'); + + $this->assertEquals($expected, $m->invoke($o, $test, $isAttribute)); + } + + public function testAttrs() + { + $dom = $this->html5->loadHTML('
foo bar baz
'); - $stream = fopen('php://temp', 'w'); - $r = new OutputRules($stream, $this->html5->getOptions()); - $t = new Traverser($dom, $stream, $r, $this->html5->getOptions()); + $stream = fopen('php://temp', 'w'); + $r = new OutputRules($stream, $this->html5->getOptions()); + $t = new Traverser($dom, $stream, $r, $this->html5->getOptions()); - $list = $dom->getElementsByTagName('div'); + $list = $dom->getElementsByTagName('div'); - $m = $this->getProtectedMethod('attrs'); - $m->invoke($r, $list->item(0)); + $m = $this->getProtectedMethod('attrs'); + $m->invoke($r, $list->item(0)); - $content = stream_get_contents($stream, -1, 0); - $this->assertEquals(' id="foo" class="bar baz" disabled', $content); - } + $content = stream_get_contents($stream, - 1, 0); + $this->assertEquals(' id="foo" class="bar baz" disabled', $content); + } - function testSvg() { - $dom = $this->html5->loadHTML(' + public function testSvg() + { + $dom = $this->html5->loadHTML( + '
foo bar baz
@@ -377,20 +440,22 @@ class OutputRulesTest extends \Masterminds\HTML5\Tests\TestCase { '); - $stream = fopen('php://temp', 'w'); - $r = new OutputRules($stream, $this->html5->getOptions()); - $t = new Traverser($dom, $stream, $r, $this->html5->getOptions()); - - $list = $dom->getElementsByTagName('svg'); - $r->element($list->item(0)); - $contents = stream_get_contents($stream, -1, 0); - $this->assertRegExp('||', $contents); - $this->assertRegExp('||', $contents); - $this->assertRegExp('||', $contents); - } - - function testMath() { - $dom = $this->html5->loadHTML(' + $stream = fopen('php://temp', 'w'); + $r = new OutputRules($stream, $this->html5->getOptions()); + $t = new Traverser($dom, $stream, $r, $this->html5->getOptions()); + + $list = $dom->getElementsByTagName('svg'); + $r->element($list->item(0)); + $contents = stream_get_contents($stream, - 1, 0); + $this->assertRegExp('||', $contents); + $this->assertRegExp('||', $contents); + $this->assertRegExp('||', $contents); + } + + public function testMath() + { + $dom = $this->html5->loadHTML( + '
foo bar baz
@@ -404,26 +469,27 @@ class OutputRulesTest extends \Masterminds\HTML5\Tests\TestCase { '); - $stream = fopen('php://temp', 'w'); - $r = new OutputRules($stream, $this->html5->getOptions()); - $t = new Traverser($dom, $stream, $r, $this->html5->getOptions()); - - $list = $dom->getElementsByTagName('math'); - $r->element($list->item(0)); - $content = stream_get_contents($stream, -1, 0); - $this->assertRegExp('||', $content); - $this->assertRegExp('||', $content); - } - - function testProcessorInstruction() { - $dom = $this->html5->loadHTMLFragment(''); - - $stream = fopen('php://temp', 'w'); - $r = new OutputRules($stream, $this->html5->getOptions()); - $t = new Traverser($dom, $stream, $r, $this->html5->getOptions()); - - $r->processorInstruction($dom->firstChild); - $content = stream_get_contents($stream, -1, 0); - $this->assertRegExp('|<\?foo bar \?>|', $content); - } + $stream = fopen('php://temp', 'w'); + $r = new OutputRules($stream, $this->html5->getOptions()); + $t = new Traverser($dom, $stream, $r, $this->html5->getOptions()); + + $list = $dom->getElementsByTagName('math'); + $r->element($list->item(0)); + $content = stream_get_contents($stream, - 1, 0); + $this->assertRegExp('||', $content); + $this->assertRegExp('||', $content); + } + + public function testProcessorInstruction() + { + $dom = $this->html5->loadHTMLFragment(''); + + $stream = fopen('php://temp', 'w'); + $r = new OutputRules($stream, $this->html5->getOptions()); + $t = new Traverser($dom, $stream, $r, $this->html5->getOptions()); + + $r->processorInstruction($dom->firstChild); + $content = stream_get_contents($stream, - 1, 0); + $this->assertRegExp('|<\?foo bar \?>|', $content); + } } -- cgit v1.2.3