Linux SRV-16 6.18.44-paas #1 SMP PREEMPT_DYNAMIC Thu Aug 13 10:08:12 UTC 2026 x86_64
/
opt
/
php-8.2.24-202410171016
/
share
/
test
/
mongodb
/
tests
/
bson
/
//opt/php-8.2.24-202410171016/share/test/mongodb/tests/bson/bson-document-fromPHP-001.phpt
--TEST-- MongoDB\BSON\Document::fromPHP(): Serializable::bsonSerialize() returns array or stdClass --FILE-- <?php require_once __DIR__ . '/../utils/basic.inc'; class MyDocument implements MongoDB\BSON\Serializable { private $data; public function __construct($data = null) { $this->data = $data; } #[ReturnTypeWillChange] public function bsonSerialize() { return $this->data; } } $tests = [ [1, 2, 3], ['foo' => 'bar'], (object) [1, 2, 3], (object) ['foo' => 'bar'], ]; echo "Testing top-level objects\n"; foreach ($tests as $test) { try { echo MongoDB\BSON\Document::fromPHP(new MyDocument($test))->toRelaxedExtendedJson(), "\n"; } catch (MongoDB\Driver\Exception\UnexpectedValueException $e) { echo $e->getMessage(), "\n"; } } echo "\nTesting nested objects\n"; foreach ($tests as $test) { try { echo MongoDB\BSON\Document::fromPHP(new MyDocument(['nested' => new MyDocument($test)]))->toRelaxedExtendedJson(), "\n"; } catch (MongoDB\Driver\Exception\UnexpectedValueException $e) { echo $e->getMessage(), "\n"; } } ?> ===DONE=== <?php exit(0); ?> --EXPECT-- Testing top-level objects { "0" : 1, "1" : 2, "2" : 3 } { "foo" : "bar" } { "0" : 1, "1" : 2, "2" : 3 } { "foo" : "bar" } Testing nested objects { "nested" : [ 1, 2, 3 ] } { "nested" : { "foo" : "bar" } } { "nested" : { "0" : 1, "1" : 2, "2" : 3 } } { "nested" : { "foo" : "bar" } } ===DONE===