StreamHandlerTest.php 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. * @expectedException LogicException
  51. * @covers Monolog\Handler\StreamHandler::__construct
  52. * @covers Monolog\Handler\StreamHandler::write
  53. */
  54. public function testWriteMissingResource()
  55. {
  56. $handler = new StreamHandler(null);
  57. $handler->handle($this->getRecord());
  58. }
  59. /**
  60. * @expectedException UnexpectedValueException
  61. * @covers Monolog\Handler\StreamHandler::__construct
  62. * @covers Monolog\Handler\StreamHandler::write
  63. */
  64. public function testWriteInvalidResource()
  65. {
  66. $handler = new StreamHandler('bogus://url');
  67. $handler->handle($this->getRecord());
  68. }
  69. }