-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add unit test to assert that calling Rust from PHP via FFI works
- Loading branch information
Showing
1 changed file
with
32 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace Tests\Unit; | ||
|
||
use Tests\UnitTestCase; | ||
|
||
class RustFfiTest extends UnitTestCase | ||
{ | ||
/** | ||
* Test that we can call a Rust test program via PHP FFI. | ||
*/ | ||
public function testRustFfiInterface(): void | ||
{ | ||
// Path to your .so file | ||
$libPath = base_path('storage/ffi-libs/libtest_ffi.so'); | ||
|
||
// Define the function signature in C syntax | ||
$ffi = \FFI::cdef(" | ||
char* rust_hello(void); | ||
", $libPath); | ||
|
||
// Call the Rust function and get the returned string | ||
/** @phpstan-ignore-next-line */ | ||
$result = $ffi->rust_hello(); | ||
|
||
// Convert the C string to a PHP string | ||
$output = \FFI::string($result); | ||
|
||
// Assert the expected output | ||
$this->assertEquals("Hello from Rust!", $output); | ||
} | ||
} |