-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathScriptRunnerTest.php
More file actions
77 lines (62 loc) · 2.15 KB
/
ScriptRunnerTest.php
File metadata and controls
77 lines (62 loc) · 2.15 KB
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
<?php
namespace GT\Cron\Test;
use GT\Cron\ResolvedScriptCommand;
use GT\Cron\ScriptExecutionException;
use GT\Cron\ScriptOutputMode;
use GT\Cron\ScriptRunner;
use GT\Cron\Test\Helper\Override;
use PHPUnit\Framework\TestCase;
class ScriptRunnerTest extends TestCase {
public function testRunCapturesOutput():void {
$command = PHP_BINARY . " -r "
. escapeshellarg("fwrite(STDOUT, 'out');fwrite(STDERR, 'err');");
$runner = new ScriptRunner(ScriptOutputMode::CAPTURE);
$result = $runner->run($command);
self::assertSame("out", $result->stdout);
self::assertSame("err", $result->stderr);
}
/** @runInSeparateProcess */
public function testRunUsesInjectedCommandResolver():void {
$resolver = $this->createMock(ResolvedScriptCommand::class);
$resolver->expects(self::once())
->method("resolve")
->with("example")
->willReturn("resolved-example");
$calledCommand = null;
Override::setCallback("proc_open", function($command) use(&$calledCommand) {
$calledCommand = $command;
return "EXAMPLE_PROCESS";
});
Override::load("proc_get_status");
Override::setCallback("proc_close", function() {
});
$runner = new ScriptRunner(ScriptOutputMode::DISCARD, $resolver);
$runner->run("example");
self::assertSame("resolved-example", $calledCommand);
}
/** @runInSeparateProcess */
public function testRunUsesInheritDescriptor():void {
$descriptor = null;
Override::setCallback("proc_open", function($command, $descriptorArg) use(&$descriptor) {
$descriptor = $descriptorArg;
return "EXAMPLE_PROCESS";
});
Override::load("proc_get_status");
Override::setCallback("proc_close", function() {
});
$runner = new ScriptRunner(ScriptOutputMode::INHERIT);
$runner->run("example");
self::assertSame(["file", "php://stdout", "w"], $descriptor[1]);
self::assertSame(["file", "php://stderr", "w"], $descriptor[2]);
}
/** @runInSeparateProcess */
public function testRunThrowsWhenProcessFails():void {
Override::setCallback("proc_open", function() {
return false;
});
Override::load("proc_get_status");
$runner = new ScriptRunner();
self::expectException(ScriptExecutionException::class);
$runner->run("example");
}
}