format(array('message' => 'foo', 'priority' => 42)); $this->assertContains('foo', $line); $this->assertContains((string)42, $line); } public function testConfiguringElementMapping() { $f = new Zend_Log_Formatter_Xml('log', array('foo' => 'bar')); $line = $f->format(array('bar' => 'baz')); $this->assertContains('baz', $line); } public function testXmlDeclarationIsStripped() { $f = new Zend_Log_Formatter_Xml(); $line = $f->format(array('message' => 'foo', 'priority' => 42)); $this->assertNotContains('<\?xml version=', $line); } public function testXmlValidates() { $f = new Zend_Log_Formatter_Xml(); $line = $f->format(array('message' => 'foo', 'priority' => 42)); $sxml = @simplexml_load_string($line); $this->assertTrue($sxml instanceof SimpleXMLElement, 'Formatted XML is invalid'); } /** * @group ZF-2062 * @group ZF-4190 */ public function testHtmlSpecialCharsInMessageGetEscapedForValidXml() { $f = new Zend_Log_Formatter_Xml(); $line = $f->format(array('message' => '&key1=value1&key2=value2', 'priority' => 42)); $this->assertContains("&", $line); $this->assertTrue(substr_count($line, "&") == 2); } /** * @group ZF-2062 * @group ZF-4190 */ public function testFixingBrokenCharsSoXmlIsValid() { $f = new Zend_Log_Formatter_Xml(); $line = $f->format(array('message' => '&', 'priority' => 42)); $this->assertContains('&amp', $line); } public function testConstructorWithArray() { $options = array( 'rootElement' => 'log', 'elementMap' => array( 'word' => 'message', 'priority' => 'priority' ) ); $event = array( 'message' => 'tottakai', 'priority' => 4 ); $expected = 'tottakai4'; $formatter = new Zend_Log_Formatter_Xml($options); $output = $formatter->format($event); $this->assertContains($expected, $output); $this->assertEquals('UTF-8', $formatter->getEncoding()); } /** * @group ZF-9176 */ public function testFactory() { $options = array( 'rootElement' => 'log', 'elementMap' => array( 'timestamp' => 'timestamp', 'response' => 'message' ) ); $formatter = Zend_Log_Formatter_Xml::factory($options); $this->assertTrue($formatter instanceof Zend_Log_Formatter_Xml); } /** * @group ZF-11161 */ public function testNonScalarValuesAreExcludedFromFormattedString() { $options = array( 'rootElement' => 'log' ); $event = array( 'message' => 'tottakai', 'priority' => 4, 'context' => array('test'=>'one'), 'reference' => new Zend_Log_Formatter_Xml() ); $expected = 'tottakai4'; $formatter = new Zend_Log_Formatter_Xml($options); $output = $formatter->format($event); $this->assertContains($expected, $output); } /** * @group ZF-11161 */ public function testObjectsWithStringSerializationAreIncludedInFormattedString() { $options = array( 'rootElement' => 'log' ); $event = array( 'message' => 'tottakai', 'priority' => 4, 'context' => array('test'=>'one'), 'reference' => new Zend_Log_Formatter_XmlTest_SerializableObject() ); $expected = 'tottakai4Zend_Log_Formatter_XmlTest_SerializableObject'; $formatter = new Zend_Log_Formatter_Xml($options); $output = $formatter->format($event); $this->assertContains($expected, $output); } } class Zend_Log_Formatter_XmlTest_SerializableObject { public function __toString() { return __CLASS__; } } if (PHPUnit_MAIN_METHOD == 'Zend_Log_Formatter_XmlTest::main') { Zend_Log_Formatter_XmlTest::main(); }