File: InvokerTest.php

package info (click to toggle)
php-invoker 6.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 29,592 kB
  • sloc: php: 188; xml: 47; makefile: 16
file content (86 lines) | stat: -rw-r--r-- 2,377 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php declare(strict_types=1);
/*
 * This file is part of phpunit/php-invoker.
 *
 * (c) Sebastian Bergmann <sebastian@phpunit.de>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
namespace SebastianBergmann\Invoker;

use function sleep;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\RequiresPhpExtension;
use PHPUnit\Framework\TestCase;
use RuntimeException;
use SebastianBergmann\Invoker\TestFixture\TestCallable;

#[RequiresPhpExtension('pcntl')]
#[CoversClass(Invoker::class)]
final class InvokerTest extends TestCase
{
    private TestCallable $callable;
    private Invoker $invoker;

    protected function setUp(): void
    {
        $this->callable = new TestCallable;
        $this->invoker  = new Invoker;
    }

    public function testExecutionOfCallableIsNotAbortedWhenTimeoutIsNotReached(): void
    {
        $this->assertTrue(
            $this->invoker->invoke([$this->callable, 'test'], [0], 1),
        );
    }

    public function testExecutionOfCallableIsAbortedWhenTimeoutIsReached(): void
    {
        $this->expectException(TimeoutException::class);
        $this->expectExceptionMessage('Execution aborted after 1 second');

        $this->invoker->invoke([$this->callable, 'test'], [2], 1);
    }

    public function testRequirementsCanBeChecked(): void
    {
        $this->assertTrue($this->invoker->canInvokeWithTimeout());
    }

    public function testAlarmIsClearedWhenCallableTimeoutIsNotReached(): void
    {
        $this->assertTrue(
            $this->invoker->invoke([$this->callable, 'test'], [0], 1),
        );

        try {
            sleep(1);
        } catch (TimeoutException) {
            $this->fail('Alarm timeout was not cleared');
        }
    }

    public function testAlarmIsClearedWhenCallableThrowsException(): void
    {
        $exception = new RuntimeException;

        $callable = static function () use ($exception): void
        {
            throw $exception;
        };

        try {
            $this->invoker->invoke($callable, [], 1);
        } catch (RuntimeException $e) {
            $this->assertSame($exception, $e);
        }

        try {
            sleep(1);
        } catch (TimeoutException) {
            $this->fail('Alarm timeout was not cleared');
        }
    }
}