1
0
mirror of https://github.com/php/php-src.git synced 2026-04-29 03:03:26 +02:00
Files
archived-php-src/Zend/tests/offsets/invalid_container_offset_behaviour.phpt
Gina Peter Banyard a479ed7cc5 Zend: Add tests for offsets and containers (#12723)
Add various tests showcasing the behavioural variations of different offset types used on various container types in all possible operations:
 - Read
 - Write
 - Read Write
 - Appending
 - Unsetting
 - Existence checks with isset()/empty/null coalesce operator
 - Behaviour of nesting dimensions (e.g. $container[$offset1][$offset2])

Add a test to ensure compile time and runtime behaviour is identical for offsets.

Add an internal class that overloads the dimension object handlers to zend_test
2024-01-25 15:06:59 +00:00

93 lines
1.9 KiB
PHP

--TEST--
Invalid containers with offsets
--FILE--
<?php
require_once __DIR__ . DIRECTORY_SEPARATOR . 'test_offset_helpers.inc';
$containers = [
//false,
true,
4,
5.5,
STDERR,
//new stdClass(),
];
ob_start();
foreach ($containers as $container) {
$containerStr = get_zend_debug_type($container);
$EXPECTED_OUTPUT = <<<OUTPUT
Read before write:
Warning: Trying to access array offset on $containerStr in %s on line 8
NULL
Write:
Cannot use a scalar value as an array
Read:
Warning: Trying to access array offset on $containerStr in %s on line 22
NULL
Read-Write:
Cannot use a scalar value as an array
isset():
bool(false)
empty():
bool(true)
null coalesce:
string(7) "default"
unset():
Cannot unset offset in a non-array variable
Nested read:
Warning: Trying to access array offset on $containerStr in %s on line 62
Warning: Trying to access array offset on null in %s on line 62
NULL
Nested write:
Cannot use a scalar value as an array
Nested Read-Write:
Cannot use a scalar value as an array
Nested isset():
bool(false)
Nested empty():
bool(true)
Nested null coalesce:
string(7) "default"
Nested unset():
Cannot unset offset in a non-array variable
OUTPUT;
foreach ($offsets as $dimension) {
$error = $containerStr . '[' . zend_test_var_export($dimension) . '] has different outputs' . "\n";
include $var_dim_filename;
$varOutput = ob_get_contents();
ob_clean();
$varOutput = str_replace(
[$var_dim_filename],
['%s'],
$varOutput
);
if ($EXPECTED_OUTPUT !== $varOutput) {
file_put_contents(__DIR__ . DIRECTORY_SEPARATOR . "debug_invalid_container_{$failuresNb}.txt", $varOutput);
++$failuresNb;
$failures[] = $error;
}
++$testCasesTotal;
}
}
ob_end_clean();
echo "Executed tests\n";
if ($failures !== []) {
echo "Failures:\n" . implode($failures);
}
?>
--EXPECT--
Executed tests