2
0

ProcessHandlerTest.php 6.6 KB

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