00001 <?php
00002
00003 class XmlTest extends PHPUnit_Framework_TestCase {
00004
00005 function testElementOpen() {
00006 $this->assertEquals(
00007 '<element>',
00008 Xml::element( 'element', null, null ),
00009 'Opening element with no attributes'
00010 );
00011 }
00012
00013 function testElementEmpty() {
00014 $this->assertEquals(
00015 '<element />',
00016 Xml::element( 'element', null, '' ),
00017 'Terminated empty element'
00018 );
00019 }
00020
00021 function testElementEscaping() {
00022 $this->assertEquals(
00023 '<element>hello <there> you & you</element>',
00024 Xml::element( 'element', null, 'hello <there> you & you' ),
00025 'Element with no attributes and content that needs escaping'
00026 );
00027 }
00028
00029 function testElementAttributes() {
00030 $this->assertEquals(
00031 '<element key="value" <>="<>">',
00032 Xml::element( 'element', array( 'key' => 'value', '<>' => '<>' ), null ),
00033 'Element attributes, keys are not escaped'
00034 );
00035 }
00036
00037 function testOpenElement() {
00038 $this->assertEquals(
00039 '<element k="v">',
00040 Xml::openElement( 'element', array( 'k' => 'v' ) ),
00041 'openElement() shortcut'
00042 );
00043 }
00044
00045 function testCloseElement() {
00046 $this->assertEquals( '</element>', Xml::closeElement( 'element' ), 'closeElement() shortcut' );
00047 }
00048
00049 #
00050 # textarea
00051 #
00052 function testTextareaNoContent() {
00053 $this->assertEquals(
00054 '<textarea name="name" id="name" cols="40" rows="5"></textarea>',
00055 Xml::textarea( 'name', '' ),
00056 'textarea() with not content'
00057 );
00058 }
00059
00060 function testTextareaAttribs() {
00061 $this->assertEquals(
00062 '<textarea name="name" id="name" cols="20" rows="10"><txt></textarea>',
00063 Xml::textarea( 'name', '<txt>', 20, 10 ),
00064 'textarea() with custom attribs'
00065 );
00066 }
00067
00068 #
00069 # JS
00070 #
00071 function testEscapeJsStringSpecialChars() {
00072 $this->assertEquals(
00073 '\\\\\r\n',
00074 Xml::escapeJsString( "\\\r\n" ),
00075 'escapeJsString() with special characters'
00076 );
00077 }
00078
00079 function testEncodeJsVarBoolean() {
00080 $this->assertEquals(
00081 'true',
00082 Xml::encodeJsVar( true ),
00083 'encodeJsVar() with boolean'
00084 );
00085 }
00086
00087 function testEncodeJsVarNull() {
00088 $this->assertEquals(
00089 'null',
00090 Xml::encodeJsVar( null ),
00091 'encodeJsVar() with null'
00092 );
00093 }
00094
00095 function testEncodeJsVarArray() {
00096 $this->assertEquals(
00097 '["a", 1]',
00098 Xml::encodeJsVar( array( 'a', 1 ) ),
00099 'encodeJsVar() with array'
00100 );
00101 $this->assertEquals(
00102 '{"a": "a", "b": 1}',
00103 Xml::encodeJsVar( array( 'a' => 'a', 'b' => 1 ) ),
00104 'encodeJsVar() with associative array'
00105 );
00106 }
00107
00108 function testEncodeJsVarObject() {
00109 $this->assertEquals(
00110 '{"a": "a", "b": 1}',
00111 Xml::encodeJsVar( (object)array( 'a' => 'a', 'b' => 1 ) ),
00112 'encodeJsVar() with object'
00113 );
00114 }
00115 }