From 8c2bf0373c35d363f69ef8ddd94bd30080e22481 Mon Sep 17 00:00:00 2001 From: nikic Date: Sun, 27 Nov 2011 21:43:27 +0100 Subject: [PATCH] Some more test improvements (+ fixes) --- lib/PHPParser/Error.php | 4 +- lib/PHPParser/Serializer/XML.php | 2 +- lib/PHPParser/Unserializer/XML.php | 2 +- test/PHPParser/Tests/ErrorTest.php | 33 +++++ test/PHPParser/Tests/NodeAbstractTest.php | 43 ++++++ test/PHPParser/Tests/NodeDumperTest.php | 13 +- .../Tests/NodeVisitor/NameResolverTest.php | 2 +- test/PHPParser/Tests/Serializer/XMLTest.php | 135 +++++++++-------- test/PHPParser/Tests/Unserializer/XMLTest.php | 140 ++++++++++-------- test/PHPParser/Tests/codeTest.php | 25 ++-- test/code/stmt/haltCompiler.test | 31 ++++ test/code/stmt/haltCompiler1.test | 17 --- test/code/stmt/haltCompiler2.test | 15 -- .../stmt/haltCompilerInvalidSyntax.test-fail | 6 + .../stmt/haltCompilerOutermostScope.test-fail | 8 + test/code/stmt/namespace/alias.test | 28 ++++ 16 files changed, 333 insertions(+), 171 deletions(-) create mode 100644 test/PHPParser/Tests/ErrorTest.php create mode 100644 test/PHPParser/Tests/NodeAbstractTest.php create mode 100644 test/code/stmt/haltCompiler.test delete mode 100644 test/code/stmt/haltCompiler1.test delete mode 100644 test/code/stmt/haltCompiler2.test create mode 100644 test/code/stmt/haltCompilerInvalidSyntax.test-fail create mode 100644 test/code/stmt/haltCompilerOutermostScope.test-fail diff --git a/lib/PHPParser/Error.php b/lib/PHPParser/Error.php index 0ef71f5..ba248f8 100644 --- a/lib/PHPParser/Error.php +++ b/lib/PHPParser/Error.php @@ -23,7 +23,7 @@ class PHPParser_Error extends RuntimeException * @return string Error message */ public function getRawMessage() { - return $this->message; + return $this->rawMessage; } /** @@ -32,7 +32,7 @@ class PHPParser_Error extends RuntimeException * @param string $message Error message */ public function setRawMessage($message) { - $this->message = (string) $message; + $this->rawMessage = (string) $message; $this->updateMessage(); } diff --git a/lib/PHPParser/Serializer/XML.php b/lib/PHPParser/Serializer/XML.php index 1c2a1ce..4718944 100644 --- a/lib/PHPParser/Serializer/XML.php +++ b/lib/PHPParser/Serializer/XML.php @@ -68,7 +68,7 @@ class PHPParser_Serializer_XML implements PHPParser_Serializer } elseif (null === $node) { $this->writer->writeElement('scalar:null'); } else { - throw new Exception('Unexpected node type'); + throw new InvalidArgumentException('Unexpected node type'); } } } \ No newline at end of file diff --git a/lib/PHPParser/Unserializer/XML.php b/lib/PHPParser/Unserializer/XML.php index f1e6236..fb62fca 100644 --- a/lib/PHPParser/Unserializer/XML.php +++ b/lib/PHPParser/Unserializer/XML.php @@ -13,7 +13,7 @@ class PHPParser_Unserializer_XML implements PHPParser_Unserializer $this->reader->read(); if ('AST' !== $this->reader->name) { - throw new Exception('AST root element not found'); + throw new DomainException('AST root element not found'); } return $this->read(); diff --git a/test/PHPParser/Tests/ErrorTest.php b/test/PHPParser/Tests/ErrorTest.php new file mode 100644 index 0000000..de21cdd --- /dev/null +++ b/test/PHPParser/Tests/ErrorTest.php @@ -0,0 +1,33 @@ +assertEquals('Some error', $error->getRawMessage()); + $this->assertEquals(10, $error->getRawLine()); + $this->assertEquals('Some error on line 10', $error->getMessage()); + + return $error; + } + + /** + * @depends testConstruct + */ + public function testSetMessageAndLine(PHPParser_Error $error) { + $error->setRawMessage('Some other error'); + $error->setRawLine(15); + + $this->assertEquals('Some other error', $error->getRawMessage()); + $this->assertEquals(15, $error->getRawLine()); + $this->assertEquals('Some other error on line 15', $error->getMessage()); + } + + public function testUnknownLine() { + $error = new PHPParser_Error('Some error'); + + $this->assertEquals(-1, $error->getRawLine()); + $this->assertEquals('Some error on unknown line', $error->getMessage()); + } +} \ No newline at end of file diff --git a/test/PHPParser/Tests/NodeAbstractTest.php b/test/PHPParser/Tests/NodeAbstractTest.php new file mode 100644 index 0000000..efb1136 --- /dev/null +++ b/test/PHPParser/Tests/NodeAbstractTest.php @@ -0,0 +1,43 @@ +getMockForAbstractClass( + 'PHPParser_NodeAbstract', + array( + array( + 'subNode' => 'value' + ), + 10, + '/** doc comment */' + ), + 'PHPParser_Node_Dummy' + ); + + $this->assertEquals(10, $node->getLine()); + $this->assertEquals('/** doc comment */', $node->getDocComment()); + $this->assertEquals('Dummy', $node->getType()); + $this->assertEquals('value', $node->subNode); + $this->assertTrue(isset($node->subNode)); + + return $node; + } + + /** + * @depends testConstruct + */ + public function testChange(PHPParser_Node $node) { + $node->setLine(15); + $this->assertEquals(15, $node->getLine()); + + $node->setDocComment('/** other doc comment */'); + $this->assertEquals('/** other doc comment */', $node->getDocComment()); + + $node->subNode = 'newValue'; + $this->assertEquals('newValue', $node->subNode); + + unset($node->subNode); + $this->assertFalse(isset($node->subNode)); + } +} \ No newline at end of file diff --git a/test/PHPParser/Tests/NodeDumperTest.php b/test/PHPParser/Tests/NodeDumperTest.php index c2a5746..5ea29d5 100644 --- a/test/PHPParser/Tests/NodeDumperTest.php +++ b/test/PHPParser/Tests/NodeDumperTest.php @@ -7,9 +7,9 @@ class PHPParser_Tests_NodeDumperTest extends PHPUnit_Framework_TestCase * @covers PHPParser_NodeDumper::dump */ public function testDump($node, $dump) { - $nodeDumper = new PHPParser_NodeDumper; + $dumper = new PHPParser_NodeDumper; - $this->assertEquals($dump, $nodeDumper->dump($node)); + $this->assertEquals($dump, $dumper->dump($node)); } public function provideTestDump() { @@ -54,4 +54,13 @@ class PHPParser_Tests_NodeDumperTest extends PHPUnit_Framework_TestCase ), ); } + + /** + * @expectedException InvalidArgumentException + * @expectedExceptionMessage Can only dump nodes and arrays. + */ + public function testError() { + $dumper = new PHPParser_NodeDumper; + $dumper->dump(new stdClass); + } } \ No newline at end of file diff --git a/test/PHPParser/Tests/NodeVisitor/NameResolverTest.php b/test/PHPParser/Tests/NodeVisitor/NameResolverTest.php index 7066651..ee98fc8 100644 --- a/test/PHPParser/Tests/NodeVisitor/NameResolverTest.php +++ b/test/PHPParser/Tests/NodeVisitor/NameResolverTest.php @@ -127,7 +127,7 @@ EOC; */ public function testAddTraitNamespacedName() { if (!version_compare(PHP_VERSION, '5.4.0RC1', '>=')) { - $this->markTestSkipped('The test require PHP 5.4'); + $this->markTestSkipped('The test requires PHP 5.4'); } $code = << - - - - - - - - - - b - - - - - - - - C - - - - - - - - - + + - + - - - - - - - - - - - Foo - - - - - Bar - - - - - - - - - A - - - + + + + + a + + + + + 0 + + + + + + + + + + + + + b + + + + + 1 + + + + + + + + + + + + + + + + + + + + Foo + + + + + + + + + functionName + + + XML; @@ -82,4 +90,13 @@ XML; $stmts = $parser->parse(new PHPParser_Lexer($code)); $this->assertXmlStringEqualsXmlString($xml, $serializer->serialize($stmts)); } + + /** + * @expectedException InvalidArgumentException + * @expectedExceptionMessage Unexpected node type + */ + public function testError() { + $serializer = new PHPParser_Serializer_XML; + $serializer->serialize(array(new stdClass)); + } } \ No newline at end of file diff --git a/test/PHPParser/Tests/Unserializer/XMLTest.php b/test/PHPParser/Tests/Unserializer/XMLTest.php index cee93d4..ac92e5f 100644 --- a/test/PHPParser/Tests/Unserializer/XMLTest.php +++ b/test/PHPParser/Tests/Unserializer/XMLTest.php @@ -6,73 +6,81 @@ class PHPParser_Tests_Unserializer_XMLTest extends PHPUnit_Framework_TestCase * @covers PHPParser_Unserializer_XML */ public function testUnserialize() { - $xml = <<<'XML' + $xml = << - - - - - - - - - - b - - - - - - - - C - - - - - - - - - + + - + - - - - - - - - - - - Foo - - - - - Bar - - - - - - - - - A - - - + + + + + a + + + + + 0 + + + + + + + + + + + + + b + + + + + 1 + + + + + + + + + + + + + + + + + + + + Foo + + + + + + + + + functionName + + + XML; $code = <<<'CODE' -function A($b = C) +/** doc comment */ +function functionName(&$a = 0, $b = 1.0) { - echo 'Foo', 'Bar'; + echo 'Foo'; } CODE; @@ -82,4 +90,18 @@ CODE; $stmts = $unserializer->unserialize($xml); $this->assertEquals($code, $prettyPrinter->prettyPrint($stmts), '', 0, 10, true); } + + /** + * @expectedException DomainException + * @expectedExceptionMessage AST root element not found + */ + public function testWrongRootElementError() { + $xml = << + +XML; + + $unserializer = new PHPParser_Unserializer_XML; + $unserializer->unserialize($xml); + } } \ No newline at end of file diff --git a/test/PHPParser/Tests/codeTest.php b/test/PHPParser/Tests/codeTest.php index a28cf00..11fd0b4 100644 --- a/test/PHPParser/Tests/codeTest.php +++ b/test/PHPParser/Tests/codeTest.php @@ -37,22 +37,10 @@ class PHPParser_Tests_codeTest extends PHPUnit_Framework_TestCase } public function provideTestCodeFail() { - $preTests = $this->getTests('test-fail'); - - $tests = array(); - foreach ($preTests as $preTest) { - $name = array_shift($preTest); - foreach (array_chunk($preTest, 2) as $chunk) { - $tests[] = array($name, $chunk[0], $chunk[1]); - } - } - - return $tests; + return $this->getTests('test-fail'); } protected function getTests($ext) { - $tests = array(); - $it = new RecursiveDirectoryIterator(dirname(__FILE__) . '/../../code'); $it = new RecursiveIteratorIterator($it, RecursiveIteratorIterator::LEAVES_ONLY); @@ -63,6 +51,7 @@ class PHPParser_Tests_codeTest extends PHPUnit_Framework_TestCase $it = new RegexIterator($it, '~\.' . $ext . '$~'); } + $tests = array(); foreach ($it as $file) { // read file $fileContents = file_get_contents($file); @@ -71,7 +60,15 @@ class PHPParser_Tests_codeTest extends PHPUnit_Framework_TestCase $fileContents = preg_replace('/@@\{(.*?)\}@@/e', '$1', $fileContents); // parse sections - $tests[] = array_map('trim', explode('-----', $fileContents)); + $parts = array_map('trim', explode('-----', $fileContents)); + + // first part is the name + $name = array_shift($parts); + + // multiple sections possible with always two forming a pair + foreach (array_chunk($parts, 2) as $chunk) { + $tests[] = array($name, $chunk[0], $chunk[1]); + } } return $tests; diff --git a/test/code/stmt/haltCompiler.test b/test/code/stmt/haltCompiler.test new file mode 100644 index 0000000..3ef87c3 --- /dev/null +++ b/test/code/stmt/haltCompiler.test @@ -0,0 +1,31 @@ +__halt_compiler +----- + +Hallo World! +----- +array( + 0: Expr_Variable( + name: a + ) + 1: Stmt_HaltCompiler( + remaining: Hallo World! + ) +) +----- + -Hallo World! ------ -array( - 0: Expr_Variable( - name: a - ) - 1: Stmt_HaltCompiler( - remaining: Hallo World! - ) -) \ No newline at end of file diff --git a/test/code/stmt/haltCompiler2.test b/test/code/stmt/haltCompiler2.test deleted file mode 100644 index 1dddd0e..0000000 --- a/test/code/stmt/haltCompiler2.test +++ /dev/null @@ -1,15 +0,0 @@ -__halt_compiler (2) ------ -