Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
M
mongo-php-library
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
sinan
mongo-php-library
Commits
9d1a9cba
Commit
9d1a9cba
authored
Jan 05, 2016
by
Will Banfield
Committed by
Jeremy Mikola
Mar 14, 2016
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
additional tests: WIP
parent
9b1f1646
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
243 additions
and
6 deletions
+243
-6
GridFsDownload.php
src/GridFS/GridFsDownload.php
+9
-1
GridFsUpload.php
src/GridFS/GridFsUpload.php
+15
-0
BigInsertTest.txt
tests/GridFS/BigInsertTest.txt
+1
-0
BucketFunctionalTest.php
tests/GridFS/BucketFunctionalTest.php
+6
-0
FunctionalTestCase.php
tests/GridFS/FunctionalTestCase.php
+5
-5
GridFsStreamTest.php
tests/GridFS/GridFsStreamTest.php
+207
-0
No files found.
src/GridFS/GridFsDownload.php
View file @
9d1a9cba
...
...
@@ -37,7 +37,11 @@ class GridFsDownload
{
$this
->
collectionsWrapper
=
$collectionsWrapper
;
$this
->
file
=
$file
;
$cursor
=
$this
->
collectionsWrapper
->
getChunksCollection
()
->
find
([
'files_id'
=>
$this
->
file
->
_id
],
[
'sort'
=>
[
'n'
=>
1
]]);
try
{
$cursor
=
$this
->
collectionsWrapper
->
getChunksCollection
()
->
find
([
'files_id'
=>
$this
->
file
->
_id
],
[
'sort'
=>
[
'n'
=>
1
]]);
}
catch
(
\MongoDB\Exception
$e
){
throw
new
\MongoDB\Exception\GridFSCorruptFileException
();
}
$this
->
chunksIterator
=
new
\IteratorIterator
(
$cursor
);
if
(
$this
->
file
->
length
>=
0
)
{
$this
->
numChunks
=
ceil
(
$this
->
file
->
length
/
$this
->
file
->
chunkSize
);
...
...
@@ -96,6 +100,10 @@ class GridFsDownload
{
return
$this
->
file
->
_id
;
}
public
function
getFile
()
{
return
$this
->
file
;
}
private
function
advanceChunks
()
{
if
(
$this
->
chunkOffset
>=
$this
->
numChunks
)
{
...
...
src/GridFS/GridFsUpload.php
View file @
9d1a9cba
...
...
@@ -54,7 +54,10 @@ class GridFsUpload
$this
->
ctx
=
hash_init
(
'md5'
);
$this
->
collectionsWrapper
=
$collectionsWrapper
;
$this
->
buffer
=
fopen
(
'php://temp'
,
'w+'
);
$options
+=
[
'chunkSizeBytes'
=>
261120
];
$this
->
chunkSize
=
$options
[
'chunkSizeBytes'
];
$time
=
$this
->
millitime
();
$uploadDate
=
new
\MongoDB\BSON\UTCDateTime
(
$time
);
$objectId
=
new
\MongoDB\BSON\ObjectId
();
...
...
@@ -158,6 +161,18 @@ class GridFsUpload
{
return
$this
->
file
[
"_id"
];
}
public
function
getLength
()
{
return
$this
->
length
;
}
public
function
getChunkSize
()
{
return
$this
->
chunkSize
;
}
public
function
getFile
()
{
return
$this
->
file
;
}
private
function
insertChunk
(
$data
)
{
$toUpload
=
[
"files_id"
=>
$this
->
file
[
'_id'
],
"n"
=>
$this
->
chunkOffset
,
"data"
=>
new
\MongoDB\BSON\Binary
(
$data
,
\MongoDB\BSON\Binary
::
TYPE_GENERIC
)];
...
...
tests/GridFS/BigInsertTest.txt
0 → 100644
View file @
9d1a9cba
This diff is collapsed.
Click to expand it.
tests/GridFS/BucketFunctionalTest.php
View file @
9d1a9cba
...
...
@@ -201,6 +201,12 @@ class BucketFunctionalTest extends FunctionalTestCase
[
'$set'
=>
[
'chunkSize'
=>
100.00
]]);
$this
->
assertEquals
(
"data"
,
stream_get_contents
(
$this
->
bucket
->
openDownloadStream
(
$id
)));
}
public
function
testBigInsert
()
{
$testPath
=
__DIR__
.
"/BigInsertTest.txt"
;
$testStream
=
fopen
(
$testPath
,
"r"
);
$id
=
$this
->
bucket
->
uploadFromStream
(
"BigInsertTest"
,
$testStream
);
}
private
function
generateStream
(
$input
)
{
$stream
=
fopen
(
'php://temp'
,
'w+'
);
...
...
tests/GridFS/FunctionalTestCase.php
View file @
9d1a9cba
...
...
@@ -12,7 +12,7 @@ use MongoDB\Tests\FunctionalTestCase as BaseFunctionalTestCase;
abstract
class
FunctionalTestCase
extends
BaseFunctionalTestCase
{
protected
$bucket
;
protected
$
bucketReadWrit
er
;
protected
$
collectionsWrapp
er
;
public
function
setUp
()
{
...
...
@@ -24,17 +24,17 @@ abstract class FunctionalTestCase extends BaseFunctionalTestCase
$streamWrapper
=
new
\MongoDB\GridFS\StreamWrapper
();
$streamWrapper
->
register
(
$this
->
manager
);
$this
->
bucket
=
new
\MongoDB\GridFS\Bucket
(
$this
->
manager
,
$this
->
getDatabaseName
());
$this
->
bucketReadWriter
=
new
\MongoDB\GridFS\BucketReadWriter
(
$this
->
bucket
);
$this
->
collectionsWrapper
=
$this
->
bucket
->
getCollectionsWrapper
(
);
}
public
function
tearDown
()
{
if
(
$this
->
hasFailed
())
{
return
;
}
foreach
([
'fs.files'
,
'fs.chunks'
]
as
$collection
){
$col
=
new
Collection
(
$this
->
manager
,
sprintf
(
"%s.%s"
,
$this
->
getDatabaseName
(),
$collection
));
$col
->
drop
();
}
if
(
$this
->
hasFailed
())
{
return
;
}
}
}
tests/GridFS/GridFsStreamTest.php
0 → 100644
View file @
9d1a9cba
<?php
namespace
MongoDB\Tests\GridFS
;
use
MongoDB\GridFS
;
/**
* Functional tests for the Bucket class.
*/
class
GridFsStreamTest
extends
FunctionalTestCase
{
/* public function testConstructorOptionTypeChecks(array $options)
{
new \MongoDB\GridFS\Bucket($this->manager, $this->getDatabaseName(), $options);
}
public function provideInvalidConstructorOptions()
{
$options = [];
$invalidBucketNames = [123, 3.14, true, [], new \stdClass];
$invalidChunkSizes = ['foo', 3.14, true, [], new \stdClass];
foreach ($this->getInvalidReadPreferenceValues() as $value) {
$options[][] = ['readPreference' => $value];
}
foreach ($this->getInvalidWriteConcernValues() as $value) {
$options[][] = ['writeConcern' => $value];
}
foreach ($invalidBucketNames as $value) {
$options[][] = ['bucketName' => $value];
}
foreach ($invalidChunkSizes as $value) {
$options[][] = ['chunkSizeBytes' => $value];
}
return $options;
}
*/
public
function
testBasic
()
{
$upload
=
new
\MongoDB\GridFS\GridFsUpload
(
$this
->
collectionsWrapper
,
"test"
);
$upload
->
insertChunks
(
"hello world"
);
$id
=
$upload
->
getId
();
$upload
->
close
();
$this
->
assertEquals
(
1
,
$this
->
collectionsWrapper
->
getFilesCollection
()
->
count
());
$this
->
assertEquals
(
1
,
$this
->
collectionsWrapper
->
getChunksCollection
()
->
count
());
$file
=
$this
->
collectionsWrapper
->
getFilesCollection
()
->
findOne
([
"_id"
=>
$id
]);
$download
=
new
\MongoDB\GridFS\GridFsDownload
(
$this
->
collectionsWrapper
,
$file
);
$stream
=
fopen
(
'php://temp'
,
'w+'
);
$download
->
downloadToStream
(
$stream
);
rewind
(
$stream
);
$contents
=
stream_get_contents
(
$stream
);
$this
->
assertEquals
(
"hello world"
,
$contents
);
fclose
(
$stream
);
#make sure it's still there!
$download
=
new
\MongoDB\GridFS\GridFsDownload
(
$this
->
collectionsWrapper
,
$file
);
$stream
=
fopen
(
'php://temp'
,
'w+'
);
$download
->
downloadToStream
(
$stream
);
rewind
(
$stream
);
$contents
=
stream_get_contents
(
$stream
);
$this
->
assertEquals
(
"hello world"
,
$contents
);
fclose
(
$stream
);
$upload
=
new
\MongoDB\GridFS\GridFsUpload
(
$this
->
collectionsWrapper
,
"test"
);
$id
=
$upload
->
getId
();
$upload
->
close
();
$this
->
assertEquals
(
2
,
$this
->
collectionsWrapper
->
getFilesCollection
()
->
count
());
$this
->
assertEquals
(
1
,
$this
->
collectionsWrapper
->
getChunksCollection
()
->
count
());
$file
=
$this
->
collectionsWrapper
->
getFilesCollection
()
->
findOne
([
"_id"
=>
$id
]);
$download
=
new
\MongoDB\GridFS\GridFsDownload
(
$this
->
collectionsWrapper
,
$file
);
$stream
=
fopen
(
'php://temp'
,
'w+'
);
$download
->
downloadToStream
(
$stream
);
rewind
(
$stream
);
$contents
=
stream_get_contents
(
$stream
);
$this
->
assertEquals
(
""
,
$contents
);
}
public
function
testMd5
()
{
$upload
=
new
\MongoDB\GridFS\GridFsUpload
(
$this
->
collectionsWrapper
,
"test"
);
$upload
->
insertChunks
(
"hello world
\n
"
);
$id
=
$upload
->
getId
();
$upload
->
close
();
$file
=
$this
->
collectionsWrapper
->
getFilesCollection
()
->
findOne
([
"_id"
=>
$id
]);
$this
->
assertEquals
(
"6f5902ac237024bdd0c176cb93063dc4"
,
$file
->
md5
);
}
public
function
testUploadDefaultOpts
()
{
$upload
=
new
\MongoDB\GridFS\GridFsUpload
(
$this
->
collectionsWrapper
,
"test"
);
$this
->
assertTrue
(
$upload
->
getId
()
instanceof
\MongoDB\BSON\ObjectId
);
$this
->
assertTrue
(
$upload
->
getFile
()[
"uploadDate"
]
instanceof
\MongoDB\BSON\UTCDateTime
);
$this
->
assertEquals
(
$upload
->
getFile
()[
"filename"
],
"test"
);
$this
->
assertEquals
(
$upload
->
getLength
(),
0
);
$this
->
assertTrue
(
!
isset
(
$upload
->
getFile
()[
"contentType"
]));
$this
->
assertTrue
(
!
isset
(
$upload
->
getFile
()[
"aliases"
]));
$this
->
assertTrue
(
!
isset
(
$upload
->
getFile
()[
"metadata"
]));
$this
->
assertEquals
(
255
*
1024
,
$upload
->
getChunkSize
());
}
public
function
testUploadCustomOpts
()
{
$options
=
[
"chunkSizeBytes"
=>
1
,
"contentType"
=>
"text/html"
,
"aliases"
=>
[
"foo"
,
"bar"
],
"metadata"
=>
[
"foo"
=>
1
,
"bar"
=>
2
]
];
$upload
=
new
\MongoDB\GridFS\GridFsUpload
(
$this
->
collectionsWrapper
,
"test"
,
$options
);
$this
->
assertEquals
(
$upload
->
getChunkSize
(),
1
);
$this
->
assertEquals
(
$upload
->
getFile
()[
"contentType"
],
"text/html"
);
$this
->
assertEquals
(
$upload
->
getFile
()[
"aliases"
],
[
"foo"
,
"bar"
]);
$this
->
assertEquals
(
$upload
->
getFile
()[
"metadata"
],
[
"foo"
=>
1
,
"bar"
=>
2
]);
}
public
function
testDownloadDefaultOpts
()
{
$upload
=
new
\MongoDB\GridFS\GridFsUpload
(
$this
->
collectionsWrapper
,
"test"
);
$upload
->
close
();
$file
=
$this
->
collectionsWrapper
->
getFilesCollection
()
->
findOne
([
"_id"
=>
$upload
->
getId
()]);
$download
=
new
\MongoDB\GridFS\GridFsDownload
(
$this
->
collectionsWrapper
,
$file
);
$download
->
close
();
$this
->
assertEquals
(
$upload
->
getId
(),
$download
->
getId
());
$this
->
assertEquals
(
0
,
$download
->
getFile
()
->
length
);
$this
->
assertTrue
(
!
isset
(
$download
->
getFile
()
->
contentType
));
$this
->
assertTrue
(
!
isset
(
$download
->
getFile
()
->
aliases
));
$this
->
assertTrue
(
!
isset
(
$download
->
getFile
()
->
metadata
));
$this
->
assertTrue
(
$download
->
getFile
()
->
uploadDate
instanceof
\MongoDB\BSON\UTCDateTime
);
$this
->
assertEquals
(
255
*
1024
,
$download
->
getFile
()
->
chunkSize
);
$this
->
assertEquals
(
"d41d8cd98f00b204e9800998ecf8427e"
,
$download
->
getFile
()
->
md5
);
}
public
function
testDownloadCustomOpts
()
{
$options
=
[
"chunkSizeBytes"
=>
1000
,
"contentType"
=>
"text/html"
,
"aliases"
=>
[
"foo"
,
"bar"
],
"metadata"
=>
[
"foo"
=>
1
,
"bar"
=>
2
]
];
$upload
=
new
\MongoDB\GridFS\GridFsUpload
(
$this
->
collectionsWrapper
,
"test"
,
$options
);
$upload
->
insertChunks
(
"hello world"
);
$upload
->
close
();
$file
=
$this
->
collectionsWrapper
->
getFilesCollection
()
->
findOne
([
"_id"
=>
$upload
->
getId
()]);
$download
=
new
\MongoDB\GridFS\GridFsDownload
(
$this
->
collectionsWrapper
,
$file
);
$this
->
assertEquals
(
"test"
,
$download
->
getFile
()
->
filename
);
$this
->
assertEquals
(
$upload
->
getId
(),
$download
->
getId
());
$this
->
assertEquals
(
11
,
$download
->
getFile
()
->
length
);
$this
->
assertEquals
(
"text/html"
,
$download
->
getFile
()
->
contentType
);
$this
->
assertEquals
(
1000
,
$download
->
getFile
()
->
chunkSize
);
$this
->
assertEquals
([
"foo"
,
"bar"
],
$download
->
getFile
()
->
aliases
);
$this
->
assertEquals
([
"foo"
=>
1
,
"bar"
=>
2
],
(
array
)
$download
->
getFile
()
->
metadata
);
$this
->
assertEquals
(
"5eb63bbbe01eeed093cb22bb8f5acdc3"
,
$download
->
getFile
()
->
md5
);
}
/**
*@dataProvider provideInsertChunks
*/
public
function
testInsertChunks
(
$data
)
{
$upload
=
new
\MongoDB\GridFS\GridFsUpload
(
$this
->
collectionsWrapper
,
"test"
);
$upload
->
insertChunks
(
$data
);
$upload
->
close
();
$stream
=
$this
->
bucket
->
openDownloadStream
(
$upload
->
getId
());
$this
->
assertEquals
(
$data
,
stream_get_contents
(
$stream
));
}
public
function
provideInsertChunks
()
{
$dataVals
=
[];
$testArgs
[][]
=
"hello world"
;
$testArgs
[][]
=
"1234567890"
;
$testArgs
[][]
=
"~!@#$%^&*()_+"
;
for
(
$j
=
0
;
$j
<
10
;
$j
++
){
$randomTest
=
""
;
for
(
$i
=
0
;
$i
<
100
;
$i
++
){
$randomTest
.=
chr
(
rand
(
0
,
256
));
}
$testArgs
[][]
=
$randomTest
;
}
$utf8
=
""
;
for
(
$i
=
0
;
$i
<
256
;
$i
++
){
$utf8
.=
chr
(
$i
);
}
$testArgs
[][]
=
$utf8
;
return
$testArgs
;
}
private
function
generateStream
(
$input
)
{
$stream
=
fopen
(
'php://temp'
,
'w+'
);
fwrite
(
$stream
,
$input
);
rewind
(
$stream
);
return
$stream
;
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment