Commit Graph

317 Commits

Author SHA1 Message Date
Andreas Braun
a237193eca BSON class implementations (#1387)
* Extract bson to json conversion logic

* PHPC-326: Add class to represent raw BSON document

* PHPC-326: Add class to represent raw BSON array

* Typemaps now support raw BSON type

* Short-circuit encoding of BSON zvals

* PHPC-324: Add BSONIterator class

* Polyfill zend_array_is_list on PHP < 8.1

* Add offset accessors to BSON classes

* Add more tests around iterators and fetching offsets

* Remove compare handler for BSONIterator

* Rename BSONArray to ArrayList

* Rename BSONDocument to Document

* Rename BSONIterator to Iterator

* Implement Document::fromBSONString

* Test BSON document in bson-corpus tests

* Fix ArrayList handling of BSON arrays with wrong keys

* Address code review for bson-corpus tests

* Address code review in ArrayList

* Address code review for Document

* Address code review for Iterator

* Allow BSON iterators to be rewound

* Address code review in tests

* Fix comment explaining php_phongo_bson_append_object

* Remove obsolete php_phongo_bson_append_zval

* More review changes to tests

* Return base64 encoded data when debugging BSON classes

* Allow serialisation of Document and ArrayList

* Add test for iterating past the end of a structure

* Update exception message to contain class name

* Make BSON pclass handling test more complete

* Fix wrong handling of type map in nested structures

* Ensure objects are backed by actual memory

* Validate UTF-8 BSON data before returning it

* Allow ArrayList::toPHP() to accept a different root type map

* Ignore null return type for invalid BSON iterators on PHP < 8

* Throw when calling get on non-existing BSON offsets

* Remove unnecessary length from serialised output

* Refactor names around type map handling

* Use short array syntax in new tests

* Improve test descriptions

* Restructure BSON iterator tests

* Expand BSON class serialisation tests

* Throw exception when iterator is exhausted

The previous method relied on the type system of PHP to throw an exception when accessing the key of an exhausted iterator, but this does not work on non-debug versions of PHP.

* Update test description

* Rename ArrayList class to PackedArray
2022-12-16 08:48:09 +01:00
Jeremy Mikola
594cad9a30 PHPC-2083: Revise BSON handling of enum classes
Revert previous enum instantiation behavior and PersistableEnum trait from de5f1e5a93

Backed enums will be encoded as their case value. Non-backed cannot be encoded and no enums can be encoded at the root level.

Prohibit enums from implementing Persistable and Unserializable.
2022-11-10 20:21:36 +08:00
Jeremy Mikola
7423da41aa PHPC-2159: Consider enums and traits in BSON decoding instantiatable checks
Prohibit uninstantiatable classes in type maps, and ignore them when processing __pclass fields.

Utilize zend_get_object_type_case from PHP 8.2 for more helpful error messages in php_phongo_bson_state_fetch_class.
2022-11-10 20:21:36 +08:00
levon80999
9df0785fea PHPC-2101: Default Binary constructor parameter to TYPE_GENERIC (#1362) 2022-09-15 00:23:24 +04:00
Jeremy Mikola
de5f1e5a93 PHPC-2083: Allow enums to be instantiated during BSON decoding (#1317)
Enums serialize like PHP objects with a "name" property (BackedEnum instance will also have a "value") and thus become BSON documents. In order for a document to unserialize back to an enum, it must implement either Unserializable or Persistable. The bsonUnserialize() method serves no purpose for initialization, but it will still be invoked.

This introduces a PersistableEnum trait, which can be used for both Unserializable or Persistable implementations.

Co-authored-by: Sara Golemon <pollita@php.net>
2022-08-25 23:09:45 -04:00
levon80999
c30e8a2890 PHPC-1945: Remove class_exists checks from SKIPIF blocks in Decimal128 tests 2022-08-16 12:21:42 +04:00
Andreas Braun
6ffcb1f1dd PHPC-2117: Test on PHP 8.2 (#1340)
* Test on PHP 8.2

* Fix tests relating to dynamic properties

* Fix tests failing due to different __set_state output

* Duplicate clone test without dynamic properties for PHP >= 8.2
2022-08-04 08:53:25 +02:00
Andreas Braun
6887226d34 PHPC-1709: Add typing information to arginfo (#1337)
* Add missing macros for declaring arguments

* Add macros to define disabled constructor and __wakeup

* Define arginfo via stubs for BSON classes

* Declare tentative return types in interfaces

* Add correct return type for __set_state methods

* Generate class entries for BSON classes

* Declare Binary class constants in stub file

* Use stubs in exception classes

* Add stubs for monitoring classes

* Fix wrong JsonSerializable class in BSON stubs

* Disable declaration-after-statement

This is necessary to let the generated arginfo files compile

* Use stubs for driver classes

* Fix deprecation messages in tests

* Add missing macro

* Fix tests relying on value injection

* Parse parameters in disabled constructor/wakup function

* Add note about arginfo files to contribution docs

* Add GitHub action to check generated arginfo files

* PHPC-2115: Use DateTimeInterface in UTCDateTime constructor signature

* Don't install mongodb extension in GitHub actions workflows

* Change indentation in stub files

* Use individual #if conditions for each stub method

* Make disabled constructor/wakeup methods static

* Fix bulkWrite parameter name

* Use %d to match property count in tests

* Ensure all classes with disabled serialisation declare __wakeup
2022-08-03 08:42:07 +02:00
Jeremy Mikola
bd757bbb2b PHPC-1342: Regression tests for duplicate keys (#1294) 2022-01-18 09:59:45 -05:00
Jeremy Mikola
21fd5a6af6 PHPC-1942: BSON Binary subtype 0x07 (column) (#1281)
Defines new Binary::TYPE_COLUMN constant.

BSON corpus spec tests synced with mongodb/specifications@b3b74c97c3
2021-12-06 16:38:55 -05:00
Jeremy Mikola
93bd10bd21 PHPC-1897: Test serialization of BSON with embedded null bytes in strings (#1255)
* BSON corpus prose tests for null bytes

For tests that already existed we just add a reference to the prose test

* BSON corpus tests for null bytes

Synced with mongodb/specifications@c64fc79899

This sync also removes some old DBRef tests, which should have been removed in 6929bcb608.

* Bump libmongoc to 1.19.1 to pull in changes from CDRIVER-4083
2021-10-25 13:27:33 -04:00
Andreas Braun
b27fa4d019 PHPC-1965 Re-enable PHP 8.1 build on GH Actions (#1258)
* Re-enable PHP 8.1 build on GH Actions

* Work around different error wording in PHP 8.1

* Limit failing tests to PHP < 8.1

* Add tests for PHPC-1839 for PHP >= 8.1
2021-09-07 08:18:08 +02:00
Jeremy Mikola
bd10948415 PHPC-1849: Handle deprecation of Serializable in PHP 8.1 (#1246)
* Add __serialize and __unserialize methods to Serializable classes

These methods will be used on PHP 7.4+ when available. Implementing them is required to suppress a deprecation notice on PHP 8.1+.

This commit also renames is_debug parameter for get_properties_hash utility functions (and related macros) to more accurately reflect its purpose of allocating a new HashTable instead of using an internal props table.

* Explicitly handle serialize output in php_phongo_cursorid_get_properties_hash

* Explicitly handle serialize output in php_phongo_writeconcern_get_properties_hash

* Note maxStalenessSeconds handling in php_phongo_readpreference_get_properties_hash

* CursorId should always serialize id as string
2021-08-31 22:13:11 -04:00
Jeremy Mikola
fc534446fa Merge branch 'v1.9' 2021-07-12 17:06:52 -04:00
Andreas Braun
c68cdcda76 PHPC-1839 Fix accessing referenced strings (#1223)
* PHPC-1839 Fix accessing referenced non-interned strings

* Use ZVAL_DEREF

* Update tests to not require a live server

* Use correct case for method names
2021-06-16 08:16:58 +02:00
Jeremy Mikola
c1960f330f PHPC-1813: Unpin session after aborting a transaction (#1219)
* PHPC-1813: Unpin session after aborting a transaction

Bumps libmongoc dependency to pull in changes from CDRIVER-3949

* Revise Extended JSON tests for CDRIVER-3958
2021-04-26 16:57:59 -04:00
Andreas Braun
deead9687e PHPC-1716 Allow configuring server API version in manager (#1204)
* Introduce MongoDB\Driver\ServerApi

* Accept serverApi driver option

* Introduce create_test_manager factory to create manager

A centralised entry point is required to inject the API_VERSION env variable later.

* Add build variant to test with requireApiVersion=true

* Fix wrong configuration for auth variable

This changed when migrating from our own scripts to drivers-evergreen-tools and was not updated properly, causing all tests to run with auth disabled.

* Declare ZEND_PARSE_PARAMETERS_NONE macro

This macro is missing on PHP < 7.3

* Remove duplicated API param storage

* Add missing semicolons

* Add ZEND_PARSE_PARAMETERS_NON_EX macro

* Extract error handling functionality to separate macros

* Throw if internal mongoc_server_api_t is already initialised

* Use imported namespaces in tools file

* Fix type info for reflection

* Use American English spelling

* Only use typed serialize signature on PHP 8+

* Update PHONGO_PARSE_PARAMETERS_NONE macro for PHP < 7.3

* Remove usage of ZEND_STRL within zend_hash_str_add

This causes compile failures on PHP < 7.3 that I have yet to understand.

* Fix errors in new PHONGO_PARSE_PARAMETERS macros
2021-03-26 15:43:48 +01:00
Andreas Braun
60febd0f28 PHPC-1699: Ensure all argument parsing errors throw InvalidArgumentException (#1173)
Co-authored-by: Jeremy Mikola <jmikola@gmail.com>
2020-10-28 08:20:32 +01:00
Jeremy Mikola
ca4c52eb1c PHPC-1692: Test suite fixes for PHP 8 2020-10-13 15:24:02 +02:00
Jeremy Mikola
354bcc9441 PHPC-1598: Fix get_gc handlers for classes with get_properties
The ReadConcern, ReadPreference, WriteConcern, and BSON classes have no properties beyond pointers to libmongoc/libbson structs; however, all of these classes use get_properties to report fields for var_export (PHPC-850, PHPC-460).

The standard get_gc handler defers to get_properties to collect other zvals for GC inspection. This is problematic for the aforementioned get_properties handlers, since a HashTable of zvals intended for debugging will be returned and those properties will already be freed by our free_object handler (via FREE_HASHTABLE).

Having each class define its own get_gc handler is the first step to fixing this issue. The BSON classes already defined their own get_gc handlers, but erroneously returned the internally cached properties directly.

The second step to fixing this issue is ensuring that get_gc delegates to zend_std_get_properties, as is done in various PHP core extensions. Doing so ensures that we do not leak other zvals that may be assigned as public properties by the application (covered by the second regression test), since zend_std_get_properties will return those for GC inspection.
2020-05-19 18:14:53 -04:00
Andreas Braun
545db4eabd PHPC-1499: Expose BSON constant for subtype 6 2020-01-22 08:18:32 +01:00
Jeremy Mikola
639484345d PHPC-1470: Remove unnecessary timezone settings in tests 2019-11-13 13:30:55 -05:00
Jeremy Mikola
ebc1873e33 PHPC-1492: Remove unnecessary EXPECTF patterns
UTCDateTime timestamps are always printed as strings.
2019-11-13 13:30:12 -05:00
Andreas Braun
aed838497b PHPC-1230: Test that user properties in BSON objects are cloned 2019-10-30 06:59:18 +01:00
Andreas Braun
108f2b7787 PHPC-1230: Allow cloning UTCDateTime objects 2019-10-30 06:59:18 +01:00
Andreas Braun
87efc1b59d PHPC-1230: Allow cloning Undefined objects 2019-10-30 06:59:18 +01:00
Andreas Braun
847a02ff42 PHPC-1230: Allow cloning Timestamp objects 2019-10-30 06:59:18 +01:00
Andreas Braun
fd1984a59f PHPC-1230: Allow cloning Symbol objects 2019-10-30 06:59:18 +01:00
Andreas Braun
625ffaf54c PHPC-1230: Allow cloning regex objects 2019-10-30 06:59:18 +01:00
Andreas Braun
4b799ecce5 PHPC-1230: Allow cloning *key objects 2019-10-30 06:59:18 +01:00
Andreas Braun
519ded4324 PHPC-1230: Allow cloning Javascript objects 2019-10-30 06:59:18 +01:00
Andreas Braun
65aa7932e9 PHPC-1230: Allow cloning Int64 objects 2019-10-30 06:51:25 +01:00
Andreas Braun
4788a1a6f6 PHPC-1230: Allow cloning Decimal128 objects 2019-10-30 06:51:25 +01:00
Andreas Braun
117206db72 PHPC-1230: Allow cloning DBPointer objects 2019-10-30 06:51:25 +01:00
Andreas Braun
051694ddfb PHPC-1230: Allow cloning Binary objects 2019-10-30 06:51:25 +01:00
Andreas Braun
79afd5dbea PHPC-1230: Allow cloning ObjectID objects 2019-10-30 06:51:25 +01:00
Andreas Braun
15a1cdc24b PHPC-1254: Implement test plan for ObjectID spec 2019-08-22 09:19:57 +02:00
Jeremy Mikola
cdc8be38f1 toJSON() may encode NAN as -nan(ind) on Windows 2019-02-20 13:20:26 -05:00
Derick Rethans
82de814be3 PHPC-1266: Empty deeply nested BSON document causes unallocated memory writes 2018-09-07 13:04:07 +01:00
Derick Rethans
9778b84da7 PHPC-1263: Fix tests for PHP 7.3 output changes 2018-08-30 11:49:01 +01:00
Jeremy Mikola
9f118a2229 PHPC-1211: Int64 class to wrap 64-bit integers on 32-bit platforms 2018-06-22 13:07:33 -04:00
Derick Rethans
98e583b738 PHPC-1175: Fixed test cases due to changes in variable type names in PHP 7.3 2018-05-17 16:18:27 +02:00
Jeremy Mikola
58fa85e85e PHPC-1113: Use common URI constant 2018-05-02 16:04:09 -04:00
Jeremy Mikola
5f7f350670 PHPC-1113: Replace NEEDS('STANDALONE') with skip function 2018-05-02 16:04:08 -04:00
Jeremy Mikola
b4062f880c PHPC-1113: Replace CLEANUP() with skip function 2018-05-02 16:04:08 -04:00
Derick Rethans
032f08fe4b PHPC-314: Added a few tests that don't touch the server 2018-04-25 11:55:10 +01:00
Derick Rethans
da8853a092 Move test case that doesn't touch server to BSON\toPHP() 2018-04-18 11:02:56 +01:00
Derick Rethans
b42165eecb Tweaks to test cases 2018-04-18 11:02:56 +01:00
Derick Rethans
e7e9fe76e6 PHPC-1154: Fix memory leak with typemaps and MongoDB\BSON\toPHP 2018-04-18 11:02:56 +01:00
Derick Rethans
eddcd4bc5b PHPC-1154: Implement wild card syntax for typemap field paths 2018-04-18 11:02:56 +01:00