XmlTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. /**
  26. * Test helper
  27. */
  28. require_once dirname(__FILE__) . '/../../../TestHelper.php';
  29. /** Zend_Log_Formatter_Xml */
  30. require_once 'Zend/Log/Formatter/Xml.php';
  31. /**
  32. * @category Zend
  33. * @package Zend_Log
  34. * @subpackage UnitTests
  35. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  36. * @license http://framework.zend.com/license/new-bsd New BSD License
  37. * @group Zend_Log
  38. */
  39. class Zend_Log_Formatter_XmlTest extends PHPUnit_Framework_TestCase
  40. {
  41. public static function main()
  42. {
  43. $suite = new PHPUnit_Framework_TestSuite(__CLASS__);
  44. $result = PHPUnit_TextUI_TestRunner::run($suite);
  45. }
  46. public function testDefaultFormat()
  47. {
  48. $f = new Zend_Log_Formatter_Xml();
  49. $line = $f->format(array('message' => 'foo', 'priority' => 42));
  50. $this->assertContains('foo', $line);
  51. $this->assertContains((string)42, $line);
  52. }
  53. public function testConfiguringElementMapping()
  54. {
  55. $f = new Zend_Log_Formatter_Xml('log', array('foo' => 'bar'));
  56. $line = $f->format(array('bar' => 'baz'));
  57. $this->assertContains('<log><foo>baz</foo></log>', $line);
  58. }
  59. public function testXmlDeclarationIsStripped()
  60. {
  61. $f = new Zend_Log_Formatter_Xml();
  62. $line = $f->format(array('message' => 'foo', 'priority' => 42));
  63. $this->assertNotContains('<\?xml version=', $line);
  64. }
  65. public function testXmlValidates()
  66. {
  67. $f = new Zend_Log_Formatter_Xml();
  68. $line = $f->format(array('message' => 'foo', 'priority' => 42));
  69. $sxml = @simplexml_load_string($line);
  70. $this->assertType('SimpleXMLElement', $sxml, 'Formatted XML is invalid');
  71. }
  72. /**
  73. * @group ZF-2062
  74. * @group ZF-4190
  75. */
  76. public function testHtmlSpecialCharsInMessageGetEscapedForValidXml()
  77. {
  78. $f = new Zend_Log_Formatter_Xml();
  79. $line = $f->format(array('message' => '&key1=value1&key2=value2', 'priority' => 42));
  80. $this->assertContains("&amp;", $line);
  81. $this->assertTrue(substr_count($line, "&amp;") == 2);
  82. }
  83. /**
  84. * @group ZF-2062
  85. * @group ZF-4190
  86. */
  87. public function testFixingBrokenCharsSoXmlIsValid()
  88. {
  89. $f = new Zend_Log_Formatter_Xml();
  90. $line = $f->format(array('message' => '&amp', 'priority' => 42));
  91. $this->assertContains('&amp;amp', $line);
  92. }
  93. }
  94. if (PHPUnit_MAIN_METHOD == 'Zend_Log_Formatter_XmlTest::main') {
  95. Zend_Log_Formatter_XmlTest::main();
  96. }