1
0
mirror of https://github.com/danog/PHP-Parser.git synced 2024-11-26 20:04:48 +01:00

Add tests for XML serialization

Also fix the invalid namespace URIs I used.
This commit is contained in:
nikic 2011-11-06 18:09:32 +01:00
parent d9452e611a
commit c94a4767df
3 changed files with 167 additions and 3 deletions

View File

@ -18,9 +18,9 @@ class PHPParser_Serializer_XML implements PHPParser_Serializer
$this->writer->startDocument('1.0', 'UTF-8'); $this->writer->startDocument('1.0', 'UTF-8');
$this->writer->startElement('AST'); $this->writer->startElement('AST');
$this->writer->writeAttribute('xmlns:node', 'PHPParser/node'); $this->writer->writeAttribute('xmlns:node', 'http://nikic.github.com/PHPParser/XML/node');
$this->writer->writeAttribute('xmlns:subNode', 'PHPParser/subNode'); $this->writer->writeAttribute('xmlns:subNode', 'http://nikic.github.com/PHPParser/XML/subNode');
$this->writer->writeAttribute('xmlns:scalar', 'PHPParser/scalar'); $this->writer->writeAttribute('xmlns:scalar', 'http://nikic.github.com/PHPParser/XML/scalar');
$this->_serialize($nodes); $this->_serialize($nodes);

View File

@ -0,0 +1,82 @@
<?php
class PHPParser_Tests_Serializer_XMLTest extends PHPUnit_Framework_TestCase
{
public function testSerialize() {
$code = <<<'CODE'
<?php
function A($b = C) {
echo 'Foo', 'Bar';
}
CODE;
$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>
<subNode:byRef>
<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>
</AST>
XML;
$parser = new PHPParser_Parser;
$serializer = new PHPParser_Serializer_XML;
$stmts = $parser->parse(new PHPParser_Lexer($code));
$this->assertXmlStringEqualsXmlString($xml, $serializer->serialize($stmts));
}
}

View File

@ -0,0 +1,82 @@
<?php
class PHPParser_Tests_Unserializer_XMLTest extends PHPUnit_Framework_TestCase
{
public function testUnserialize() {
$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>
<subNode:byRef>
<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>
</AST>
XML;
$code = <<<'CODE'
function A($b = C)
{
echo 'Foo', 'Bar';
}
CODE;
$unserializer = new PHPParser_Unserializer_XML;
$prettyPrinter = new PHPParser_PrettyPrinter_Zend;
$stmts = $unserializer->unserialize($xml);
$this->assertEquals($code, $prettyPrinter->prettyPrint($stmts), '', 0, 10, true);
}
}