XmlTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  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-2010 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. }
  90. if (PHPUnit_MAIN_METHOD == 'Zend_Log_Formatter_XmlTest::main') {
  91. Zend_Log_Formatter_XmlTest::main();
  92. }