ProcessHandlerTest.php 6.7 KB

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