2011-11-06 18:09:32 +01:00
|
|
|
<?php
|
|
|
|
|
2014-02-06 14:44:16 +01:00
|
|
|
namespace PhpParser\Serializer;
|
|
|
|
|
|
|
|
use PhpParser;
|
|
|
|
|
|
|
|
class XMLTest extends \PHPUnit_Framework_TestCase
|
2011-11-06 18:09:32 +01:00
|
|
|
{
|
2011-11-27 12:53:48 +01:00
|
|
|
/**
|
2014-02-06 14:44:16 +01:00
|
|
|
* @covers XML<extended>
|
2011-11-27 12:53:48 +01:00
|
|
|
*/
|
2011-11-06 18:09:32 +01:00
|
|
|
public function testSerialize() {
|
2011-12-18 13:04:27 +01:00
|
|
|
$code = <<<CODE
|
2011-11-06 18:09:32 +01:00
|
|
|
<?php
|
2012-05-06 17:49:04 +02:00
|
|
|
// comment
|
2011-11-27 21:43:27 +01:00
|
|
|
/** doc comment */
|
2011-12-18 13:04:27 +01:00
|
|
|
function functionName(&\$a = 0, \$b = 1.0) {
|
2011-11-27 21:43:27 +01:00
|
|
|
echo 'Foo';
|
2011-11-06 18:09:32 +01:00
|
|
|
}
|
|
|
|
CODE;
|
2011-11-27 21:43:27 +01:00
|
|
|
$xml = <<<XML
|
2011-11-06 18:09:32 +01:00
|
|
|
<?xml version="1.0" encoding="UTF-8"?>
|
2012-04-29 23:32:09 +02:00
|
|
|
<AST xmlns:node="http://nikic.github.com/PHPParser/XML/node" xmlns:subNode="http://nikic.github.com/PHPParser/XML/subNode" xmlns:attribute="http://nikic.github.com/PHPParser/XML/attribute" xmlns:scalar="http://nikic.github.com/PHPParser/XML/scalar">
|
|
|
|
<scalar:array>
|
|
|
|
<node:Stmt_Function>
|
2012-05-06 17:49:04 +02:00
|
|
|
<attribute:comments>
|
|
|
|
<scalar:array>
|
2012-06-06 15:33:38 +02:00
|
|
|
<comment isDocComment="false" line="2">// comment
|
2012-05-06 17:49:04 +02:00
|
|
|
</comment>
|
2012-06-06 15:33:38 +02:00
|
|
|
<comment isDocComment="true" line="3">/** doc comment */</comment>
|
2012-05-06 17:49:04 +02:00
|
|
|
</scalar:array>
|
|
|
|
</attribute:comments>
|
2012-05-05 17:34:27 +02:00
|
|
|
<attribute:startLine>
|
2012-05-06 17:49:04 +02:00
|
|
|
<scalar:int>4</scalar:int>
|
2012-05-05 17:34:27 +02:00
|
|
|
</attribute:startLine>
|
|
|
|
<attribute:endLine>
|
2012-05-06 17:49:04 +02:00
|
|
|
<scalar:int>6</scalar:int>
|
2012-05-05 17:34:27 +02:00
|
|
|
</attribute:endLine>
|
2012-04-29 23:32:09 +02:00
|
|
|
<subNode:byRef>
|
|
|
|
<scalar:false/>
|
|
|
|
</subNode:byRef>
|
|
|
|
<subNode:params>
|
|
|
|
<scalar:array>
|
|
|
|
<node:Param>
|
2012-05-05 17:34:27 +02:00
|
|
|
<attribute:startLine>
|
2012-05-06 17:49:04 +02:00
|
|
|
<scalar:int>4</scalar:int>
|
2012-05-05 17:34:27 +02:00
|
|
|
</attribute:startLine>
|
|
|
|
<attribute:endLine>
|
2012-05-06 17:49:04 +02:00
|
|
|
<scalar:int>4</scalar:int>
|
2012-05-05 17:34:27 +02:00
|
|
|
</attribute:endLine>
|
2012-04-29 23:32:09 +02:00
|
|
|
<subNode:name>
|
|
|
|
<scalar:string>a</scalar:string>
|
|
|
|
</subNode:name>
|
|
|
|
<subNode:default>
|
|
|
|
<node:Scalar_LNumber>
|
2012-05-05 17:34:27 +02:00
|
|
|
<attribute:startLine>
|
2012-05-06 17:49:04 +02:00
|
|
|
<scalar:int>4</scalar:int>
|
2012-05-05 17:34:27 +02:00
|
|
|
</attribute:startLine>
|
|
|
|
<attribute:endLine>
|
2012-05-06 17:49:04 +02:00
|
|
|
<scalar:int>4</scalar:int>
|
2012-05-05 17:34:27 +02:00
|
|
|
</attribute:endLine>
|
2012-04-29 23:32:09 +02:00
|
|
|
<subNode:value>
|
|
|
|
<scalar:int>0</scalar:int>
|
|
|
|
</subNode:value>
|
|
|
|
</node:Scalar_LNumber>
|
|
|
|
</subNode:default>
|
|
|
|
<subNode:type>
|
|
|
|
<scalar:null/>
|
|
|
|
</subNode:type>
|
2011-11-06 18:09:32 +01:00
|
|
|
<subNode:byRef>
|
2012-04-29 23:32:09 +02:00
|
|
|
<scalar:true/>
|
2011-11-06 18:09:32 +01:00
|
|
|
</subNode:byRef>
|
2012-04-29 23:32:09 +02:00
|
|
|
</node:Param>
|
|
|
|
<node:Param>
|
2012-05-05 17:34:27 +02:00
|
|
|
<attribute:startLine>
|
2012-05-06 17:49:04 +02:00
|
|
|
<scalar:int>4</scalar:int>
|
2012-05-05 17:34:27 +02:00
|
|
|
</attribute:startLine>
|
|
|
|
<attribute:endLine>
|
2012-05-06 17:49:04 +02:00
|
|
|
<scalar:int>4</scalar:int>
|
2012-05-05 17:34:27 +02:00
|
|
|
</attribute:endLine>
|
2011-11-27 21:43:27 +01:00
|
|
|
<subNode:name>
|
2012-04-29 23:32:09 +02:00
|
|
|
<scalar:string>b</scalar:string>
|
2011-11-27 21:43:27 +01:00
|
|
|
</subNode:name>
|
2012-04-29 23:32:09 +02:00
|
|
|
<subNode:default>
|
|
|
|
<node:Scalar_DNumber>
|
2012-05-05 17:34:27 +02:00
|
|
|
<attribute:startLine>
|
2012-05-06 17:49:04 +02:00
|
|
|
<scalar:int>4</scalar:int>
|
2012-05-05 17:34:27 +02:00
|
|
|
</attribute:startLine>
|
|
|
|
<attribute:endLine>
|
2012-05-06 17:49:04 +02:00
|
|
|
<scalar:int>4</scalar:int>
|
2012-05-05 17:34:27 +02:00
|
|
|
</attribute:endLine>
|
2012-04-29 23:32:09 +02:00
|
|
|
<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>
|
2012-05-05 17:34:27 +02:00
|
|
|
<attribute:startLine>
|
2012-05-06 17:49:04 +02:00
|
|
|
<scalar:int>5</scalar:int>
|
2012-05-05 17:34:27 +02:00
|
|
|
</attribute:startLine>
|
|
|
|
<attribute:endLine>
|
2012-05-06 17:49:04 +02:00
|
|
|
<scalar:int>5</scalar:int>
|
2012-05-05 17:34:27 +02:00
|
|
|
</attribute:endLine>
|
2012-04-29 23:32:09 +02:00
|
|
|
<subNode:exprs>
|
|
|
|
<scalar:array>
|
|
|
|
<node:Scalar_String>
|
2012-05-05 17:34:27 +02:00
|
|
|
<attribute:startLine>
|
2012-05-06 17:49:04 +02:00
|
|
|
<scalar:int>5</scalar:int>
|
2012-05-05 17:34:27 +02:00
|
|
|
</attribute:startLine>
|
|
|
|
<attribute:endLine>
|
2012-05-06 17:49:04 +02:00
|
|
|
<scalar:int>5</scalar:int>
|
2012-05-05 17:34:27 +02:00
|
|
|
</attribute:endLine>
|
2012-04-29 23:32:09 +02:00
|
|
|
<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>
|
2011-11-06 18:09:32 +01:00
|
|
|
</AST>
|
|
|
|
XML;
|
|
|
|
|
2014-02-06 14:44:16 +01:00
|
|
|
$parser = new PhpParser\Parser(new PhpParser\Lexer);
|
|
|
|
$serializer = new XML;
|
2011-11-06 18:09:32 +01:00
|
|
|
|
2012-04-25 20:04:46 +02:00
|
|
|
$stmts = $parser->parse($code);
|
2011-11-06 18:09:32 +01:00
|
|
|
$this->assertXmlStringEqualsXmlString($xml, $serializer->serialize($stmts));
|
|
|
|
}
|
2011-11-27 21:43:27 +01:00
|
|
|
|
|
|
|
/**
|
2014-02-06 14:44:16 +01:00
|
|
|
* @expectedException \InvalidArgumentException
|
2011-11-27 21:43:27 +01:00
|
|
|
* @expectedExceptionMessage Unexpected node type
|
|
|
|
*/
|
|
|
|
public function testError() {
|
2014-02-06 14:44:16 +01:00
|
|
|
$serializer = new XML;
|
|
|
|
$serializer->serialize(array(new \stdClass));
|
2011-11-27 21:43:27 +01:00
|
|
|
}
|
2011-11-06 18:09:32 +01:00
|
|
|
}
|