Skip to content

PHPC-1057: Refactor option parsing for execute methods #707

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 18, 2017

Conversation

jmikola
Copy link
Member

@jmikola jmikola commented Dec 15, 2017

https://jira.mongodb.org/browse/PHPC-1057
https://jira.mongodb.org/browse/PHPC-1066

This depends on the initial session implementation in #680. I've split up the PRs to make reviewing a bit easier, although I'll note that the option parsing refactoring in this PR was necessary to finish the session API (phongo_execute_query in particular).

Since I ended up improving the error tests to cover all invalid, known options, I ended up moving (and enhancing) tests for unknown options into separate files. Those split tests for unknown options now have XFAIL blocks depending on PHPC-1066. There are no other changes related to PHPC-1066 in this PR, but I didn't want to lose track of any existing test cases we had.

The other two commits for fixing titles in server tests and updating server_id to uint32_t are just small cleanup tasks.

@jmikola jmikola requested a review from derickr December 15, 2017 06:29
@jmikola jmikola mentioned this pull request Dec 15, 2017
@jmikola
Copy link
Member Author

jmikola commented Dec 15, 2017

@derickr Note that Travis builds are failing because STANDALONE is not running MongoDB 3.6. I don't recall if we had an open ticket to address that, or if we only spoke about fixing that before 1.4.0.

Copy link
Contributor

@derickr derickr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have a look at the Travis failures. PHP 5 and 7 both have (different) issues.

php_phongo.c Outdated
}

if (Z_TYPE_P(options) != IS_ARRAY) {
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected options to be array, %s given", zend_get_type_by_const(Z_TYPE_P(options)));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should use the new macro to show object name.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I implemented #705 independently and wasn't about to rebase this atop it. I do intend to update all of these to use the new macros after #705 is merged and I rebase, though.

php_phongo.c Outdated
}
}
if (Z_TYPE_P(option) != IS_OBJECT || !instanceof_function(Z_OBJCE_P(option), php_phongo_readconcern_ce TSRMLS_CC)) {
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected \"readConcern\" option to be %s, %s given", ZSTR_VAL(php_phongo_readconcern_ce->name), zend_get_type_by_const(Z_TYPE_P(option)));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto, and I suspect in other places too

$bulk->insert(['wc' => 1]);
$manager->executeBulkWrite(NS, $bulk, ['readPreference' => 'foo']);
$bulk->insert(['x' => 1]);
$manager->executeBulkWrite(NS, $bulk, ['writeConcern' => new stdClass]);
}, 'MongoDB\Driver\Exception\InvalidArgumentException'), "\n";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should try to minimize changes to tests. There was no need to change the order, and to change "wc" to "x" for the writeConcern options for example.

Copy link
Member Author

@jmikola jmikola Dec 15, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noted re: "wc" to "x". The order changed as I basically wrote one test for all options and then copy/pasted it throughout all of the myriad of execute method tests, stripping out irrelevant options for each test based on what the method took. I made sure to leave string and object test cases for each invalid option, though, and used just a string for unknown options (as you originally had).

@jmikola
Copy link
Member Author

jmikola commented Dec 15, 2017

Have a look at the Travis failures. PHP 5 and 7 both have (different) issues.

Looks like php_phongo_prep_legacy_option_free() was missing a TSRMLS_DC, which Travis caught on a PHP 5 ZTS build. Fixed.

@jmikola
Copy link
Member Author

jmikola commented Dec 15, 2017

Rebased on master and #680 and using the new PHONGO_ZVAL_CLASS_OR_TYPE_NAME() macro. Expected output in tests has been updated accordingly.

Execute options are now parsed via php_array functions, which makes the options case-sensitive.

A helper function is now used to convert legacy options for the original three execute methods into an array. This simplifies option parsing in php_phongo.c.

Additionally, Manager methods now perform their own server selection, which means that execute functions in php_phongo.c can rely on a server_id being provided.

Tests have been updated to expect a serverId option when relevant (e.g. query opts). Additionally, error tests for execute methods have been improved so that invalid values for all known options are tested.
These tests were extracted from previous tests that combined invalid/unknown options.
Since Manager methods no longer pass -1 to execute functions in php_phongo.c, server_id should always be unsigned.
Copy link
Contributor

@derickr derickr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, but I'm dubious about the duplicate enum value.

PHONGO_OPTION_WRITE_CONCERN = 0x04,
PHONGO_COMMAND_RAW = 0x07,
PHONGO_COMMAND_READ = 0x03,
PHONGO_COMMAND_WRITE = 0x04,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure you can have duplicate enum values?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jmikola jmikola merged commit c94405a into mongodb:master Dec 18, 2017
jmikola added a commit that referenced this pull request Dec 18, 2017
@jmikola jmikola deleted the phpc-1057 branch December 18, 2017 19:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants