-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathJavaRunnable.php
More file actions
61 lines (52 loc) · 1.76 KB
/
JavaRunnable.php
File metadata and controls
61 lines (52 loc) · 1.76 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
<?php
declare(strict_types=1);
namespace PHPJava\Tests\Cases\Compiler;
use PHPJava\Compiler\Lang\PackageAssembler;
use PHPJava\Compiler\Lang\Stream\FileStream;
use PHPJava\Compiler\Lang\Stream\StreamReaderInterface;
use PHPJava\Core\JVM\Parameters\Runtime;
trait JavaRunnable
{
private function getDistributeDirectory(): string
{
return __DIR__ . '/../caches/compiler';
}
private function getClassNameByTestName(string $name): string
{
return ucfirst(explode('::', $name)[1]);
}
private function getFileStream(string $className): StreamReaderInterface
{
return (new FileStream(__DIR__ . '/Codes/' . $className . '.php'))
->setDistributeDirectory($this->getDistributeDirectory());
}
private function runJavaTest(string $testName, ?string $namespace = null): array
{
return $this->runJavaWithClassFileNameTest(
$testName,
null,
$namespace
);
}
private function runJavaEntryPointTest(string $testName, ?string $namespace = null): array
{
return $this->runJavaWithClassFileNameTest(
$testName,
Runtime::PHP_ENTRY_POINT_CLASS_NAME,
$namespace
);
}
private function runJavaWithClassFileNameTest(string $testName, string $classFileName = null, ?string $namespace = null)
{
$name = $this->getClassNameByTestName($testName);
(new PackageAssembler($this->getFileStream($name)))
->assemble();
$classPath = ltrim(($namespace ?? '') . '.' . ($classFileName ?? $name), '.');
exec(
'cd ' . $this->getDistributeDirectory() . ' && java ' . $classPath,
$output,
$return
);
return [$output, $return];
}
}