-
Notifications
You must be signed in to change notification settings - Fork 264
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
PHPLIB-981: Add tests for examples #1002
Conversation
tests/ExamplesTest.php
Outdated
|
||
$expectedOutput = <<<'OUTPUT' | ||
drop command started | ||
command: { "drop" : "coll", "$db" : "test", "lsid" : { %s }%S } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This command output here and all subsequent command documents could be replaced with { %s }
if we're not interested in asserting the actual command results.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see any reason to assert the command contents. Just use { %s }
for everything, or rather: command: %s
. I think the only point of testing these was to ensure they actually run with no PHP errors or uncaught exceptions. We don't need the same level of scrutiny as the doc snippets that go in the MongoDB manual.
]; | ||
} | ||
|
||
public function testChangeStream(): void |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test (and testWithTransaction
) below was extracted as it needs to check whether change streams are supported. I initially wanted to provide a closure in the data provider, but that causes errors due to closures not being serialisable (which is required to run tests in isolation). Instead of passing a string callable I opted for extracting the tests from the data provider and calling the original test method manually instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
but that causes errors due to closures not being serialisable
Looks like a job for jeremeamia/super_closure! 🦸
Since deprecated for opis/closure. I'm not actually suggesting this.
fprintf(STDERR, "Aborting after 3 seconds...\n"); | ||
printf("Aborting after 3 seconds...\n"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Necessary to avoid PHPUnit considering the tests failed due to output, and to allow asserting against the full test output.
tests/ExamplesTest.php
Outdated
|
||
$expectedOutput = <<<'OUTPUT' | ||
drop command started | ||
command: { "drop" : "coll", "$db" : "test", "lsid" : { %s }%S } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see any reason to assert the command contents. Just use { %s }
for everything, or rather: command: %s
. I think the only point of testing these was to ensure they actually run with no PHP errors or uncaught exceptions. We don't need the same level of scrutiny as the doc snippets that go in the MongoDB manual.
]; | ||
} | ||
|
||
public function testChangeStream(): void |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
but that causes errors due to closures not being serialisable
Looks like a job for jeremeamia/super_closure! 🦸
Since deprecated for opis/closure. I'm not actually suggesting this.
tests/ExamplesTest.php
Outdated
{ "_id" : { "_data" : "%s" }, "operationType" : "insert", "clusterTime" : { "$timestamp" : { "t" : %d, "i" : %d } }%S, "fullDocument" : { "_id" : { "$oid" : "%s" }, "x" : 6 }, "ns" : { "db" : "test", "coll" : "coll" }, "documentKey" : { "_id" : { "$oid" : "%s" } } } | ||
{ "_id" : { "_data" : "%s" }, "operationType" : "insert", "clusterTime" : { "$timestamp" : { "t" : %d, "i" : %d } }%S, "fullDocument" : { "_id" : { "$oid" : "%s" }, "x" : 7 }, "ns" : { "db" : "test", "coll" : "coll" }, "documentKey" : { "_id" : { "$oid" : "%s" } } } | ||
{ "_id" : { "_data" : "%s" }, "operationType" : "insert", "clusterTime" : { "$timestamp" : { "t" : %d, "i" : %d } }%S, "fullDocument" : { "_id" : { "$oid" : "%s" }, "x" : 8 }, "ns" : { "db" : "test", "coll" : "coll" }, "documentKey" : { "_id" : { "$oid" : "%s" } } } | ||
{ "_id" : { "_data" : "%s" }, "operationType" : "insert", "clusterTime" : { "$timestamp" : { "t" : %d, "i" : %d } }%S, "fullDocument" : { "_id" : { "$oid" : "%s" }, "x" : 9 }, "ns" : { "db" : "test", "coll" : "coll" }, "documentKey" : { "_id" : { "$oid" : "%s" } } } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems fragile should the server introduce new response fields to change events. I'd suggest changing this to { %s }
assertions for each line. You can also lower the number of inserts in the example (maybe 5 instead of 10) if you want to make this more concise. There's really no reason it needs to emit this much output.
c6f6ef1
to
bd9d8d7
Compare
On sharded cluster, collection deletion is sometimes not propagated across the cluster, leading to different output even if we drop the collection before the test.
e4161ce
to
267535a
Compare
This should help reduce test failures
267535a
to
b58811c
Compare
@@ -11,6 +11,10 @@ public function setUp(): void | |||
{ | |||
parent::setUp(); | |||
|
|||
if ($this->isShardedCluster()) { | |||
$this->markTestSkipped('Examples are not tested on sharded clusters.'); | |||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried hard to keep those tests in, but unfortunately some tests failed on sharing due to a server error ("no progress was made executing batch op after 5 rounds"). Since we're only dealing with examples here I opted against spending more time on testing against sharded clusters and gave up trying.
Note: test failures against MongoDB latest tracked in PHPLIB-1044. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some optional suggestions to make the test collections consistent with their filename and consider a property hint instead of calling assert()
. Everything else looks fine as-is.
src/Operation/BulkWrite.php
Outdated
@@ -325,6 +326,8 @@ public function execute(Server $server) | |||
$type = key($operation); | |||
$args = current($operation); | |||
|
|||
assert(is_array($args)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this necessary? Since $operations
is private couldn't you also just add an array
property type hint to it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe the error crept up due to a change in the baseline, so I've removed this assert
call and updated the baseline instead. Fixing this would've required updating types in more than just this file, which is why it was initially deferred. We have follow-up tickets to fix issues like this one.
Co-authored-by: Jeremy Mikola <jmikola@gmail.com>
Fixing this requires a larger-scale refactoring of the types involved, which was deferred to a later date.
4cbe45a
to
f60243b
Compare
PHPLIB-981
This PR adds tests for the example scripts. To ensure good coverage, the tests attempt to match against the output of the example scripts. This requires a change in the examples to no longer output to
STDERR
, as PHPUnit considers any output toSTDERR
as condition to fail a test and doesn't allow asserting against it.When running the tests, I noticed that the tests fail on sharded clusters, because the command replies are different. This would complicate the output matchers by quite a lot, so I'm wondering if it's actually worth matching against the actual command output, or if we're happy asserting that the rough information is the same.
I will likely also change the examples for type maps and persistable classes to no longer use
var_dump
. Having xdebug installed (which is likely on dev machines) produces different output forvar_dump
, making the tests fail on developer machines.