PageRenderTime 288ms CodeModel.GetById 277ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/Zend/Log/Writer/StreamTest.php

https://bitbucket.org/ksekar/campus
PHP | 190 lines | 133 code | 27 blank | 30 comment | 3 complexity | ca8f348fed88d6c4c6c17fbb4f98d473 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: StreamTest.php 24594 2012-01-05 21:27:01Z matthew $
 21 */
 22
 23if (!defined('PHPUnit_MAIN_METHOD')) {
 24    define('PHPUnit_MAIN_METHOD', 'Zend_Log_Writer_StreamTest::main');
 25}
 26
 27/** Zend_Log */
 28require_once 'Zend/Log.php';
 29
 30/** Zend_Log_Writer_Stream */
 31require_once 'Zend/Log/Writer/Stream.php';
 32
 33/**
 34 * @category   Zend
 35 * @package    Zend_Log
 36 * @subpackage UnitTests
 37 * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
 38 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 39 * @group      Zend_Log
 40 */
 41class Zend_Log_Writer_StreamTest extends PHPUnit_Framework_TestCase
 42{
 43    public static function main()
 44    {
 45        $suite  = new PHPUnit_Framework_TestSuite(__CLASS__);
 46        $result = PHPUnit_TextUI_TestRunner::run($suite);
 47    }
 48
 49    public function testConstructorThrowsWhenResourceIsNotStream()
 50    {
 51        $resource = xml_parser_create();
 52        try {
 53            new Zend_Log_Writer_Stream($resource);
 54            $this->fail();
 55        } catch (Exception $e) {
 56            $this->assertType('Zend_Log_Exception', $e);
 57            $this->assertRegExp('/not a stream/i', $e->getMessage());
 58        }
 59        xml_parser_free($resource);
 60    }
 61
 62    public function testConstructorWithValidStream()
 63    {
 64        $stream = fopen('php://memory', 'w+');
 65        new Zend_Log_Writer_Stream($stream);
 66    }
 67
 68    public function testConstructorWithValidUrl()
 69    {
 70        new Zend_Log_Writer_Stream('php://memory');
 71    }
 72
 73    public function testConstructorThrowsWhenModeSpecifiedForExistingStream()
 74    {
 75        $stream = fopen('php://memory', 'w+');
 76        try {
 77            new Zend_Log_Writer_Stream($stream, 'w+');
 78            $this->fail();
 79        } catch (Exception $e) {
 80            $this->assertType('Zend_Log_Exception', $e);
 81            $this->assertRegExp('/existing stream/i', $e->getMessage());
 82        }
 83    }
 84
 85    public function testConstructorThrowsWhenStreamCannotBeOpened()
 86    {
 87        try {
 88            new Zend_Log_Writer_Stream('');
 89            $this->fail();
 90        } catch (Exception $e) {
 91            $this->assertType('Zend_Log_Exception', $e);
 92            $this->assertRegExp('/cannot be opened/i', $e->getMessage());
 93        }
 94    }
 95
 96    public function testWrite()
 97    {
 98        $stream = fopen('php://memory', 'w+');
 99        $fields = array('message' => 'message-to-log');
100
101        $writer = new Zend_Log_Writer_Stream($stream);
102        $writer->write($fields);
103
104        rewind($stream);
105        $contents = stream_get_contents($stream);
106        fclose($stream);
107
108        $this->assertContains($fields['message'], $contents);
109    }
110
111    public function testWriteThrowsWhenStreamWriteFails()
112    {
113        $stream = fopen('php://memory', 'w+');
114        $writer = new Zend_Log_Writer_Stream($stream);
115        fclose($stream);
116
117        try {
118            $writer->write(array('message' => 'foo'));
119            $this->fail();
120        } catch (Exception $e) {
121            $this->assertType('Zend_Log_Exception', $e);
122            $this->assertRegExp('/unable to write/i', $e->getMessage());
123        }
124    }
125
126    public function testShutdownClosesStreamResource()
127    {
128        $writer = new Zend_Log_Writer_Stream('php://memory', 'w+');
129        $writer->write(array('message' => 'this write should succeed'));
130
131        $writer->shutdown();
132
133        try {
134            $writer->write(array('message' => 'this write should fail'));
135            $this->fail();
136        } catch (Exception $e) {
137            $this->assertType('Zend_Log_Exception', $e);
138            $this->assertRegExp('/unable to write/i', $e->getMessage());
139        }
140    }
141
142    public function testSettingNewFormatter()
143    {
144        $stream = fopen('php://memory', 'w+');
145        $writer = new Zend_Log_Writer_Stream($stream);
146        $expected = 'foo';
147
148        $formatter = new Zend_Log_Formatter_Simple($expected);
149        $writer->setFormatter($formatter);
150
151        $writer->write(array('bar'=>'baz'));
152        rewind($stream);
153        $contents = stream_get_contents($stream);
154        fclose($stream);
155
156        $this->assertContains($expected, $contents);
157    }
158
159    public function testFactoryStream()
160    {
161        $cfg = array('log' => array('memory' => array(
162            'writerName'   => "Mock",
163            'writerParams' => array(
164                'stream' => 'php://memory',
165                'mode'   => 'a'
166            )
167        )));
168
169        $logger = Zend_Log::factory($cfg['log']);
170        $this->assertTrue($logger instanceof Zend_Log);
171    }
172
173    public function testFactoryUrl()
174    {
175        $cfg = array('log' => array('memory' => array(
176            'writerName'   => "Mock",
177            'writerParams' => array(
178                'url'  => 'http://localhost',
179                'mode' => 'a'
180            )
181        )));
182
183        $logger = Zend_Log::factory($cfg['log']);
184        $this->assertTrue($logger instanceof Zend_Log);
185    }
186}
187
188if (PHPUnit_MAIN_METHOD == 'Zend_Log_Writer_StreamTest::main') {
189    Zend_Log_Writer_StreamTest::main();
190}