InvokerTest.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php declare(strict_types=1);
  2. /*
  3. * This file is part of phpunit/php-invoker.
  4. *
  5. * (c) Sebastian Bergmann <sebastian@phpunit.de>
  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 SebastianBergmann\Invoker;
  11. use PHPUnit\Framework\TestCase;
  12. use RuntimeException;
  13. use SebastianBergmann\Invoker\TestFixture\TestCallable;
  14. /**
  15. * @requires extension pcntl
  16. *
  17. * @covers \SebastianBergmann\Invoker\Invoker
  18. */
  19. final class InvokerTest extends TestCase
  20. {
  21. /**
  22. * @var TestCallable
  23. */
  24. private $callable;
  25. /**
  26. * @var Invoker
  27. */
  28. private $invoker;
  29. protected function setUp(): void
  30. {
  31. $this->callable = new TestCallable;
  32. $this->invoker = new Invoker;
  33. }
  34. public function testExecutionOfCallableIsNotAbortedWhenTimeoutIsNotReached(): void
  35. {
  36. $this->assertTrue(
  37. $this->invoker->invoke([$this->callable, 'test'], [0], 1)
  38. );
  39. }
  40. public function testExecutionOfCallableIsAbortedWhenTimeoutIsReached(): void
  41. {
  42. $this->expectException(TimeoutException::class);
  43. $this->expectExceptionMessage('Execution aborted after 1 second');
  44. $this->invoker->invoke([$this->callable, 'test'], [2], 1);
  45. }
  46. public function testRequirementsCanBeChecked(): void
  47. {
  48. $this->assertTrue($this->invoker->canInvokeWithTimeout());
  49. }
  50. public function testAlarmIsClearedWhenCallableTimeoutIsNotReached(): void
  51. {
  52. $this->assertTrue(
  53. $this->invoker->invoke([$this->callable, 'test'], [0], 1)
  54. );
  55. try {
  56. sleep(1);
  57. } catch (TimeoutException $e) {
  58. $this->fail('Alarm timeout was not cleared');
  59. }
  60. }
  61. public function testAlarmIsClearedWhenCallableThrowsException(): void
  62. {
  63. $exception = new RuntimeException;
  64. $callable = static function () use ($exception): void {
  65. throw $exception;
  66. };
  67. try {
  68. $this->invoker->invoke($callable, [], 1);
  69. } catch (RuntimeException $e) {
  70. $this->assertSame($exception, $e);
  71. }
  72. try {
  73. sleep(1);
  74. } catch (TimeoutException $e) {
  75. $this->fail('Alarm timeout was not cleared');
  76. }
  77. }
  78. }