Skip to content

Commit

Permalink
PHPLIB-981: Add tests for examples (#1002)
Browse files Browse the repository at this point in the history
* No longer print to STDERR

* PHPLIB-981: Test example scripts

* Relax command/reply assertions

* Create test collection in transaction example to avoid errors on MongoDB <= 4.2

* Update command logger test output to handle collection differences

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.

* Create test collection in change stream example

* Use majority write concern in bulk example

* Fix psalm errors

* Use different collection names in each example

This should help reduce test failures

* Skip example tests on sharded clusters

* Update psalm-baseline on PHP 7.4

* Update collection names in tests

Co-authored-by: Jeremy Mikola <jmikola@gmail.com>

* Exclude bulk write psalm error

Fixing this requires a larger-scale refactoring of the types involved, which was deferred to a later date.

Co-authored-by: Jeremy Mikola <jmikola@gmail.com>
  • Loading branch information
alcaeus and jmikola committed Nov 22, 2022
1 parent 035d367 commit b309cf8
Show file tree
Hide file tree
Showing 9 changed files with 269 additions and 29 deletions.
2 changes: 1 addition & 1 deletion examples/aggregate.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function toJSON(object $document): string

$client = new Client(getenv('MONGODB_URI') ?: 'mongodb://127.0.0.1/');

$collection = $client->test->coll;
$collection = $client->test->aggregate;
$collection->drop();

$documents = [];
Expand Down
5 changes: 3 additions & 2 deletions examples/bulk.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace MongoDB\Examples;

use MongoDB\Client;
use MongoDB\Driver\WriteConcern;

use function assert;
use function getenv;
Expand All @@ -21,7 +22,7 @@ function toJSON(object $document): string

$client = new Client(getenv('MONGODB_URI') ?: 'mongodb://127.0.0.1/');

$collection = $client->test->coll;
$collection = $client->test->bulk;
$collection->drop();

$documents = [];
Expand All @@ -30,7 +31,7 @@ function toJSON(object $document): string
$documents[] = ['x' => $i];
}

$collection->insertMany($documents);
$collection->insertMany($documents, ['writeConcern' => new WriteConcern('majority')]);

$collection->bulkWrite(
[
Expand Down
10 changes: 5 additions & 5 deletions examples/changestream.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,13 @@
use MongoDB\Client;

use function assert;
use function fprintf;
use function getenv;
use function is_object;
use function MongoDB\BSON\fromPHP;
use function MongoDB\BSON\toRelaxedExtendedJSON;
use function printf;
use function time;

use const STDERR;

require __DIR__ . '/../vendor/autoload.php';

function toJSON(object $document): string
Expand All @@ -26,9 +23,12 @@ function toJSON(object $document): string
// Change streams require a replica set or sharded cluster
$client = new Client(getenv('MONGODB_URI') ?: 'mongodb://127.0.0.1/');

$collection = $client->test->coll;
$collection = $client->test->changestream;
$collection->drop();

// Create collection before starting change stream; this is required on MongoDB 3.6
$client->test->createCollection('changestream');

$changeStream = $collection->watch();

$documents = [];
Expand All @@ -53,7 +53,7 @@ function toJSON(object $document): string
$changeStream->next();

if (time() - $startTime > 3) {
fprintf(STDERR, "Aborting after 3 seconds...\n");
printf("Aborting after 3 seconds...\n");
break;
}
}
29 changes: 13 additions & 16 deletions examples/command_logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,13 @@
use MongoDB\Driver\Monitoring\CommandSucceededEvent;

use function assert;
use function fprintf;
use function get_class;
use function getenv;
use function is_object;
use function MongoDB\BSON\fromPHP;
use function MongoDB\BSON\toRelaxedExtendedJSON;
use function printf;

use const STDERR;

require __DIR__ . '/../vendor/autoload.php';

function toJSON(object $document): string
Expand All @@ -31,37 +28,37 @@ class CommandLogger implements CommandSubscriber
{
public function commandStarted(CommandStartedEvent $event): void
{
fprintf(STDERR, "%s command started\n", $event->getCommandName());
printf("%s command started\n", $event->getCommandName());

fprintf(STDERR, "command: %s\n", toJson($event->getCommand()));
fprintf(STDERR, "\n");
printf("command: %s\n", toJson($event->getCommand()));
printf("\n");
}

public function commandSucceeded(CommandSucceededEvent $event): void
{
fprintf(STDERR, "%s command succeeded\n", $event->getCommandName());
fprintf(STDERR, "reply: %s\n", toJson($event->getReply()));
fprintf(STDERR, "\n");
printf("%s command succeeded\n", $event->getCommandName());
printf("reply: %s\n", toJson($event->getReply()));
printf("\n");
}

public function commandFailed(CommandFailedEvent $event): void
{
fprintf(STDERR, "%s command failed\n", $event->getCommandName());
fprintf(STDERR, "reply: %s\n", toJson($event->getReply()));
printf("%s command failed\n", $event->getCommandName());
printf("reply: %s\n", toJson($event->getReply()));

$exception = $event->getError();
fprintf(STDERR, "exception: %s\n", get_class($exception));
fprintf(STDERR, "exception.code: %d\n", $exception->getCode());
fprintf(STDERR, "exception.message: %s\n", $exception->getMessage());
fprintf(STDERR, "\n");
printf("exception: %s\n", get_class($exception));
printf("exception.code: %d\n", $exception->getCode());
printf("exception.message: %s\n", $exception->getMessage());
printf("\n");
}
}

$client = new Client(getenv('MONGODB_URI') ?: 'mongodb://127.0.0.1/');

$client->getManager()->addSubscriber(new CommandLogger());

$collection = $client->test->coll;
$collection = $client->test->command_logger;
$collection->drop();

$collection->insertMany([
Expand Down
2 changes: 1 addition & 1 deletion examples/persistable.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public function bsonUnserialize(array $data): void

$client = new Client(getenv('MONGODB_URI') ?: 'mongodb://127.0.0.1/');

$collection = $client->test->coll;
$collection = $client->test->persistable;
$collection->drop();

$collection->insertOne($entry);
Expand Down
2 changes: 1 addition & 1 deletion examples/typemap.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public function bsonUnserialize(array $data): void

$client = new Client(getenv('MONGODB_URI') ?: 'mongodb://127.0.0.1/');

$collection = $client->test->coll;
$collection = $client->test->typemap;
$collection->drop();

$document = [
Expand Down
5 changes: 4 additions & 1 deletion examples/with_transaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@ function toJSON(object $document): string
// Transactions require a replica set (MongoDB >= 4.0) or sharded cluster (MongoDB >= 4.2)
$client = new Client(getenv('MONGODB_URI') ?: 'mongodb://127.0.0.1/');

$collection = $client->test->coll;
$collection = $client->test->with_transaction;
$collection->drop();

// Create collection outside of transaction; this is required when using MongoDB < 4.4
$client->test->createCollection('with_transaction');

$insertData = function (Session $session) use ($collection): void {
$collection->insertMany(
[
Expand Down
45 changes: 43 additions & 2 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,37 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="4.x-dev@5108834088c1d8cae3698df6ef6bf15fe6e76c53">
<files psalm-version="4.30.0@d0bc6e25d89f649e4f36a534f330f8bb4643dd69">
<file src="examples/aggregate.php">
<MixedInferredReturnType occurrences="1">
<code>string</code>
</MixedInferredReturnType>
<MixedReturnStatement occurrences="1">
<code>toRelaxedExtendedJSON(fromPHP($document))</code>
</MixedReturnStatement>
</file>
<file src="examples/bulk.php">
<MixedInferredReturnType occurrences="1">
<code>string</code>
</MixedInferredReturnType>
<MixedReturnStatement occurrences="1">
<code>toRelaxedExtendedJSON(fromPHP($document))</code>
</MixedReturnStatement>
</file>
<file src="examples/changestream.php">
<MixedInferredReturnType occurrences="1">
<code>string</code>
</MixedInferredReturnType>
<MixedReturnStatement occurrences="1">
<code>toRelaxedExtendedJSON(fromPHP($document))</code>
</MixedReturnStatement>
</file>
<file src="examples/command_logger.php">
<MixedInferredReturnType occurrences="1">
<code>string</code>
</MixedInferredReturnType>
<MixedReturnStatement occurrences="1">
<code>toRelaxedExtendedJSON(fromPHP($document))</code>
</MixedReturnStatement>
</file>
<file src="examples/typemap.php">
<PropertyNotSetInConstructor occurrences="5">
<code>$address</code>
Expand All @@ -9,6 +41,14 @@
<code>$type</code>
</PropertyNotSetInConstructor>
</file>
<file src="examples/with_transaction.php">
<MixedInferredReturnType occurrences="1">
<code>string</code>
</MixedInferredReturnType>
<MixedReturnStatement occurrences="1">
<code>toRelaxedExtendedJSON(fromPHP($document))</code>
</MixedReturnStatement>
</file>
<file src="src/Client.php">
<MixedArgument occurrences="1">
<code>$driverOptions['driver'] ?? []</code>
Expand Down Expand Up @@ -357,7 +397,8 @@
<code>$args[1]</code>
<code>$args[2]</code>
</MixedArgument>
<MixedArrayAccess occurrences="24">
<MixedArrayAccess occurrences="25">
<code>$args[0]</code>
<code>$args[0]</code>
<code>$args[0]</code>
<code>$args[0]</code>
Expand Down
Loading

0 comments on commit b309cf8

Please sign in to comment.