1
0
mirror of https://github.com/php/php-src.git synced 2026-04-21 15:08:16 +02:00
Files
archived-php-src/ext/random/tests/03_randomizer/construct_twice.phpt
T
Tim Düsterhus a6922fdecd Clean up the implementation of Randomizer::__construct() (#9222)
* Verify that the engine doesn't change in construct_twice.phpt

* Clean up the implementation of Randomizer::__construct()

Instead of manually checking whether the constructor was already called, we
rely on the `readonly` modifier of the `$engine` property.

Additionally use `object_init_ex()` instead of manually calling
`->create_object()`.
2022-08-02 17:30:18 +02:00

52 lines
1.2 KiB
PHP

--TEST--
Random: Randomizer: Disallow manually calling __construct
--FILE--
<?php
final class UserEngine implements \Random\Engine
{
public function generate(): string
{
return \random_byte(4); /* 32-bit */
}
}
try {
(new \Random\Randomizer())->__construct();
} catch (\Error $e) {
echo $e->getMessage() . PHP_EOL;
}
try {
$r = new \Random\Randomizer(new \Random\Engine\Xoshiro256StarStar());
$r->__construct(new \Random\Engine\PcgOneseq128XslRr64());
} catch (\Error $e) {
echo $e->getMessage() . PHP_EOL;
}
try {
$r = new \Random\Randomizer(new \UserEngine());
$r->__construct(new \UserEngine());
} catch (\Error $e) {
echo $e->getMessage() . PHP_EOL;
}
try {
$r = new \Random\Randomizer(new \Random\Engine\Xoshiro256StarStar());
$r->__construct(new \UserEngine());
} catch (\Error $e) {
echo $e->getMessage(), PHP_EOL;
}
var_dump($r->engine::class);
die('success');
?>
--EXPECT--
Cannot modify readonly property Random\Randomizer::$engine
Cannot modify readonly property Random\Randomizer::$engine
Cannot modify readonly property Random\Randomizer::$engine
Cannot modify readonly property Random\Randomizer::$engine
string(32) "Random\Engine\Xoshiro256StarStar"
success