PageRenderTime 35ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/Zend/Log/Formatter/XmlTest.php

https://bitbucket.org/dbaltas/zend-framework-1.x-on-git
PHP | 194 lines | 125 code | 23 blank | 46 comment | 4 complexity | 856c51660217d0f6d5a08418882b3529 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.0, MIT
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Log
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: XmlTest.php 24593 2012-01-05 20:35:02Z matthew $
  21. */
  22. if (!defined('PHPUnit_MAIN_METHOD')) {
  23. define('PHPUnit_MAIN_METHOD', 'Zend_Log_Formatter_XmlTest::main');
  24. }
  25. /** Zend_Log_Formatter_Xml */
  26. require_once 'Zend/Log/Formatter/Xml.php';
  27. /**
  28. * @category Zend
  29. * @package Zend_Log
  30. * @subpackage UnitTests
  31. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  32. * @license http://framework.zend.com/license/new-bsd New BSD License
  33. * @group Zend_Log
  34. */
  35. class Zend_Log_Formatter_XmlTest extends PHPUnit_Framework_TestCase
  36. {
  37. public static function main()
  38. {
  39. $suite = new PHPUnit_Framework_TestSuite(__CLASS__);
  40. $result = PHPUnit_TextUI_TestRunner::run($suite);
  41. }
  42. public function testDefaultFormat()
  43. {
  44. $f = new Zend_Log_Formatter_Xml();
  45. $line = $f->format(array('message' => 'foo', 'priority' => 42));
  46. $this->assertContains('foo', $line);
  47. $this->assertContains((string)42, $line);
  48. }
  49. public function testConfiguringElementMapping()
  50. {
  51. $f = new Zend_Log_Formatter_Xml('log', array('foo' => 'bar'));
  52. $line = $f->format(array('bar' => 'baz'));
  53. $this->assertContains('<log><foo>baz</foo></log>', $line);
  54. }
  55. public function testXmlDeclarationIsStripped()
  56. {
  57. $f = new Zend_Log_Formatter_Xml();
  58. $line = $f->format(array('message' => 'foo', 'priority' => 42));
  59. $this->assertNotContains('<\?xml version=', $line);
  60. }
  61. public function testXmlValidates()
  62. {
  63. $f = new Zend_Log_Formatter_Xml();
  64. $line = $f->format(array('message' => 'foo', 'priority' => 42));
  65. $sxml = @simplexml_load_string($line);
  66. $this->assertType('SimpleXMLElement', $sxml, 'Formatted XML is invalid');
  67. }
  68. /**
  69. * @group ZF-2062
  70. * @group ZF-4190
  71. */
  72. public function testHtmlSpecialCharsInMessageGetEscapedForValidXml()
  73. {
  74. $f = new Zend_Log_Formatter_Xml();
  75. $line = $f->format(array('message' => '&key1=value1&key2=value2', 'priority' => 42));
  76. $this->assertContains("&amp;", $line);
  77. $this->assertTrue(substr_count($line, "&amp;") == 2);
  78. }
  79. /**
  80. * @group ZF-2062
  81. * @group ZF-4190
  82. */
  83. public function testFixingBrokenCharsSoXmlIsValid()
  84. {
  85. $f = new Zend_Log_Formatter_Xml();
  86. $line = $f->format(array('message' => '&amp', 'priority' => 42));
  87. $this->assertContains('&amp;amp', $line);
  88. }
  89. public function testConstructorWithArray()
  90. {
  91. $options = array(
  92. 'rootElement' => 'log',
  93. 'elementMap' => array(
  94. 'word' => 'message',
  95. 'priority' => 'priority'
  96. )
  97. );
  98. $event = array(
  99. 'message' => 'tottakai',
  100. 'priority' => 4
  101. );
  102. $expected = '<log><word>tottakai</word><priority>4</priority></log>';
  103. $formatter = new Zend_Log_Formatter_Xml($options);
  104. $output = $formatter->format($event);
  105. $this->assertContains($expected, $output);
  106. $this->assertEquals('UTF-8', $formatter->getEncoding());
  107. }
  108. /**
  109. * @group ZF-9176
  110. */
  111. public function testFactory()
  112. {
  113. $options = array(
  114. 'rootElement' => 'log',
  115. 'elementMap' => array(
  116. 'timestamp' => 'timestamp',
  117. 'response' => 'message'
  118. )
  119. );
  120. $formatter = Zend_Log_Formatter_Xml::factory($options);
  121. $this->assertType('Zend_Log_Formatter_Xml', $formatter);
  122. }
  123. /**
  124. * @group ZF-11161
  125. */
  126. public function testNonScalarValuesAreExcludedFromFormattedString()
  127. {
  128. $options = array(
  129. 'rootElement' => 'log'
  130. );
  131. $event = array(
  132. 'message' => 'tottakai',
  133. 'priority' => 4,
  134. 'context' => array('test'=>'one'),
  135. 'reference' => new Zend_Log_Formatter_Xml()
  136. );
  137. $expected = '<log><message>tottakai</message><priority>4</priority></log>';
  138. $formatter = new Zend_Log_Formatter_Xml($options);
  139. $output = $formatter->format($event);
  140. $this->assertContains($expected, $output);
  141. }
  142. /**
  143. * @group ZF-11161
  144. */
  145. public function testObjectsWithStringSerializationAreIncludedInFormattedString()
  146. {
  147. $options = array(
  148. 'rootElement' => 'log'
  149. );
  150. $event = array(
  151. 'message' => 'tottakai',
  152. 'priority' => 4,
  153. 'context' => array('test'=>'one'),
  154. 'reference' => new Zend_Log_Formatter_XmlTest_SerializableObject()
  155. );
  156. $expected = '<log><message>tottakai</message><priority>4</priority><reference>Zend_Log_Formatter_XmlTest_SerializableObject</reference></log>';
  157. $formatter = new Zend_Log_Formatter_Xml($options);
  158. $output = $formatter->format($event);
  159. $this->assertContains($expected, $output);
  160. }
  161. }
  162. class Zend_Log_Formatter_XmlTest_SerializableObject
  163. {
  164. public function __toString()
  165. {
  166. return __CLASS__;
  167. }
  168. }
  169. if (PHPUnit_MAIN_METHOD == 'Zend_Log_Formatter_XmlTest::main') {
  170. Zend_Log_Formatter_XmlTest::main();
  171. }