StreamHandlerTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /*
  3. * This file is part of the Monolog package.
  4. *
  5. * (c) Jordi Boggiano <j.boggiano@seld.be>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Monolog\Handler;
  11. use Monolog\TestCase;
  12. use Monolog\Logger;
  13. class StreamHandlerTest extends TestCase
  14. {
  15. /**
  16. * @covers Monolog\Handler\StreamHandler::__construct
  17. * @covers Monolog\Handler\StreamHandler::write
  18. */
  19. public function testWrite()
  20. {
  21. $handle = fopen('php://memory', 'a+');
  22. $handler = new StreamHandler($handle);
  23. $handler->setFormatter($this->getIdentityFormatter());
  24. $handler->handle($this->getRecord(Logger::WARNING, 'test'));
  25. $handler->handle($this->getRecord(Logger::WARNING, 'test2'));
  26. $handler->handle($this->getRecord(Logger::WARNING, 'test3'));
  27. fseek($handle, 0);
  28. $this->assertEquals('testtest2test3', fread($handle, 100));
  29. }
  30. /**
  31. * @covers Monolog\Handler\StreamHandler::close
  32. */
  33. public function testClose()
  34. {
  35. $handle = fopen('php://memory', 'a+');
  36. $handler = new StreamHandler($handle);
  37. $this->assertTrue(is_resource($handle));
  38. $handler->close();
  39. $this->assertFalse(is_resource($handle));
  40. }
  41. /**
  42. * @covers Monolog\Handler\StreamHandler::write
  43. */
  44. public function testWriteCreatesTheStreamResource()
  45. {
  46. $handler = new StreamHandler('php://memory');
  47. $handler->handle($this->getRecord());
  48. }
  49. /**
  50. * @covers Monolog\Handler\StreamHandler::__construct
  51. * @covers Monolog\Handler\StreamHandler::write
  52. */
  53. public function testWriteLocking()
  54. {
  55. $temp = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'monolog_locked_log';
  56. $handler = new StreamHandler($temp, Logger::DEBUG, true, null, true);
  57. $handler->handle($this->getRecord());
  58. }
  59. /**
  60. * @expectedException LogicException
  61. * @covers Monolog\Handler\StreamHandler::__construct
  62. * @covers Monolog\Handler\StreamHandler::write
  63. */
  64. public function testWriteMissingResource()
  65. {
  66. $handler = new StreamHandler(null);
  67. $handler->handle($this->getRecord());
  68. }
  69. public function invalidArgumentProvider()
  70. {
  71. return array(
  72. array(1),
  73. array(array()),
  74. array(array('bogus://url')),
  75. );
  76. }
  77. /**
  78. * @dataProvider invalidArgumentProvider
  79. * @expectedException InvalidArgumentException
  80. * @covers Monolog\Handler\StreamHandler::__construct
  81. * @covers Monolog\Handler\StreamHandler::write
  82. */
  83. public function testWriteInvalidArgument($invalidArgument)
  84. {
  85. $handler = new StreamHandler($invalidArgument);
  86. $handler->handle($this->getRecord());
  87. }
  88. /**
  89. * @expectedException UnexpectedValueException
  90. * @covers Monolog\Handler\StreamHandler::__construct
  91. * @covers Monolog\Handler\StreamHandler::write
  92. */
  93. public function testWriteInvalidResource()
  94. {
  95. $handler = new StreamHandler('bogus://url');
  96. $handler->handle($this->getRecord());
  97. }
  98. /**
  99. * @expectedException UnexpectedValueException
  100. * @covers Monolog\Handler\StreamHandler::__construct
  101. * @covers Monolog\Handler\StreamHandler::write
  102. */
  103. public function testWriteNonExistingResource()
  104. {
  105. $handler = new StreamHandler('/foo/bar/baz/'.rand(0, 10000));
  106. $handler->handle($this->getRecord());
  107. }
  108. }