2
0

StreamTest.php 5.6 KB

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