PageRenderTime 30ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://github.com/Exercise/zf2
PHP | 156 lines | 104 code | 24 blank | 28 comment | 0 complexity | c44e5000900abe345ab37a055885a18a 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-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. namespace ZendTest\Log\Writer;
  23. use \Zend\Log\Writer\Stream as StreamWriter,
  24. \Zend\Log\Logger,
  25. \Zend\Log\Formatter\Simple as SimpleFormatter;
  26. /**
  27. * @category Zend
  28. * @package Zend_Log
  29. * @subpackage UnitTests
  30. * @copyright Copyright (c) 2005-2010 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 StreamWriterTest extends \PHPUnit_Framework_TestCase
  35. {
  36. public function testConstructorThrowsWhenResourceIsNotStream()
  37. {
  38. $resource = xml_parser_create();
  39. try {
  40. new StreamWriter($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 StreamWriter($stream);
  52. }
  53. public function testConstructorWithValidUrl()
  54. {
  55. new StreamWriter('php://memory');
  56. }
  57. public function testConstructorThrowsWhenModeSpecifiedForExistingStream()
  58. {
  59. $stream = fopen('php://memory', 'w+');
  60. $this->setExpectedException('\\Zend\\Log\\Exception', 'existing stream');
  61. new StreamWriter($stream, 'w+');
  62. }
  63. public function testConstructorThrowsWhenStreamCannotBeOpened()
  64. {
  65. $this->setExpectedException('\\Zend\\Log\\Exception', 'cannot be opened');
  66. new StreamWriter('');
  67. }
  68. public function testWrite()
  69. {
  70. $stream = fopen('php://memory', 'w+');
  71. $fields = array('message' => 'message-to-log');
  72. $writer = new StreamWriter($stream);
  73. $writer->write($fields);
  74. rewind($stream);
  75. $contents = stream_get_contents($stream);
  76. fclose($stream);
  77. $this->assertContains($fields['message'], $contents);
  78. }
  79. public function testWriteThrowsWhenStreamWriteFails()
  80. {
  81. $stream = fopen('php://memory', 'w+');
  82. $writer = new StreamWriter($stream);
  83. fclose($stream);
  84. $this->setExpectedException('\\Zend\\Log\\Exception', 'Unable to write');
  85. $writer->write(array('message' => 'foo'));
  86. }
  87. public function testShutdownClosesStreamResource()
  88. {
  89. $writer = new StreamWriter('php://memory', 'w+');
  90. $writer->write(array('message' => 'this write should succeed'));
  91. $writer->shutdown();
  92. $this->setExpectedException('\\Zend\\Log\\Exception', 'Unable to write');
  93. $writer->write(array('message' => 'this write should fail'));
  94. }
  95. public function testSettingNewFormatter()
  96. {
  97. $stream = fopen('php://memory', 'w+');
  98. $writer = new StreamWriter($stream);
  99. $expected = 'foo';
  100. $formatter = new SimpleFormatter($expected);
  101. $writer->setFormatter($formatter);
  102. $writer->write(array('bar'=>'baz'));
  103. rewind($stream);
  104. $contents = stream_get_contents($stream);
  105. fclose($stream);
  106. $this->assertContains($expected, $contents);
  107. }
  108. public function testFactoryStream()
  109. {
  110. $cfg = array('log' => array('memory' => array(
  111. 'writerName' => "Mock",
  112. 'writerParams' => array(
  113. 'stream' => 'php://memory',
  114. 'mode' => 'a'
  115. )
  116. )));
  117. $logger = Logger::factory($cfg['log']);
  118. $this->assertTrue($logger instanceof Logger);
  119. }
  120. public function testFactoryUrl()
  121. {
  122. $cfg = array('log' => array('memory' => array(
  123. 'writerName' => "Mock",
  124. 'writerParams' => array(
  125. 'url' => 'http://localhost',
  126. 'mode' => 'a'
  127. )
  128. )));
  129. $logger = Logger::factory($cfg['log']);
  130. $this->assertTrue($logger instanceof Logger);
  131. }
  132. }