Files
cacti/tests/Unit/Core/PhpSourceContractTest.php
T
Thomas Vincent 2b6e96b23a style(php): [develop] wrap CLI and poller calls without changing execution (#8303)
* style(php): wrap CLI and poller calls without changing execution

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

* fix(ci): review security inventories after poller wrapping

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

* test(boost): compare handoff source contracts by PHP tokens

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

* style(php): wrap poller constant strings without changing values

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

* style(php): wrap poller concatenations with token-preserving source contracts

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

* test(cli): assert CDEF repair queries after constant folding

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

* test: verify PHP formatting preserves parsed behavior

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

---------

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
2026-10-14 00:01:26 -07:00

36 lines
1.5 KiB
PHP

<?php
// Copyright (C) 2004-2026 The Cacti Group. GPL-2.0-or-later.
require_once dirname(__DIR__, 2) . '/Helpers/PhpSource.php';
use Cacti\Tests\Helpers\PhpSource;
test('PHP source contracts tolerate whitespace without changing literal contents', function () {
$source = "<?php guard(\n\t\$path, /* boundary */\n\t'line\\n literal');";
expect(PhpSource::position($source, "guard(\$path, 'line\\n literal')"))->toBe(6)
->and(PhpSource::position($source, "guard(\$path, 'line literal')"))->toBeFalse();
});
test('PHP source contracts reject text in comments or quoted strings', function () {
$source = '<?php /* guard($path); */ $message = \'guard($path);\';';
expect(PhpSource::count($source, 'guard($path)'))->toBe(0);
});
test('PHP source contracts retain argument order and original byte offsets', function () {
$source = "<?php guard(\n\t\$path, \$base);\nguard(\$base, \$path);";
expect(PhpSource::count($source, 'guard($path, $base)'))->toBe(1)
->and(PhpSource::position($source, 'guard($base, $path)'))->toBe(strrpos($source, 'guard'));
});
test('PHP whitespace normalization preserves literal contents and removes comment-only contracts', function () {
$source = '<?php $value = "one two\\n";' . "\n" .
'/* db_qstr("comment only"); */ db_qstr(' . "\n\t" . '$value' . "\n" . ');';
$normalized = \Cacti\Tests\Helpers\PhpSource::normalizeWhitespace($source);
expect($normalized)->toContain('"one two\\n"');
expect($normalized)->not->toContain('comment only');
expect($normalized)->toContain('db_qstr( $value );');
});