SocketHandlerTest.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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\Handler\SocketHandler\MockSocket;
  12. use Monolog\Handler\SocketHandler\Socket;
  13. use Monolog\Handler\SocketHandler\PersistentSocket;
  14. use Monolog\TestCase;
  15. use Monolog\Logger;
  16. /**
  17. * @author Pablo de Leon Belloc <pablolb@gmail.com>
  18. */
  19. class SocketHandlerTest extends TestCase
  20. {
  21. public function testWrite()
  22. {
  23. $socket = new MockSocket('localhost');
  24. $handler = new SocketHandler('localhost');
  25. $handler->setSocket($socket);
  26. $handler->setFormatter($this->getIdentityFormatter());
  27. $handler->handle($this->getRecord(Logger::WARNING, 'test'));
  28. $handler->handle($this->getRecord(Logger::WARNING, 'test2'));
  29. $handler->handle($this->getRecord(Logger::WARNING, 'test3'));
  30. $handle = $socket->getResource();
  31. fseek($handle, 0);
  32. $this->assertEquals('testtest2test3', fread($handle, 100));
  33. }
  34. public function testCloseClosesNonPersistentSocket()
  35. {
  36. $socket = new Socket('localhost');
  37. $res = fopen('php://memory', 'a');
  38. $socket->setResource($res);
  39. $handler = new SocketHandler('localhost');
  40. $handler->setSocket($socket);
  41. $handler->close();
  42. $this->assertFalse($socket->isConnected());
  43. }
  44. public function testCloseDoesNotClosePersistentSocket()
  45. {
  46. $socket = new PersistentSocket('localhost');
  47. $res = fopen('php://memory', 'a');
  48. $socket->setResource($res);
  49. $handler = new SocketHandler('localhost');
  50. $handler->setSocket($socket);
  51. $handler->close();
  52. $this->assertTrue($socket->isConnected());
  53. }
  54. }