mirror of
https://github.com/danog/psalm.git
synced 2024-11-26 20:34:47 +01:00
FFI Stubs: Define a set of tests.
Mostly, those are derived from examples from the documentation.
This commit is contained in:
parent
4da0bede35
commit
4596301290
87
tests/FFITest.php
Normal file
87
tests/FFITest.php
Normal file
@ -0,0 +1,87 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Psalm\Tests;
|
||||||
|
|
||||||
|
use Psalm\Tests\Traits\ValidCodeAnalysisTestTrait;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* tests for the handling of code using the PHP FFI extension
|
||||||
|
*
|
||||||
|
* https://www.php.net/manual/en/book.ffi.php
|
||||||
|
*/
|
||||||
|
class FFITest extends TestCase
|
||||||
|
{
|
||||||
|
use ValidCodeAnalysisTestTrait;
|
||||||
|
|
||||||
|
public function providerValidCodeParse(): iterable
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
// Examples are from https://www.php.net/manual/en/ffi.examples-basic.php
|
||||||
|
// Some of them needed tweaks, e.g. due to unsafe use of var_dump().
|
||||||
|
'Example #1 Calling a function from shared library' => [
|
||||||
|
'code' => '<?php
|
||||||
|
$ffi = FFI::cdef(
|
||||||
|
"int printf(const char *format, ...);", // this is a regular C declaration
|
||||||
|
"libc.so.6"
|
||||||
|
);
|
||||||
|
$ffi->printf("Hello %s!\n", "world");
|
||||||
|
',
|
||||||
|
'assertions' => [],
|
||||||
|
'ignored_issues' => [],
|
||||||
|
'php_version' => '7.4',
|
||||||
|
],
|
||||||
|
'Example #3 Accessing existing C variables' => [
|
||||||
|
'code' => '<?php
|
||||||
|
$ffi = FFI::cdef(
|
||||||
|
"int errno;", // this is a regular C declaration
|
||||||
|
"libc.so.6"
|
||||||
|
);
|
||||||
|
echo $ffi->errno;
|
||||||
|
',
|
||||||
|
'assertions' => [],
|
||||||
|
'ignored_issues' => [],
|
||||||
|
'php_version' => '7.4',
|
||||||
|
],
|
||||||
|
'Example #5 Working with C arrays' => [
|
||||||
|
'code' => '<?php
|
||||||
|
// create C data structure
|
||||||
|
$a = FFI::new("long[1024]");
|
||||||
|
// work with it like with a regular PHP array
|
||||||
|
$size = count($a);
|
||||||
|
for ($i = 0; $i < $size; $i++) {
|
||||||
|
$a[$i] = $i;
|
||||||
|
}
|
||||||
|
$sum = 0;
|
||||||
|
/** @psalm-suppress MixedAssignment */
|
||||||
|
foreach ($a as $n) {
|
||||||
|
/** @psalm-suppress MixedOperand */
|
||||||
|
$sum += $n;
|
||||||
|
}
|
||||||
|
',
|
||||||
|
'assertions' => [],
|
||||||
|
'ignored_issues' => [],
|
||||||
|
'php_version' => '7.4',
|
||||||
|
],
|
||||||
|
'Example #6 Working with C enums' => [
|
||||||
|
'code' => '<?php
|
||||||
|
$a = FFI::cdef(
|
||||||
|
"typedef enum _zend_ffi_symbol_kind {
|
||||||
|
ZEND_FFI_SYM_TYPE,
|
||||||
|
ZEND_FFI_SYM_CONST = 2,
|
||||||
|
ZEND_FFI_SYM_VAR,
|
||||||
|
ZEND_FFI_SYM_FUNC
|
||||||
|
} zend_ffi_symbol_kind;"
|
||||||
|
);
|
||||||
|
echo $a->ZEND_FFI_SYM_TYPE;
|
||||||
|
echo $a->ZEND_FFI_SYM_CONST;
|
||||||
|
echo $a->ZEND_FFI_SYM_VAR;
|
||||||
|
',
|
||||||
|
'assertions' => [],
|
||||||
|
'ignored_issues' => [],
|
||||||
|
'php_version' => '7.4',
|
||||||
|
],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user