PageRenderTime 27ms CodeModel.GetById 18ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 1ms

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

https://bitbucket.org/ksekar/campus
PHP | 194 lines | 125 code | 23 blank | 46 comment | 4 complexity | 8a60d3e54d963cab3c56b3dd1f0fae76 MD5 | raw file
  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 24594 2012-01-05 21:27:01Z matthew $
 21 */
 22
 23if (!defined('PHPUnit_MAIN_METHOD')) {
 24    define('PHPUnit_MAIN_METHOD', 'Zend_Log_Formatter_XmlTest::main');
 25}
 26
 27/** Zend_Log_Formatter_Xml */
 28require_once 'Zend/Log/Formatter/Xml.php';
 29
 30/**
 31 * @category   Zend
 32 * @package    Zend_Log
 33 * @subpackage UnitTests
 34 * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
 35 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 36 * @group      Zend_Log
 37 */
 38class Zend_Log_Formatter_XmlTest extends PHPUnit_Framework_TestCase
 39{
 40    public static function main()
 41    {
 42        $suite  = new PHPUnit_Framework_TestSuite(__CLASS__);
 43        $result = PHPUnit_TextUI_TestRunner::run($suite);
 44    }
 45
 46    public function testDefaultFormat()
 47    {
 48        $f = new Zend_Log_Formatter_Xml();
 49        $line = $f->format(array('message' => 'foo', 'priority' => 42));
 50
 51        $this->assertContains('foo', $line);
 52        $this->assertContains((string)42, $line);
 53    }
 54
 55    public function testConfiguringElementMapping()
 56    {
 57        $f = new Zend_Log_Formatter_Xml('log', array('foo' => 'bar'));
 58        $line = $f->format(array('bar' => 'baz'));
 59        $this->assertContains('<log><foo>baz</foo></log>', $line);
 60    }
 61
 62    public function testXmlDeclarationIsStripped()
 63    {
 64        $f = new Zend_Log_Formatter_Xml();
 65        $line = $f->format(array('message' => 'foo', 'priority' => 42));
 66
 67        $this->assertNotContains('<\?xml version=', $line);
 68    }
 69
 70    public function testXmlValidates()
 71    {
 72        $f = new Zend_Log_Formatter_Xml();
 73        $line = $f->format(array('message' => 'foo', 'priority' => 42));
 74
 75        $sxml = @simplexml_load_string($line);
 76        $this->assertType('SimpleXMLElement', $sxml, 'Formatted XML is invalid');
 77    }
 78
 79    /**
 80     * @group ZF-2062
 81     * @group ZF-4190
 82     */
 83    public function testHtmlSpecialCharsInMessageGetEscapedForValidXml()
 84    {
 85        $f = new Zend_Log_Formatter_Xml();
 86        $line = $f->format(array('message' => '&key1=value1&key2=value2', 'priority' => 42));
 87
 88        $this->assertContains("&amp;", $line);
 89        $this->assertTrue(substr_count($line, "&amp;") == 2);
 90    }
 91
 92    /**
 93     * @group ZF-2062
 94     * @group ZF-4190
 95     */
 96    public function testFixingBrokenCharsSoXmlIsValid()
 97    {
 98        $f = new Zend_Log_Formatter_Xml();
 99        $line = $f->format(array('message' => '&amp', 'priority' => 42));
100
101        $this->assertContains('&amp;amp', $line);
102    }
103
104    public function testConstructorWithArray()
105    {
106        $options = array(
107            'rootElement' => 'log',
108            'elementMap' => array(
109                'word' => 'message',
110                'priority' => 'priority'
111            )
112        );
113        $event = array(
114            'message' => 'tottakai',
115            'priority' => 4
116        );
117        $expected = '<log><word>tottakai</word><priority>4</priority></log>';
118
119        $formatter = new Zend_Log_Formatter_Xml($options);
120        $output = $formatter->format($event);
121        $this->assertContains($expected, $output);
122        $this->assertEquals('UTF-8', $formatter->getEncoding());
123    }
124
125    /**
126     * @group ZF-9176
127     */
128    public function testFactory()
129    {
130        $options = array(
131            'rootElement' => 'log',
132            'elementMap' => array(
133                'timestamp' => 'timestamp',
134                'response' => 'message'
135            )
136        );
137        $formatter = Zend_Log_Formatter_Xml::factory($options);
138        $this->assertType('Zend_Log_Formatter_Xml', $formatter);
139    }
140    
141    /**
142     * @group ZF-11161
143     */
144    public function testNonScalarValuesAreExcludedFromFormattedString()
145    {
146        $options = array(
147            'rootElement' => 'log'
148        );
149        $event = array(
150            'message' => 'tottakai',
151            'priority' => 4,
152            'context' => array('test'=>'one'),
153            'reference' => new Zend_Log_Formatter_Xml()
154        );
155        $expected = '<log><message>tottakai</message><priority>4</priority></log>';
156
157        $formatter = new Zend_Log_Formatter_Xml($options);
158        $output = $formatter->format($event);
159        $this->assertContains($expected, $output);
160    }
161    
162    /**
163     * @group ZF-11161
164     */
165    public function testObjectsWithStringSerializationAreIncludedInFormattedString()
166    {
167        $options = array(
168            'rootElement' => 'log'
169        );
170        $event = array(
171            'message' => 'tottakai',
172            'priority' => 4,
173            'context' => array('test'=>'one'),
174            'reference' => new Zend_Log_Formatter_XmlTest_SerializableObject()
175        );
176        $expected = '<log><message>tottakai</message><priority>4</priority><reference>Zend_Log_Formatter_XmlTest_SerializableObject</reference></log>';
177
178        $formatter = new Zend_Log_Formatter_Xml($options);
179        $output = $formatter->format($event);
180        $this->assertContains($expected, $output);
181    }
182}
183
184class Zend_Log_Formatter_XmlTest_SerializableObject
185{
186    public function __toString()
187    {
188        return __CLASS__;
189    }
190}
191
192if (PHPUnit_MAIN_METHOD == 'Zend_Log_Formatter_XmlTest::main') {
193    Zend_Log_Formatter_XmlTest::main();
194}