Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[VarDumper] Fix FFICaster test to be platform-adaptable #57555

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions src/Symfony/Component/VarDumper/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