Skip to content

Commit 3f2e3aa

Browse files
committed
Add tests on gridfs examples
1 parent cfa3b9c commit 3f2e3aa

File tree

6 files changed

+43
-9
lines changed

6 files changed

+43
-9
lines changed
File renamed without changes.

examples/gridfs-stream.php renamed to examples/gridfs_stream.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88

99
namespace MongoDB\Examples;
1010

11+
use MongoDB\BSON\ObjectId;
1112
use MongoDB\Client;
1213

14+
use function assert;
1315
use function fclose;
1416
use function feof;
1517
use function fread;
@@ -45,3 +47,10 @@
4547
}
4648

4749
echo 'Read a total of ' . $size . ' bytes' . PHP_EOL;
50+
51+
// Retrieve the file ID to delete it
52+
$id = $bucket->getFileIdForStream($stream);
53+
assert($id instanceof ObjectId);
54+
$bucket->delete($id);
55+
56+
echo 'Deleted file with ID: ' . $id . PHP_EOL;

examples/gridfs-upload.php renamed to examples/gridfs_upload.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
use function fwrite;
1818
use function getenv;
1919
use function rewind;
20+
use function stream_get_contents;
2021

2122
use const PHP_EOL;
22-
use const STDOUT;
2323

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

@@ -38,10 +38,13 @@
3838
echo 'Inserted file with ID: ' . $id . PHP_EOL;
3939
fclose($stream);
4040

41-
// Download the file and print the contents directly to STDOUT, chunk by chunk
42-
echo 'File contents: ';
43-
$gridfs->downloadToStreamByName('hello.txt', STDOUT);
44-
echo PHP_EOL;
41+
// Download the file and print the contents directly to an in-memory stream, chunk by chunk
42+
$stream = fopen('php://temp', 'w+');
43+
$gridfs->downloadToStreamByName('hello.txt', $stream);
44+
rewind($stream);
45+
echo 'File contents: ' . stream_get_contents($stream) . PHP_EOL;
4546

4647
// Delete the file
4748
$gridfs->delete($id);
49+
50+
echo 'Deleted file with ID: ' . $id . PHP_EOL;

psalm-baseline.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<code><![CDATA[$clientEncryption->decrypt($document->encryptedField)]]></code>
66
</MixedArgument>
77
</file>
8-
<file src="examples/atlas-search.php">
8+
<file src="examples/atlas_search.php">
99
<MixedArgument>
1010
<code><![CDATA[$document['name']]]></code>
1111
<code><![CDATA[$index->name]]></code>

tests/ExamplesTest.php

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,28 @@ public static function provideExamples(): Generator
9999
'expectedOutput' => $expectedOutput,
100100
];
101101

102+
$expectedOutput = <<<'OUTPUT'
103+
Read a total of 17888890 bytes
104+
Deleted file with ID: %s
105+
OUTPUT;
106+
107+
yield 'gridfs_stream' => [
108+
'file' => __DIR__ . '/../examples/gridfs_stream.php',
109+
'expectedOutput' => $expectedOutput,
110+
];
111+
112+
$expectedOutput = <<<'OUTPUT'
113+
Inserted file with ID: %s
114+
File contents: Hello world!
115+
Deleted file with ID: %s
116+
117+
OUTPUT;
118+
119+
yield 'gridfs_upload' => [
120+
'file' => __DIR__ . '/../examples/gridfs_upload.php',
121+
'expectedOutput' => $expectedOutput,
122+
];
123+
102124
$expectedOutput = <<<'OUTPUT'
103125
File exists: no
104126
Writing file
@@ -109,8 +131,8 @@ public static function provideExamples(): Generator
109131
Reading previous version of the file: Hello, GridFS!
110132
OUTPUT;
111133

112-
yield 'gridfs-stream-wrapper' => [
113-
'file' => __DIR__ . '/../examples/gridfs-stream-wrapper.php',
134+
yield 'gridfs_stream_wrapper' => [
135+
'file' => __DIR__ . '/../examples/gridfs_stream_wrapper.php',
114136
'expectedOutput' => $expectedOutput,
115137
];
116138

@@ -243,7 +265,7 @@ public function testAtlasSearch(): void
243265

244266
OUTPUT;
245267

246-
$this->assertExampleOutput(__DIR__ . '/../examples/atlas-search.php', $expectedOutput);
268+
$this->assertExampleOutput(__DIR__ . '/../examples/atlas_search.php', $expectedOutput);
247269
}
248270

249271
public function testChangeStream(): void

0 commit comments

Comments
 (0)