mirror of
https://github.com/phabelio/PHP-Parser.git
synced 2024-11-27 04:24:43 +01:00
Some more test improvements (+ fixes)
This commit is contained in:
parent
3a4cc1a0f0
commit
8c2bf0373c
@ -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();
|
||||
}
|
||||
|
||||
|
@ -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');
|
||||
}
|
||||
}
|
||||
}
|
@ -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();
|
||||
|
33
test/PHPParser/Tests/ErrorTest.php
Normal file
33
test/PHPParser/Tests/ErrorTest.php
Normal file
@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
class PHPParser_Tests_ErrorTest extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function testConstruct() {
|
||||
$error = new PHPParser_Error('Some error', 10);
|
||||
|
||||
$this->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());
|
||||
}
|
||||
}
|
43
test/PHPParser/Tests/NodeAbstractTest.php
Normal file
43
test/PHPParser/Tests/NodeAbstractTest.php
Normal file
@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
class PHPParser_Tests_NodeAbstractTest extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function testConstruct() {
|
||||
$node = $this->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));
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
@ -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 = <<<EOC
|
||||
|
@ -8,71 +8,79 @@ class PHPParser_Tests_Serializer_XMLTest extends PHPUnit_Framework_TestCase
|
||||
public function testSerialize() {
|
||||
$code = <<<'CODE'
|
||||
<?php
|
||||
function A($b = C) {
|
||||
echo 'Foo', 'Bar';
|
||||
/** doc comment */
|
||||
function functionName(&$a = 0, $b = 1.0) {
|
||||
echo 'Foo';
|
||||
}
|
||||
CODE;
|
||||
$xml = <<<'XML'
|
||||
$xml = <<<XML
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<AST xmlns:node="http://nikic.github.com/PHPParser/XML/node" xmlns:subNode="http://nikic.github.com/PHPParser/XML/subNode" xmlns:scalar="http://nikic.github.com/PHPParser/XML/scalar">
|
||||
<scalar:array>
|
||||
<node:Stmt_Function line="2">
|
||||
<subNode:byRef>
|
||||
<scalar:false/>
|
||||
</subNode:byRef>
|
||||
<subNode:params>
|
||||
<scalar:array>
|
||||
<node:Param line="2">
|
||||
<subNode:name>
|
||||
<scalar:string>b</scalar:string>
|
||||
</subNode:name>
|
||||
<subNode:default>
|
||||
<node:Expr_ConstFetch line="2">
|
||||
<subNode:name>
|
||||
<node:Name line="2">
|
||||
<subNode:parts>
|
||||
<scalar:array>
|
||||
<scalar:string>C</scalar:string>
|
||||
</scalar:array>
|
||||
</subNode:parts>
|
||||
</node:Name>
|
||||
</subNode:name>
|
||||
</node:Expr_ConstFetch>
|
||||
</subNode:default>
|
||||
<subNode:type>
|
||||
<scalar:null/>
|
||||
</subNode:type>
|
||||
<scalar:array>
|
||||
<node:Stmt_Function line="3" docComment="/** doc comment */">
|
||||
<subNode:byRef>
|
||||
<scalar:false/>
|
||||
<scalar:false/>
|
||||
</subNode:byRef>
|
||||
</node:Param>
|
||||
</scalar:array>
|
||||
</subNode:params>
|
||||
<subNode:stmts>
|
||||
<scalar:array>
|
||||
<node:Stmt_Echo line="3">
|
||||
<subNode:exprs>
|
||||
<scalar:array>
|
||||
<node:Scalar_String line="3">
|
||||
<subNode:value>
|
||||
<scalar:string>Foo</scalar:string>
|
||||
</subNode:value>
|
||||
</node:Scalar_String>
|
||||
<node:Scalar_String line="3">
|
||||
<subNode:value>
|
||||
<scalar:string>Bar</scalar:string>
|
||||
</subNode:value>
|
||||
</node:Scalar_String>
|
||||
</scalar:array>
|
||||
</subNode:exprs>
|
||||
</node:Stmt_Echo>
|
||||
</scalar:array>
|
||||
</subNode:stmts>
|
||||
<subNode:name>
|
||||
<scalar:string>A</scalar:string>
|
||||
</subNode:name>
|
||||
</node:Stmt_Function>
|
||||
</scalar:array>
|
||||
<subNode:params>
|
||||
<scalar:array>
|
||||
<node:Param line="3">
|
||||
<subNode:name>
|
||||
<scalar:string>a</scalar:string>
|
||||
</subNode:name>
|
||||
<subNode:default>
|
||||
<node:Scalar_LNumber line="3">
|
||||
<subNode:value>
|
||||
<scalar:int>0</scalar:int>
|
||||
</subNode:value>
|
||||
</node:Scalar_LNumber>
|
||||
</subNode:default>
|
||||
<subNode:type>
|
||||
<scalar:null/>
|
||||
</subNode:type>
|
||||
<subNode:byRef>
|
||||
<scalar:true/>
|
||||
</subNode:byRef>
|
||||
</node:Param>
|
||||
<node:Param line="3">
|
||||
<subNode:name>
|
||||
<scalar:string>b</scalar:string>
|
||||
</subNode:name>
|
||||
<subNode:default>
|
||||
<node:Scalar_DNumber line="3">
|
||||
<subNode:value>
|
||||
<scalar:float>1</scalar:float>
|
||||
</subNode:value>
|
||||
</node:Scalar_DNumber>
|
||||
</subNode:default>
|
||||
<subNode:type>
|
||||
<scalar:null/>
|
||||
</subNode:type>
|
||||
<subNode:byRef>
|
||||
<scalar:false/>
|
||||
</subNode:byRef>
|
||||
</node:Param>
|
||||
</scalar:array>
|
||||
</subNode:params>
|
||||
<subNode:stmts>
|
||||
<scalar:array>
|
||||
<node:Stmt_Echo line="4">
|
||||
<subNode:exprs>
|
||||
<scalar:array>
|
||||
<node:Scalar_String line="4">
|
||||
<subNode:value>
|
||||
<scalar:string>Foo</scalar:string>
|
||||
</subNode:value>
|
||||
</node:Scalar_String>
|
||||
</scalar:array>
|
||||
</subNode:exprs>
|
||||
</node:Stmt_Echo>
|
||||
</scalar:array>
|
||||
</subNode:stmts>
|
||||
<subNode:name>
|
||||
<scalar:string>functionName</scalar:string>
|
||||
</subNode:name>
|
||||
</node:Stmt_Function>
|
||||
</scalar:array>
|
||||
</AST>
|
||||
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));
|
||||
}
|
||||
}
|
@ -6,73 +6,81 @@ class PHPParser_Tests_Unserializer_XMLTest extends PHPUnit_Framework_TestCase
|
||||
* @covers PHPParser_Unserializer_XML<extended>
|
||||
*/
|
||||
public function testUnserialize() {
|
||||
$xml = <<<'XML'
|
||||
$xml = <<<XML
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<AST xmlns:node="http://nikic.github.com/PHPParser/XML/node" xmlns:subNode="http://nikic.github.com/PHPParser/XML/subNode" xmlns:scalar="http://nikic.github.com/PHPParser/XML/scalar">
|
||||
<scalar:array>
|
||||
<node:Stmt_Function line="2">
|
||||
<subNode:byRef>
|
||||
<scalar:false/>
|
||||
</subNode:byRef>
|
||||
<subNode:params>
|
||||
<scalar:array>
|
||||
<node:Param line="2">
|
||||
<subNode:name>
|
||||
<scalar:string>b</scalar:string>
|
||||
</subNode:name>
|
||||
<subNode:default>
|
||||
<node:Expr_ConstFetch line="2">
|
||||
<subNode:name>
|
||||
<node:Name line="2">
|
||||
<subNode:parts>
|
||||
<scalar:array>
|
||||
<scalar:string>C</scalar:string>
|
||||
</scalar:array>
|
||||
</subNode:parts>
|
||||
</node:Name>
|
||||
</subNode:name>
|
||||
</node:Expr_ConstFetch>
|
||||
</subNode:default>
|
||||
<subNode:type>
|
||||
<scalar:null/>
|
||||
</subNode:type>
|
||||
<scalar:array>
|
||||
<node:Stmt_Function line="3" docComment="/** doc comment */">
|
||||
<subNode:byRef>
|
||||
<scalar:false/>
|
||||
<scalar:false/>
|
||||
</subNode:byRef>
|
||||
</node:Param>
|
||||
</scalar:array>
|
||||
</subNode:params>
|
||||
<subNode:stmts>
|
||||
<scalar:array>
|
||||
<node:Stmt_Echo line="3">
|
||||
<subNode:exprs>
|
||||
<scalar:array>
|
||||
<node:Scalar_String line="3">
|
||||
<subNode:value>
|
||||
<scalar:string>Foo</scalar:string>
|
||||
</subNode:value>
|
||||
</node:Scalar_String>
|
||||
<node:Scalar_String line="3">
|
||||
<subNode:value>
|
||||
<scalar:string>Bar</scalar:string>
|
||||
</subNode:value>
|
||||
</node:Scalar_String>
|
||||
</scalar:array>
|
||||
</subNode:exprs>
|
||||
</node:Stmt_Echo>
|
||||
</scalar:array>
|
||||
</subNode:stmts>
|
||||
<subNode:name>
|
||||
<scalar:string>A</scalar:string>
|
||||
</subNode:name>
|
||||
</node:Stmt_Function>
|
||||
</scalar:array>
|
||||
<subNode:params>
|
||||
<scalar:array>
|
||||
<node:Param line="3">
|
||||
<subNode:name>
|
||||
<scalar:string>a</scalar:string>
|
||||
</subNode:name>
|
||||
<subNode:default>
|
||||
<node:Scalar_LNumber line="3">
|
||||
<subNode:value>
|
||||
<scalar:int>0</scalar:int>
|
||||
</subNode:value>
|
||||
</node:Scalar_LNumber>
|
||||
</subNode:default>
|
||||
<subNode:type>
|
||||
<scalar:null/>
|
||||
</subNode:type>
|
||||
<subNode:byRef>
|
||||
<scalar:true/>
|
||||
</subNode:byRef>
|
||||
</node:Param>
|
||||
<node:Param line="3">
|
||||
<subNode:name>
|
||||
<scalar:string>b</scalar:string>
|
||||
</subNode:name>
|
||||
<subNode:default>
|
||||
<node:Scalar_DNumber line="3">
|
||||
<subNode:value>
|
||||
<scalar:float>1</scalar:float>
|
||||
</subNode:value>
|
||||
</node:Scalar_DNumber>
|
||||
</subNode:default>
|
||||
<subNode:type>
|
||||
<scalar:null/>
|
||||
</subNode:type>
|
||||
<subNode:byRef>
|
||||
<scalar:false/>
|
||||
</subNode:byRef>
|
||||
</node:Param>
|
||||
</scalar:array>
|
||||
</subNode:params>
|
||||
<subNode:stmts>
|
||||
<scalar:array>
|
||||
<node:Stmt_Echo line="4">
|
||||
<subNode:exprs>
|
||||
<scalar:array>
|
||||
<node:Scalar_String line="4">
|
||||
<subNode:value>
|
||||
<scalar:string>Foo</scalar:string>
|
||||
</subNode:value>
|
||||
</node:Scalar_String>
|
||||
</scalar:array>
|
||||
</subNode:exprs>
|
||||
</node:Stmt_Echo>
|
||||
</scalar:array>
|
||||
</subNode:stmts>
|
||||
<subNode:name>
|
||||
<scalar:string>functionName</scalar:string>
|
||||
</subNode:name>
|
||||
</node:Stmt_Function>
|
||||
</scalar:array>
|
||||
</AST>
|
||||
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
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<notAST/>
|
||||
XML;
|
||||
|
||||
$unserializer = new PHPParser_Unserializer_XML;
|
||||
$unserializer->unserialize($xml);
|
||||
}
|
||||
}
|
@ -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;
|
||||
|
31
test/code/stmt/haltCompiler.test
Normal file
31
test/code/stmt/haltCompiler.test
Normal file
@ -0,0 +1,31 @@
|
||||
__halt_compiler
|
||||
-----
|
||||
<?php
|
||||
|
||||
$a;
|
||||
__halt_compiler()
|
||||
?>
|
||||
Hallo World!
|
||||
-----
|
||||
array(
|
||||
0: Expr_Variable(
|
||||
name: a
|
||||
)
|
||||
1: Stmt_HaltCompiler(
|
||||
remaining: Hallo World!
|
||||
)
|
||||
)
|
||||
-----
|
||||
<?php
|
||||
|
||||
$a;
|
||||
__halt_compiler();Hallo World!
|
||||
-----
|
||||
array(
|
||||
0: Expr_Variable(
|
||||
name: a
|
||||
)
|
||||
1: Stmt_HaltCompiler(
|
||||
remaining: Hallo World!
|
||||
)
|
||||
)
|
@ -1,17 +0,0 @@
|
||||
__halt_compiler (1)
|
||||
-----
|
||||
<?php
|
||||
|
||||
$a;
|
||||
__halt_compiler()
|
||||
?>
|
||||
Hallo World!
|
||||
-----
|
||||
array(
|
||||
0: Expr_Variable(
|
||||
name: a
|
||||
)
|
||||
1: Stmt_HaltCompiler(
|
||||
remaining: Hallo World!
|
||||
)
|
||||
)
|
@ -1,15 +0,0 @@
|
||||
__halt_compiler (2)
|
||||
-----
|
||||
<?php
|
||||
|
||||
$a;
|
||||
__halt_compiler();Hallo World!
|
||||
-----
|
||||
array(
|
||||
0: Expr_Variable(
|
||||
name: a
|
||||
)
|
||||
1: Stmt_HaltCompiler(
|
||||
remaining: Hallo World!
|
||||
)
|
||||
)
|
6
test/code/stmt/haltCompilerInvalidSyntax.test-fail
Normal file
6
test/code/stmt/haltCompilerInvalidSyntax.test-fail
Normal file
@ -0,0 +1,6 @@
|
||||
Invalid __halt_compiler() syntax
|
||||
-----
|
||||
<?php
|
||||
__halt_compiler()
|
||||
-----
|
||||
__halt_compiler must be followed by "();" on line 2
|
8
test/code/stmt/haltCompilerOutermostScope.test-fail
Normal file
8
test/code/stmt/haltCompilerOutermostScope.test-fail
Normal file
@ -0,0 +1,8 @@
|
||||
__halt_compiler can only be used from outermost scope
|
||||
-----
|
||||
<?php
|
||||
if (true) {
|
||||
__halt_compiler();
|
||||
}
|
||||
-----
|
||||
__halt_compiler() can only be used from the outermost scope on line 3
|
@ -5,6 +5,10 @@ Aliases (use)
|
||||
use A\B;
|
||||
use C\D as E;
|
||||
use F\G as H, J;
|
||||
|
||||
// evil alias notation - Do Not Use!
|
||||
use \A;
|
||||
use \A as B;
|
||||
-----
|
||||
array(
|
||||
0: Stmt_Use(
|
||||
@ -54,4 +58,28 @@ array(
|
||||
)
|
||||
)
|
||||
)
|
||||
3: Stmt_Use(
|
||||
uses: array(
|
||||
0: Stmt_UseUse(
|
||||
name: Name(
|
||||
parts: array(
|
||||
0: A
|
||||
)
|
||||
)
|
||||
alias: A
|
||||
)
|
||||
)
|
||||
)
|
||||
4: Stmt_Use(
|
||||
uses: array(
|
||||
0: Stmt_UseUse(
|
||||
name: Name(
|
||||
parts: array(
|
||||
0: A
|
||||
)
|
||||
)
|
||||
alias: B
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
Loading…
Reference in New Issue
Block a user