|
| 1 | +--TEST-- |
| 2 | +BSON\fromPHP(): bsonSerialize() must return an array or stdClass |
| 3 | +--SKIPIF-- |
| 4 | +<?php require __DIR__ . "/../utils/basic-skipif.inc"?> |
| 5 | +--FILE-- |
| 6 | +<?php |
| 7 | +use MongoDB\BSON as BSON; |
| 8 | + |
| 9 | +require_once __DIR__ . "/../utils/basic.inc"; |
| 10 | + |
| 11 | +class MyDocument implements BSON\Serializable |
| 12 | +{ |
| 13 | + private $data; |
| 14 | + |
| 15 | + public function __construct($data = null) |
| 16 | + { |
| 17 | + $this->data = $data; |
| 18 | + } |
| 19 | + |
| 20 | + public function bsonSerialize() |
| 21 | + { |
| 22 | + return $this->data; |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +$invalidValues = array(null, 123, 'foo', true, new MyDocument); |
| 27 | + |
| 28 | +echo "Testing top-level objects\n"; |
| 29 | + |
| 30 | +foreach ($invalidValues as $invalidValue) { |
| 31 | + try { |
| 32 | + hex_dump(fromPHP(new MyDocument($invalidValue))); |
| 33 | + } catch (MongoDB\Driver\Exception\UnexpectedValueException $e) { |
| 34 | + echo $e->getMessage(), "\n"; |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +echo "\nTesting nested objects\n"; |
| 39 | + |
| 40 | +foreach ($invalidValues as $invalidValue) { |
| 41 | + try { |
| 42 | + hex_dump(fromPHP(new MyDocument(array('nested' => new MyDocument($invalidValue))))); |
| 43 | + } catch (MongoDB\Driver\Exception\UnexpectedValueException $e) { |
| 44 | + echo $e->getMessage(), "\n"; |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +?> |
| 49 | +===DONE=== |
| 50 | +<?php exit(0); ?> |
| 51 | +--EXPECTF-- |
| 52 | +Testing top-level objects |
| 53 | +Expected MyDocument::bsonSerialize() to return an array or stdClass, null given |
| 54 | +Expected MyDocument::bsonSerialize() to return an array or stdClass, integer given |
| 55 | +Expected MyDocument::bsonSerialize() to return an array or stdClass, string given |
| 56 | +Expected MyDocument::bsonSerialize() to return an array or stdClass, boolean given |
| 57 | +Expected MyDocument::bsonSerialize() to return an array or stdClass, MyDocument given |
| 58 | + |
| 59 | +Testing nested objects |
| 60 | +Expected MyDocument::bsonSerialize() to return an array or stdClass, null given |
| 61 | +Expected MyDocument::bsonSerialize() to return an array or stdClass, integer given |
| 62 | +Expected MyDocument::bsonSerialize() to return an array or stdClass, string given |
| 63 | +Expected MyDocument::bsonSerialize() to return an array or stdClass, boolean given |
| 64 | +Expected MyDocument::bsonSerialize() to return an array or stdClass, MyDocument given |
| 65 | +===DONE=== |
0 commit comments