Skip to content

Commit 9d1a9cb

Browse files
Will Banfieldjmikola
authored andcommitted
additional tests: WIP
1 parent 9b1f164 commit 9d1a9cb

File tree

6 files changed

+243
-6
lines changed

6 files changed

+243
-6
lines changed

src/GridFS/GridFsDownload.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,11 @@ public function __construct(
3737
{
3838
$this->collectionsWrapper = $collectionsWrapper;
3939
$this->file = $file;
40-
$cursor = $this->collectionsWrapper->getChunksCollection()->find(['files_id' => $this->file->_id], ['sort' => ['n' => 1]]);
40+
try{
41+
$cursor = $this->collectionsWrapper->getChunksCollection()->find(['files_id' => $this->file->_id], ['sort' => ['n' => 1]]);
42+
} catch(\MongoDB\Exception $e){
43+
throw new \MongoDB\Exception\GridFSCorruptFileException();
44+
}
4145
$this->chunksIterator = new \IteratorIterator($cursor);
4246
if ($this->file->length >= 0) {
4347
$this->numChunks = ceil($this->file->length / $this->file->chunkSize);
@@ -96,6 +100,10 @@ public function getId()
96100
{
97101
return $this->file->_id;
98102
}
103+
public function getFile()
104+
{
105+
return $this->file;
106+
}
99107
private function advanceChunks()
100108
{
101109
if($this->chunkOffset >= $this->numChunks) {

src/GridFS/GridFsUpload.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,10 @@ public function __construct(
5454
$this->ctx = hash_init('md5');
5555
$this->collectionsWrapper = $collectionsWrapper;
5656
$this->buffer = fopen('php://temp', 'w+');
57+
58+
$options +=['chunkSizeBytes' => 261120];
5759
$this->chunkSize = $options['chunkSizeBytes'];
60+
5861
$time = $this->millitime();
5962
$uploadDate = new \MongoDB\BSON\UTCDateTime($time);
6063
$objectId = new \MongoDB\BSON\ObjectId();
@@ -158,6 +161,18 @@ public function getId()
158161
{
159162
return $this->file["_id"];
160163
}
164+
public function getLength()
165+
{
166+
return $this->length;
167+
}
168+
public function getChunkSize()
169+
{
170+
return $this->chunkSize;
171+
}
172+
public function getFile()
173+
{
174+
return $this->file;
175+
}
161176
private function insertChunk($data)
162177
{
163178
$toUpload = ["files_id" => $this->file['_id'], "n" => $this->chunkOffset, "data" => new \MongoDB\BSON\Binary($data, \MongoDB\BSON\Binary::TYPE_GENERIC)];

tests/GridFS/BigInsertTest.txt

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

tests/GridFS/BucketFunctionalTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,12 @@ public function testGridInNonIntChunksize()
201201
['$set'=> ['chunkSize' => 100.00]]);
202202
$this->assertEquals("data", stream_get_contents($this->bucket->openDownloadStream($id)));
203203
}
204+
public function testBigInsert()
205+
{
206+
$testPath= __DIR__."/BigInsertTest.txt";
207+
$testStream = fopen($testPath, "r");
208+
$id = $this->bucket->uploadFromStream("BigInsertTest", $testStream);
209+
}
204210
private function generateStream($input)
205211
{
206212
$stream = fopen('php://temp', 'w+');

tests/GridFS/FunctionalTestCase.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
abstract class FunctionalTestCase extends BaseFunctionalTestCase
1313
{
1414
protected $bucket;
15-
protected $bucketReadWriter;
15+
protected $collectionsWrapper;
1616

1717
public function setUp()
1818
{
@@ -24,17 +24,17 @@ public function setUp()
2424
$streamWrapper = new \MongoDB\GridFS\StreamWrapper();
2525
$streamWrapper->register($this->manager);
2626
$this->bucket = new \MongoDB\GridFS\Bucket($this->manager, $this->getDatabaseName());
27-
$this->bucketReadWriter = new \MongoDB\GridFS\BucketReadWriter($this->bucket);
27+
$this->collectionsWrapper = $this->bucket->getCollectionsWrapper();
2828
}
2929

3030
public function tearDown()
3131
{
32-
if ($this->hasFailed()) {
33-
return;
34-
}
3532
foreach(['fs.files', 'fs.chunks'] as $collection){
3633
$col = new Collection($this->manager, sprintf("%s.%s",$this->getDatabaseName(), $collection));
3734
$col->drop();
3835
}
36+
if ($this->hasFailed()) {
37+
return;
38+
}
3939
}
4040
}

tests/GridFS/GridFsStreamTest.php

Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
<?php
2+
3+
namespace MongoDB\Tests\GridFS;
4+
5+
use MongoDB\GridFS;
6+
7+
/**
8+
* Functional tests for the Bucket class.
9+
*/
10+
class GridFsStreamTest extends FunctionalTestCase
11+
{
12+
13+
/* public function testConstructorOptionTypeChecks(array $options)
14+
{
15+
new \MongoDB\GridFS\Bucket($this->manager, $this->getDatabaseName(), $options);
16+
}
17+
18+
public function provideInvalidConstructorOptions()
19+
{
20+
$options = [];
21+
$invalidBucketNames = [123, 3.14, true, [], new \stdClass];
22+
$invalidChunkSizes = ['foo', 3.14, true, [], new \stdClass];
23+
24+
25+
foreach ($this->getInvalidReadPreferenceValues() as $value) {
26+
$options[][] = ['readPreference' => $value];
27+
}
28+
29+
foreach ($this->getInvalidWriteConcernValues() as $value) {
30+
$options[][] = ['writeConcern' => $value];
31+
}
32+
foreach ($invalidBucketNames as $value) {
33+
$options[][] = ['bucketName' => $value];
34+
}
35+
foreach ($invalidChunkSizes as $value) {
36+
$options[][] = ['chunkSizeBytes' => $value];
37+
}
38+
39+
return $options;
40+
}
41+
*/
42+
public function testBasic()
43+
{
44+
$upload = new \MongoDB\GridFS\GridFsUpload($this->collectionsWrapper, "test");
45+
$upload->insertChunks("hello world");
46+
$id = $upload->getId();
47+
$upload->close();
48+
49+
$this->assertEquals(1, $this->collectionsWrapper->getFilesCollection()->count());
50+
$this->assertEquals(1, $this->collectionsWrapper->getChunksCollection()->count());
51+
52+
$file = $this->collectionsWrapper->getFilesCollection()->findOne(["_id"=>$id]);
53+
54+
$download = new \MongoDB\GridFS\GridFsDownload($this->collectionsWrapper, $file);
55+
$stream = fopen('php://temp', 'w+');
56+
$download->downloadToStream($stream);
57+
rewind($stream);
58+
$contents = stream_get_contents($stream);
59+
$this->assertEquals("hello world", $contents);
60+
fclose($stream);
61+
62+
#make sure it's still there!
63+
$download = new \MongoDB\GridFS\GridFsDownload($this->collectionsWrapper, $file);
64+
$stream = fopen('php://temp', 'w+');
65+
$download->downloadToStream($stream);
66+
rewind($stream);
67+
$contents = stream_get_contents($stream);
68+
$this->assertEquals("hello world", $contents);
69+
fclose($stream);
70+
71+
$upload = new \MongoDB\GridFS\GridFsUpload($this->collectionsWrapper, "test");
72+
$id = $upload->getId();
73+
$upload->close();
74+
75+
$this->assertEquals(2, $this->collectionsWrapper->getFilesCollection()->count());
76+
$this->assertEquals(1, $this->collectionsWrapper->getChunksCollection()->count());
77+
78+
$file = $this->collectionsWrapper->getFilesCollection()->findOne(["_id"=>$id]);
79+
$download = new \MongoDB\GridFS\GridFsDownload($this->collectionsWrapper, $file);
80+
$stream = fopen('php://temp', 'w+');
81+
$download->downloadToStream($stream);
82+
rewind($stream);
83+
$contents = stream_get_contents($stream);
84+
85+
$this->assertEquals("", $contents);
86+
}
87+
88+
public function testMd5()
89+
{
90+
$upload = new \MongoDB\GridFS\GridFsUpload($this->collectionsWrapper, "test");
91+
$upload->insertChunks("hello world\n");
92+
$id = $upload->getId();
93+
$upload->close();
94+
95+
$file = $this->collectionsWrapper->getFilesCollection()->findOne(["_id"=>$id]);
96+
$this->assertEquals("6f5902ac237024bdd0c176cb93063dc4", $file->md5);
97+
}
98+
public function testUploadDefaultOpts()
99+
{
100+
$upload = new \MongoDB\GridFS\GridFsUpload($this->collectionsWrapper, "test");
101+
102+
$this->assertTrue($upload->getId() instanceof \MongoDB\BSON\ObjectId);
103+
$this->assertTrue($upload->getFile()["uploadDate"] instanceof \MongoDB\BSON\UTCDateTime);
104+
105+
$this->assertEquals($upload->getFile()["filename"], "test");
106+
$this->assertEquals($upload->getLength(),0);
107+
108+
$this->assertTrue(!isset($upload->getFile()["contentType"]));
109+
$this->assertTrue(!isset($upload->getFile()["aliases"]));
110+
$this->assertTrue(!isset($upload->getFile()["metadata"]));
111+
112+
$this->assertEquals(255 * 1024, $upload->getChunkSize());
113+
}
114+
public function testUploadCustomOpts()
115+
{
116+
$options = ["chunkSizeBytes" => 1,
117+
"contentType" => "text/html",
118+
"aliases" => ["foo", "bar"],
119+
"metadata" => ["foo" => 1, "bar" => 2]
120+
];
121+
$upload = new \MongoDB\GridFS\GridFsUpload($this->collectionsWrapper, "test", $options);
122+
$this->assertEquals($upload->getChunkSize(), 1);
123+
$this->assertEquals($upload->getFile()["contentType"], "text/html");
124+
$this->assertEquals($upload->getFile()["aliases"], ["foo", "bar"]);
125+
$this->assertEquals($upload->getFile()["metadata"], ["foo" => 1, "bar" => 2]);
126+
}
127+
public function testDownloadDefaultOpts()
128+
{
129+
$upload = new \MongoDB\GridFS\GridFsUpload($this->collectionsWrapper, "test");
130+
$upload->close();
131+
132+
$file = $this->collectionsWrapper->getFilesCollection()->findOne(["_id" => $upload->getId()]);
133+
$download = new \MongoDB\GridFS\GridFsDownload($this->collectionsWrapper, $file);
134+
$download->close();
135+
136+
$this->assertEquals($upload->getId(), $download->getId());
137+
$this->assertEquals(0, $download->getFile()->length);
138+
$this->assertTrue(!isset($download->getFile()->contentType));
139+
$this->assertTrue(!isset($download->getFile()->aliases));
140+
$this->assertTrue(!isset($download->getFile()->metadata));
141+
$this->assertTrue($download->getFile()->uploadDate instanceof \MongoDB\BSON\UTCDateTime);
142+
$this->assertEquals(255 * 1024, $download->getFile()->chunkSize);
143+
$this->assertEquals("d41d8cd98f00b204e9800998ecf8427e", $download->getFile()->md5);
144+
}
145+
public function testDownloadCustomOpts()
146+
{
147+
$options = ["chunkSizeBytes" => 1000,
148+
"contentType" => "text/html",
149+
"aliases" => ["foo", "bar"],
150+
"metadata" => ["foo" => 1, "bar" => 2]
151+
];
152+
$upload = new \MongoDB\GridFS\GridFsUpload($this->collectionsWrapper, "test", $options);
153+
$upload->insertChunks("hello world");
154+
$upload->close();
155+
156+
$file = $this->collectionsWrapper->getFilesCollection()->findOne(["_id" => $upload->getId()]);
157+
$download = new \MongoDB\GridFS\GridFsDownload($this->collectionsWrapper, $file);
158+
159+
$this->assertEquals("test", $download->getFile()->filename);
160+
$this->assertEquals($upload->getId(), $download->getId());
161+
$this->assertEquals(11, $download->getFile()->length);
162+
$this->assertEquals("text/html", $download->getFile()->contentType);
163+
$this->assertEquals(1000, $download->getFile()->chunkSize);
164+
$this->assertEquals(["foo", "bar"], $download->getFile()->aliases);
165+
$this->assertEquals(["foo"=> 1, "bar"=> 2], (array) $download->getFile()->metadata);
166+
$this->assertEquals("5eb63bbbe01eeed093cb22bb8f5acdc3", $download->getFile()->md5);
167+
}
168+
/**
169+
*@dataProvider provideInsertChunks
170+
*/
171+
public function testInsertChunks($data)
172+
{
173+
$upload = new \MongoDB\GridFS\GridFsUpload($this->collectionsWrapper, "test");
174+
$upload->insertChunks($data);
175+
$upload->close();
176+
$stream = $this->bucket->openDownloadStream($upload->getId());
177+
$this->assertEquals($data, stream_get_contents($stream));
178+
}
179+
180+
public function provideInsertChunks()
181+
{
182+
$dataVals = [];
183+
$testArgs[][] = "hello world";
184+
$testArgs[][] = "1234567890";
185+
$testArgs[][] = "~!@#$%^&*()_+";
186+
for($j=0; $j<10; $j++){
187+
$randomTest = "";
188+
for($i=0; $i<100; $i++){
189+
$randomTest .= chr(rand(0, 256));
190+
}
191+
$testArgs[][] = $randomTest;
192+
}
193+
$utf8="";
194+
for($i=0; $i<256; $i++){
195+
$utf8 .= chr($i);
196+
}
197+
$testArgs[][]=$utf8;
198+
return $testArgs;
199+
}
200+
private function generateStream($input)
201+
{
202+
$stream = fopen('php://temp', 'w+');
203+
fwrite($stream, $input);
204+
rewind($stream);
205+
return $stream;
206+
}
207+
}

0 commit comments

Comments
 (0)