ProcessHandlerTest.php 6.6 KB

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