Skip to content

Commit

Permalink
Add unit test to assert that calling Rust from PHP via FFI works
Browse files Browse the repository at this point in the history
  • Loading branch information
lanedirt committed Jan 8, 2025
1 parent 23ef712 commit a44fb47
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions tests/Unit/RustFfiTest.php
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);
}
}

0 comments on commit a44fb47

Please sign in to comment.