Skip to content

Commit b309cf8

Browse files
alcaeusjmikola
andauthored
PHPLIB-981: Add tests for examples (#1002)
* 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 <[email protected]> * 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 <[email protected]>
1 parent 035d367 commit b309cf8

File tree

9 files changed

+269
-29
lines changed

9 files changed

+269
-29
lines changed

examples/aggregate.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ function toJSON(object $document): string
2222

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

25-
$collection = $client->test->coll;
25+
$collection = $client->test->aggregate;
2626
$collection->drop();
2727

2828
$documents = [];

examples/bulk.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
namespace MongoDB\Examples;
55

66
use MongoDB\Client;
7+
use MongoDB\Driver\WriteConcern;
78

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

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

24-
$collection = $client->test->coll;
25+
$collection = $client->test->bulk;
2526
$collection->drop();
2627

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

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

3536
$collection->bulkWrite(
3637
[

examples/changestream.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,13 @@
66
use MongoDB\Client;
77

88
use function assert;
9-
use function fprintf;
109
use function getenv;
1110
use function is_object;
1211
use function MongoDB\BSON\fromPHP;
1312
use function MongoDB\BSON\toRelaxedExtendedJSON;
1413
use function printf;
1514
use function time;
1615

17-
use const STDERR;
18-
1916
require __DIR__ . '/../vendor/autoload.php';
2017

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

29-
$collection = $client->test->coll;
26+
$collection = $client->test->changestream;
3027
$collection->drop();
3128

29+
// Create collection before starting change stream; this is required on MongoDB 3.6
30+
$client->test->createCollection('changestream');
31+
3232
$changeStream = $collection->watch();
3333

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

5555
if (time() - $startTime > 3) {
56-
fprintf(STDERR, "Aborting after 3 seconds...\n");
56+
printf("Aborting after 3 seconds...\n");
5757
break;
5858
}
5959
}

examples/command_logger.php

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,13 @@
1010
use MongoDB\Driver\Monitoring\CommandSucceededEvent;
1111

1212
use function assert;
13-
use function fprintf;
1413
use function get_class;
1514
use function getenv;
1615
use function is_object;
1716
use function MongoDB\BSON\fromPHP;
1817
use function MongoDB\BSON\toRelaxedExtendedJSON;
1918
use function printf;
2019

21-
use const STDERR;
22-
2320
require __DIR__ . '/../vendor/autoload.php';
2421

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

36-
fprintf(STDERR, "command: %s\n", toJson($event->getCommand()));
37-
fprintf(STDERR, "\n");
33+
printf("command: %s\n", toJson($event->getCommand()));
34+
printf("\n");
3835
}
3936

4037
public function commandSucceeded(CommandSucceededEvent $event): void
4138
{
42-
fprintf(STDERR, "%s command succeeded\n", $event->getCommandName());
43-
fprintf(STDERR, "reply: %s\n", toJson($event->getReply()));
44-
fprintf(STDERR, "\n");
39+
printf("%s command succeeded\n", $event->getCommandName());
40+
printf("reply: %s\n", toJson($event->getReply()));
41+
printf("\n");
4542
}
4643

4744
public function commandFailed(CommandFailedEvent $event): void
4845
{
49-
fprintf(STDERR, "%s command failed\n", $event->getCommandName());
50-
fprintf(STDERR, "reply: %s\n", toJson($event->getReply()));
46+
printf("%s command failed\n", $event->getCommandName());
47+
printf("reply: %s\n", toJson($event->getReply()));
5148

5249
$exception = $event->getError();
53-
fprintf(STDERR, "exception: %s\n", get_class($exception));
54-
fprintf(STDERR, "exception.code: %d\n", $exception->getCode());
55-
fprintf(STDERR, "exception.message: %s\n", $exception->getMessage());
56-
fprintf(STDERR, "\n");
50+
printf("exception: %s\n", get_class($exception));
51+
printf("exception.code: %d\n", $exception->getCode());
52+
printf("exception.message: %s\n", $exception->getMessage());
53+
printf("\n");
5754
}
5855
}
5956

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

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

64-
$collection = $client->test->coll;
61+
$collection = $client->test->command_logger;
6562
$collection->drop();
6663

6764
$collection->insertMany([

examples/persistable.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public function bsonUnserialize(array $data): void
9898

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

101-
$collection = $client->test->coll;
101+
$collection = $client->test->persistable;
102102
$collection->drop();
103103

104104
$collection->insertOne($entry);

examples/typemap.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public function bsonUnserialize(array $data): void
9393

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

96-
$collection = $client->test->coll;
96+
$collection = $client->test->typemap;
9797
$collection->drop();
9898

9999
$document = [

examples/with_transaction.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,12 @@ function toJSON(object $document): string
2424
// Transactions require a replica set (MongoDB >= 4.0) or sharded cluster (MongoDB >= 4.2)
2525
$client = new Client(getenv('MONGODB_URI') ?: 'mongodb://127.0.0.1/');
2626

27-
$collection = $client->test->coll;
27+
$collection = $client->test->with_transaction;
2828
$collection->drop();
2929

30+
// Create collection outside of transaction; this is required when using MongoDB < 4.4
31+
$client->test->createCollection('with_transaction');
32+
3033
$insertData = function (Session $session) use ($collection): void {
3134
$collection->insertMany(
3235
[

psalm-baseline.xml

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,37 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<files psalm-version="4.x-dev@5108834088c1d8cae3698df6ef6bf15fe6e76c53">
2+
<files psalm-version="4.30.0@d0bc6e25d89f649e4f36a534f330f8bb4643dd69">
3+
<file src="examples/aggregate.php">
4+
<MixedInferredReturnType occurrences="1">
5+
<code>string</code>
6+
</MixedInferredReturnType>
7+
<MixedReturnStatement occurrences="1">
8+
<code>toRelaxedExtendedJSON(fromPHP($document))</code>
9+
</MixedReturnStatement>
10+
</file>
11+
<file src="examples/bulk.php">
12+
<MixedInferredReturnType occurrences="1">
13+
<code>string</code>
14+
</MixedInferredReturnType>
15+
<MixedReturnStatement occurrences="1">
16+
<code>toRelaxedExtendedJSON(fromPHP($document))</code>
17+
</MixedReturnStatement>
18+
</file>
19+
<file src="examples/changestream.php">
20+
<MixedInferredReturnType occurrences="1">
21+
<code>string</code>
22+
</MixedInferredReturnType>
23+
<MixedReturnStatement occurrences="1">
24+
<code>toRelaxedExtendedJSON(fromPHP($document))</code>
25+
</MixedReturnStatement>
26+
</file>
27+
<file src="examples/command_logger.php">
28+
<MixedInferredReturnType occurrences="1">
29+
<code>string</code>
30+
</MixedInferredReturnType>
31+
<MixedReturnStatement occurrences="1">
32+
<code>toRelaxedExtendedJSON(fromPHP($document))</code>
33+
</MixedReturnStatement>
34+
</file>
335
<file src="examples/typemap.php">
436
<PropertyNotSetInConstructor occurrences="5">
537
<code>$address</code>
@@ -9,6 +41,14 @@
941
<code>$type</code>
1042
</PropertyNotSetInConstructor>
1143
</file>
44+
<file src="examples/with_transaction.php">
45+
<MixedInferredReturnType occurrences="1">
46+
<code>string</code>
47+
</MixedInferredReturnType>
48+
<MixedReturnStatement occurrences="1">
49+
<code>toRelaxedExtendedJSON(fromPHP($document))</code>
50+
</MixedReturnStatement>
51+
</file>
1252
<file src="src/Client.php">
1353
<MixedArgument occurrences="1">
1454
<code>$driverOptions['driver'] ?? []</code>
@@ -357,7 +397,8 @@
357397
<code>$args[1]</code>
358398
<code>$args[2]</code>
359399
</MixedArgument>
360-
<MixedArrayAccess occurrences="24">
400+
<MixedArrayAccess occurrences="25">
401+
<code>$args[0]</code>
361402
<code>$args[0]</code>
362403
<code>$args[0]</code>
363404
<code>$args[0]</code>

0 commit comments

Comments
 (0)