Skip to content

Commit 5ae1e00

Browse files
committed
Add tests on gridfs examples
1 parent c0cd6f3 commit 5ae1e00

File tree

5 files changed

+28
-7
lines changed

5 files changed

+28
-7
lines changed
File renamed without changes.
File renamed without changes.

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

Lines changed: 6 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,11 @@
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);

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: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,26 @@ public static function provideExamples(): Generator
9999
'expectedOutput' => $expectedOutput,
100100
];
101101

102+
$expectedOutput = <<<'OUTPUT'
103+
Read a total of 17888890 bytes
104+
OUTPUT;
105+
106+
yield 'gridfs_stream' => [
107+
'file' => __DIR__ . '/../examples/gridfs_stream.php',
108+
'expectedOutput' => $expectedOutput,
109+
];
110+
111+
$expectedOutput = <<<'OUTPUT'
112+
Inserted file with ID: %s
113+
File contents: Hello world!
114+
115+
OUTPUT;
116+
117+
yield 'gridfs_upload' => [
118+
'file' => __DIR__ . '/../examples/gridfs_upload.php',
119+
'expectedOutput' => $expectedOutput,
120+
];
121+
102122
$expectedOutput = <<<'OUTPUT'
103123
File exists: no
104124
Writing file
@@ -243,7 +263,7 @@ public function testAtlasSearch(): void
243263

244264
OUTPUT;
245265

246-
$this->assertExampleOutput(__DIR__ . '/../examples/atlas-search.php', $expectedOutput);
266+
$this->assertExampleOutput(__DIR__ . '/../examples/atlas_search.php', $expectedOutput);
247267
}
248268

249269
public function testChangeStream(): void

0 commit comments

Comments
 (0)