2
0

StreamTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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-2009 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. /** PHPUnit_Framework_TestCase */
  23. require_once 'PHPUnit/Framework/TestCase.php';
  24. /** Zend_Log_Writer_Mock */
  25. require_once 'Zend/Log/Writer/Stream.php';
  26. /**
  27. * @category Zend
  28. * @package Zend_Log
  29. * @subpackage UnitTests
  30. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. * @group Zend_Log
  33. */
  34. class Zend_Log_Writer_StreamTest extends PHPUnit_Framework_TestCase
  35. {
  36. public function testConstructorThrowsWhenResourceIsNotStream()
  37. {
  38. $resource = xml_parser_create();
  39. try {
  40. new Zend_Log_Writer_Stream($resource);
  41. $this->fail();
  42. } catch (Exception $e) {
  43. $this->assertType('Zend_Log_Exception', $e);
  44. $this->assertRegExp('/not a stream/i', $e->getMessage());
  45. }
  46. xml_parser_free($resource);
  47. }
  48. public function testConstructorWithValidStream()
  49. {
  50. $stream = fopen('php://memory', 'w+');
  51. new Zend_Log_Writer_Stream($stream);
  52. }
  53. public function testConstructorWithValidUrl()
  54. {
  55. new Zend_Log_Writer_Stream('php://memory');
  56. }
  57. public function testConstructorThrowsWhenModeSpecifiedForExistingStream()
  58. {
  59. $stream = fopen('php://memory', 'w+');
  60. try {
  61. new Zend_Log_Writer_Stream($stream, 'w+');
  62. $this->fail();
  63. } catch (Exception $e) {
  64. $this->assertType('Zend_Log_Exception', $e);
  65. $this->assertRegExp('/existing stream/i', $e->getMessage());
  66. }
  67. }
  68. public function testConstructorThrowsWhenStreamCannotBeOpened()
  69. {
  70. try {
  71. new Zend_Log_Writer_Stream('');
  72. $this->fail();
  73. } catch (Exception $e) {
  74. $this->assertType('Zend_Log_Exception', $e);
  75. $this->assertRegExp('/cannot be opened/i', $e->getMessage());
  76. }
  77. }
  78. public function testWrite()
  79. {
  80. $stream = fopen('php://memory', 'w+');
  81. $fields = array('message' => 'message-to-log');
  82. $writer = new Zend_Log_Writer_Stream($stream);
  83. $writer->write($fields);
  84. rewind($stream);
  85. $contents = stream_get_contents($stream);
  86. fclose($stream);
  87. $this->assertContains($fields['message'], $contents);
  88. }
  89. public function testWriteThrowsWhenStreamWriteFails()
  90. {
  91. $stream = fopen('php://memory', 'w+');
  92. $writer = new Zend_Log_Writer_Stream($stream);
  93. fclose($stream);
  94. try {
  95. $writer->write(array('message' => 'foo'));
  96. $this->fail();
  97. } catch (Exception $e) {
  98. $this->assertType('Zend_Log_Exception', $e);
  99. $this->assertRegExp('/unable to write/i', $e->getMessage());
  100. }
  101. }
  102. public function testShutdownClosesStreamResource()
  103. {
  104. $writer = new Zend_Log_Writer_Stream('php://memory', 'w+');
  105. $writer->write(array('message' => 'this write should succeed'));
  106. $writer->shutdown();
  107. try {
  108. $writer->write(array('message' => 'this write should fail'));
  109. $this->fail();
  110. } catch (Exception $e) {
  111. $this->assertType('Zend_Log_Exception', $e);
  112. $this->assertRegExp('/unable to write/i', $e->getMessage());
  113. }
  114. }
  115. public function testSettingNewFormatter()
  116. {
  117. $stream = fopen('php://memory', 'w+');
  118. $writer = new Zend_Log_Writer_Stream($stream);
  119. $expected = 'foo';
  120. $formatter = new Zend_Log_Formatter_Simple($expected);
  121. $writer->setFormatter($formatter);
  122. $writer->write(array('bar'=>'baz'));
  123. rewind($stream);
  124. $contents = stream_get_contents($stream);
  125. fclose($stream);
  126. $this->assertContains($expected, $contents);
  127. }
  128. }