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