Skip to content

Commit

Permalink
[VarDumper] Fix FFICaster test to be platform-adaptable
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandre-daubois committed Jun 27, 2024
1 parent f828481 commit c31566e
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions Tests/Caster/FFICasterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\VarDumper\Tests\Caster;

use PHPUnit\Framework\TestCase;
use Symfony\Component\VarDumper\Caster\FFICaster;
use Symfony\Component\VarDumper\Test\VarDumperTestTrait;

/**
Expand All @@ -23,6 +24,11 @@ class FFICasterTest extends TestCase
{
use VarDumperTestTrait;

/**
* @see FFICaster::MAX_STRING_LENGTH
*/
private const MAX_STRING_LENGTH = 255;

protected function setUp(): void
{
if (\in_array(\PHP_SAPI, ['cli', 'phpdbg'], true) && 'preload' === \ini_get('ffi.enable')) {
Expand Down Expand Up @@ -172,17 +178,24 @@ public function testCastCuttedPointerToChar()
{
$actualMessage = str_repeat('Hello World!', 30)."\0";
$actualLength = \strlen($actualMessage);

$expectedMessage = 'Hello World!Hello World!Hello World!Hello World!'
.'Hello World!Hello World!Hello World!Hello World!Hello World!Hel'
.'lo World!Hello World!Hello World!Hello World!Hello World!Hello '
.'World!Hello World!Hello World!Hello World!Hello World!Hello Wor'
.'ld!Hello World!Hel';
$expectedMessage = substr($actualMessage, 0, self::MAX_STRING_LENGTH);

$string = \FFI::cdef()->new('char['.$actualLength.']');
$pointer = \FFI::addr($string[0]);
\FFI::memcpy($pointer, $actualMessage, $actualLength);

// the max length is platform-dependent and can be less than 255,
// so we need to cut the expected message to the maximum length
// allowed by pages size of the current system
$ffi = \FFI::cdef(<<<C
size_t zend_get_page_size(void);
C);

$pageSize = $ffi->zend_get_page_size();
$start = $ffi->cast('uintptr_t', $ffi->cast('char*', $pointer))->cdata;
$max = min(self::MAX_STRING_LENGTH, ($start | ($pageSize - 1)) - $start);
$expectedMessage = substr($expectedMessage, 0, $max);

$this->assertDumpEquals(<<<PHP
FFI\CData<char*> size 8 align 8 {
cdata: "$expectedMessage"…
Expand Down

0 comments on commit c31566e

Please sign in to comment.