PHP-1272 et al: Add missing bson_encode() tests and fix segfaults

 * https://jira.mongodb.org/browse/PHP-1272
 * https://jira.mongodb.org/browse/PHP-1273
 * https://jira.mongodb.org/browse/PHP-1277
On a related note, flags should always be encoded in alphabetical order ([PHP-1274](https://jira.mongodb.org/browse/PHP-1274)). Not fixing this in the current PR though.