2
0

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. use PHPUnit\Framework\Attributes\DataProvider;
  14. class ProcessHandlerTest extends TestCase
  15. {
  16. /**
  17. * Dummy command to be used by tests that should not fail due to the command.
  18. *
  19. * @var string
  20. */
  21. const DUMMY_COMMAND = 'echo';
  22. /**
  23. * @covers Monolog\Handler\ProcessHandler::__construct
  24. * @covers Monolog\Handler\ProcessHandler::guardAgainstInvalidCommand
  25. * @covers Monolog\Handler\ProcessHandler::guardAgainstInvalidCwd
  26. * @covers Monolog\Handler\ProcessHandler::write
  27. * @covers Monolog\Handler\ProcessHandler::ensureProcessIsStarted
  28. * @covers Monolog\Handler\ProcessHandler::startProcess
  29. * @covers Monolog\Handler\ProcessHandler::handleStartupErrors
  30. */
  31. public function testWriteOpensProcessAndWritesToStdInOfProcess()
  32. {
  33. $fixtures = [
  34. 'chuck norris',
  35. 'foobar1337',
  36. ];
  37. $mockBuilder = $this->getMockBuilder('Monolog\Handler\ProcessHandler');
  38. $mockBuilder->onlyMethods(['writeProcessInput']);
  39. // using echo as command, as it is most probably available
  40. $mockBuilder->setConstructorArgs([self::DUMMY_COMMAND]);
  41. $handler = $mockBuilder->getMock();
  42. $matcher = $this->exactly(2);
  43. $handler->expects($matcher)
  44. ->method('writeProcessInput')
  45. ->willReturnCallback(function () use ($matcher, $fixtures) {
  46. match ($matcher->numberOfInvocations()) {
  47. 1 => $this->stringContains($fixtures[0]),
  48. 2 => $this->stringContains($fixtures[1]),
  49. };
  50. })
  51. ;
  52. /** @var ProcessHandler $handler */
  53. $handler->handle($this->getRecord(Level::Warning, $fixtures[0]));
  54. $handler->handle($this->getRecord(Level::Error, $fixtures[1]));
  55. }
  56. /**
  57. * Data provider for invalid commands.
  58. */
  59. public static function invalidCommandProvider(): array
  60. {
  61. return [
  62. [1337, 'TypeError'],
  63. ['', 'InvalidArgumentException'],
  64. [null, 'TypeError'],
  65. [fopen('php://input', 'r'), 'TypeError'],
  66. ];
  67. }
  68. /**
  69. * @covers Monolog\Handler\ProcessHandler::guardAgainstInvalidCommand
  70. */
  71. #[DataProvider('invalidCommandProvider')]
  72. public function testConstructWithInvalidCommandThrowsInvalidArgumentException(mixed $invalidCommand, string $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. * @param mixed $invalidCwd
  90. * @covers Monolog\Handler\ProcessHandler::guardAgainstInvalidCwd
  91. */
  92. #[DataProvider('invalidCwdProvider')]
  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. ->willReturn(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('', '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. }