Skip to content

Commit df0589c

Browse files
committed
Add tests and support outer class
1 parent c72c921 commit df0589c

6 files changed

Lines changed: 60 additions & 4 deletions

File tree

src/Core/JavaClassReader.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@ public function __construct(string $file)
1515

1616
// Add resolving path
1717
ClassResolver::add(
18-
ClassResolver::RESOURCE_TYPE_FILE,
19-
dirname($file)
18+
[
19+
[ClassResolver::RESOURCE_TYPE_FILE, dirname($file)],
20+
[ClassResolver::RESOURCE_TYPE_FILE, getcwd()],
21+
]
2022
);
2123
}
2224

src/Utilities/TypeResolver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public static function convertPHPtoJava($arguments, $defaultJavaArgumentType = '
5757
}
5858
if (empty($getNestedValues)) {
5959
$flipped = array_flip(static::PHP_TYPE_MAP);
60-
$resolveType = static::SIGNATURE_MAP[static::resolve($defaultJavaArgumentType)];
60+
$resolveType = static::SIGNATURE_MAP[static::resolve($defaultJavaArgumentType)[0]];
6161
if ($resolveType === 'class') {
6262
return [
6363
'type' => $resolveType,

tests/Base.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ public function setUp(): void
1313
parent::setUp();
1414
// build fixtures
1515

16+
$pathRoot = __DIR__ . '/fixtures/java/';
17+
1618
foreach ($this->fixtures as $fixture) {
17-
exec('javac -encoding UTF8 ' . __DIR__ . '/fixtures/java/' . str_replace(['../', './'], '', $fixture) . '.java -d ' . __DIR__ . '/caches');
19+
exec('javac -classpath ' . $pathRoot . ':' . $pathRoot . 'caches -encoding UTF8 ' . $pathRoot . str_replace(['../', './'], '', $fixture) . '.java -d ' . __DIR__ . '/caches');
1820
$this->initiatedJavaClasses[$fixture] = new \PHPJava\Core\JavaClass(
1921
new \PHPJava\Core\JavaClassReader(
2022
$this->getClassName($fixture)

tests/OuterClassTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
namespace PHPJava\Tests;
3+
4+
use PHPUnit\Framework\TestCase;
5+
6+
class OuterClassTest extends Base
7+
{
8+
protected $fixtures = [
9+
'OuterClassTestOuterClass',
10+
'OuterClassTest',
11+
];
12+
13+
public function testCallMain()
14+
{
15+
ob_start();
16+
$calculatedValue = $this->initiatedJavaClasses['OuterClassTest']
17+
->getInvoker()
18+
->getStatic()
19+
->getMethods()
20+
->call(
21+
'main',
22+
[]
23+
);
24+
$result = ob_get_clean();
25+
26+
$this->assertEquals(
27+
'Called Static Method AND Called Dynamic Method',
28+
$result
29+
);
30+
}
31+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class OuterClassTest
2+
{
3+
public static void main(String[] args)
4+
{
5+
System.out.print(OuterClassTestOuterClass.staticHelloWorld());
6+
System.out.print(" AND ");
7+
System.out.print((new OuterClassTestOuterClass()).dynamicHelloWorld());
8+
}
9+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class OuterClassTestOuterClass
2+
{
3+
public static String staticHelloWorld()
4+
{
5+
return "Called Static Method";
6+
}
7+
8+
public String dynamicHelloWorld()
9+
{
10+
return "Called Dynamic Method";
11+
}
12+
}

0 commit comments

Comments
 (0)