ProcessHandlerTest.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php declare(strict_types=1);
  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\Test\TestCase;
  12. use Monolog\Level;
  13. class ProcessHandlerTest extends TestCase
  14. {
  15. /**
  16. * Dummy command to be used by tests that should not fail due to the command.
  17. *
  18. * @var string
  19. */
  20. const DUMMY_COMMAND = 'echo';
  21. /**
  22. * @covers Monolog\Handler\ProcessHandler::__construct
  23. * @covers Monolog\Handler\ProcessHandler::guardAgainstInvalidCommand
  24. * @covers Monolog\Handler\ProcessHandler::guardAgainstInvalidCwd
  25. * @covers Monolog\Handler\ProcessHandler::write
  26. * @covers Monolog\Handler\ProcessHandler::ensureProcessIsStarted
  27. * @covers Monolog\Handler\ProcessHandler::startProcess
  28. * @covers Monolog\Handler\ProcessHandler::handleStartupErrors
  29. */
  30. public function testWriteOpensProcessAndWritesToStdInOfProcess()
  31. {
  32. $fixtures = [
  33. 'chuck norris',
  34. 'foobar1337',
  35. ];
  36. $mockBuilder = $this->getMockBuilder('Monolog\Handler\ProcessHandler');
  37. $mockBuilder->onlyMethods(['writeProcessInput']);
  38. // using echo as command, as it is most probably available
  39. $mockBuilder->setConstructorArgs([self::DUMMY_COMMAND]);
  40. $handler = $mockBuilder->getMock();
  41. $handler->expects($this->exactly(2))
  42. ->method('writeProcessInput')
  43. ->withConsecutive([$this->stringContains($fixtures[0])], [$this->stringContains($fixtures[1])]);
  44. /** @var ProcessHandler $handler */
  45. $handler->handle($this->getRecord(Level::Warning, $fixtures[0]));
  46. $handler->handle($this->getRecord(Level::Error, $fixtures[1]));
  47. }
  48. /**
  49. * Data provider for invalid commands.
  50. */
  51. public function invalidCommandProvider(): array
  52. {
  53. return [
  54. [1337, 'TypeError'],
  55. ['', 'InvalidArgumentException'],
  56. [null, 'TypeError'],
  57. [fopen('php://input', 'r'), 'TypeError'],
  58. ];
  59. }
  60. /**
  61. * @dataProvider invalidCommandProvider
  62. * @param mixed $invalidCommand
  63. * @covers Monolog\Handler\ProcessHandler::guardAgainstInvalidCommand
  64. */
  65. public function testConstructWithInvalidCommandThrowsInvalidArgumentException($invalidCommand, $expectedExcep)
  66. {
  67. $this->expectException($expectedExcep);
  68. new ProcessHandler($invalidCommand, Level::Debug);
  69. }
  70. /**
  71. * Data provider for invalid CWDs.
  72. */
  73. public function invalidCwdProvider(): array
  74. {
  75. return [
  76. [1337, 'TypeError'],
  77. ['', 'InvalidArgumentException'],
  78. [fopen('php://input', 'r'), 'TypeError'],
  79. ];
  80. }
  81. /**
  82. * @dataProvider invalidCwdProvider
  83. * @param mixed $invalidCwd
  84. * @covers Monolog\Handler\ProcessHandler::guardAgainstInvalidCwd
  85. */
  86. public function testConstructWithInvalidCwdThrowsInvalidArgumentException($invalidCwd, $expectedExcep)
  87. {
  88. $this->expectException($expectedExcep);
  89. new ProcessHandler(self::DUMMY_COMMAND, Level::Debug, true, $invalidCwd);
  90. }
  91. /**
  92. * @covers Monolog\Handler\ProcessHandler::__construct
  93. * @covers Monolog\Handler\ProcessHandler::guardAgainstInvalidCwd
  94. */
  95. public function testConstructWithValidCwdWorks()
  96. {
  97. $handler = new ProcessHandler(self::DUMMY_COMMAND, Level::Debug, true, sys_get_temp_dir());
  98. $this->assertInstanceOf(
  99. 'Monolog\Handler\ProcessHandler',
  100. $handler,
  101. 'Constructed handler is not a ProcessHandler.'
  102. );
  103. }
  104. /**
  105. * @covers Monolog\Handler\ProcessHandler::handleStartupErrors
  106. */
  107. public function testStartupWithFailingToSelectErrorStreamThrowsUnexpectedValueException()
  108. {
  109. $mockBuilder = $this->getMockBuilder('Monolog\Handler\ProcessHandler');
  110. $mockBuilder->onlyMethods(['selectErrorStream']);
  111. $mockBuilder->setConstructorArgs([self::DUMMY_COMMAND]);
  112. $handler = $mockBuilder->getMock();
  113. $handler->expects($this->once())
  114. ->method('selectErrorStream')
  115. ->will($this->returnValue(false));
  116. $this->expectException(\UnexpectedValueException::class);
  117. /** @var ProcessHandler $handler */
  118. $handler->handle($this->getRecord(Level::Warning, 'stream failing, whoops'));
  119. }
  120. /**
  121. * @covers Monolog\Handler\ProcessHandler::handleStartupErrors
  122. * @covers Monolog\Handler\ProcessHandler::selectErrorStream
  123. */
  124. public function testStartupWithErrorsThrowsUnexpectedValueException()
  125. {
  126. $handler = new ProcessHandler('>&2 echo "some fake error message"');
  127. $this->expectException(\UnexpectedValueException::class);
  128. $handler->handle($this->getRecord(Level::Warning, 'some warning in the house'));
  129. }
  130. /**
  131. * @covers Monolog\Handler\ProcessHandler::write
  132. */
  133. public function testWritingWithErrorsOnStdOutOfProcessThrowsInvalidArgumentException()
  134. {
  135. $mockBuilder = $this->getMockBuilder('Monolog\Handler\ProcessHandler');
  136. $mockBuilder->onlyMethods(['readProcessErrors']);
  137. // using echo as command, as it is most probably available
  138. $mockBuilder->setConstructorArgs([self::DUMMY_COMMAND]);
  139. $handler = $mockBuilder->getMock();
  140. $handler->expects($this->exactly(2))
  141. ->method('readProcessErrors')
  142. ->willReturnOnConsecutiveCalls('', $this->returnValue('some fake error message here'));
  143. $this->expectException(\UnexpectedValueException::class);
  144. /** @var ProcessHandler $handler */
  145. $handler->handle($this->getRecord(Level::Warning, 'some test stuff'));
  146. }
  147. /**
  148. * @covers Monolog\Handler\ProcessHandler::close
  149. */
  150. public function testCloseClosesProcess()
  151. {
  152. $class = new \ReflectionClass('Monolog\Handler\ProcessHandler');
  153. $property = $class->getProperty('process');
  154. $property->setAccessible(true);
  155. $handler = new ProcessHandler(self::DUMMY_COMMAND);
  156. $handler->handle($this->getRecord(Level::Warning, '21 is only the half truth'));
  157. $process = $property->getValue($handler);
  158. $this->assertTrue(is_resource($process), 'Process is not running although it should.');
  159. $handler->close();
  160. $process = $property->getValue($handler);
  161. $this->assertFalse(is_resource($process), 'Process is still running although it should not.');
  162. }
  163. }