Skip to content

Commit

Permalink
Add tests on gridfs examples
Browse files Browse the repository at this point in the history
  • Loading branch information
GromNaN committed Jan 5, 2024
1 parent c0cd6f3 commit 40e804a
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 6 deletions.
File renamed without changes.
File renamed without changes.
11 changes: 6 additions & 5 deletions examples/gridfs-upload.php → examples/gridfs_upload.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
use function fwrite;
use function getenv;
use function rewind;
use function stream_get_contents;

use const PHP_EOL;
use const STDOUT;

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

Expand All @@ -38,10 +38,11 @@
echo 'Inserted file with ID: ' . $id . PHP_EOL;
fclose($stream);

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

// Delete the file
$gridfs->delete($id);
22 changes: 21 additions & 1 deletion tests/ExamplesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,26 @@ public static function provideExamples(): Generator
'expectedOutput' => $expectedOutput,
];

$expectedOutput = <<<'OUTPUT'
Read a total of 17888890 bytes
OUTPUT;

yield 'gridfs_stream' => [
'file' => __DIR__ . '/../examples/gridfs_stream.php',
'expectedOutput' => $expectedOutput,
];

$expectedOutput = <<<'OUTPUT'
Inserted file with ID: %s
File contents: Hello world!

OUTPUT;

yield 'gridfs_upload' => [
'file' => __DIR__ . '/../examples/gridfs_upload.php',
'expectedOutput' => $expectedOutput,
];

$expectedOutput = <<<'OUTPUT'
File exists: no
Writing file
Expand Down Expand Up @@ -243,7 +263,7 @@ public function testAtlasSearch(): void

OUTPUT;

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

public function testChangeStream(): void
Expand Down

0 comments on commit 40e804a

Please sign in to comment.