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
f2666e19
Commit
f2666e19
authored
Jan 11, 2016
by
Jeremy Mikola
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Apply CS fixes and refactor GridFS classes
parent
0b31800c
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
824 additions
and
303 deletions
+824
-303
GridFSFileNotFoundException.php
src/Exception/GridFSFileNotFoundException.php
+4
-3
Bucket.php
src/GridFS/Bucket.php
+267
-133
GridFSCollectionsWrapper.php
src/GridFS/GridFSCollectionsWrapper.php
+45
-72
GridFSDownload.php
src/GridFS/GridFSDownload.php
+163
-0
GridFSUpload.php
src/GridFS/GridFSUpload.php
+254
-0
StreamWrapper.php
src/GridFS/StreamWrapper.php
+62
-48
BucketFunctionalTest.php
tests/GridFS/BucketFunctionalTest.php
+8
-8
GridFSStreamTest.php
tests/GridFS/GridFSStreamTest.php
+21
-39
No files found.
src/Exception/GridFSFileNotFoundException.php
View file @
f2666e19
...
...
@@ -4,7 +4,8 @@ namespace MongoDB\Exception;
class
GridFSFileNotFoundException
extends
\MongoDB\Driver\Exception\RuntimeException
implements
Exception
{
public
function
__construct
(
$fname
,
$nameSpace
){
parent
::
__construct
(
sprintf
(
'Unable to find file by: %s in %s'
,
$fname
,
$nameSpace
));
public
function
__construct
(
$filename
,
$namespace
)
{
parent
::
__construct
(
sprintf
(
'Unable to find file "%s" in namespace "%s"'
,
$filename
,
$namespace
));
}
}
src/GridFS/Bucket.php
View file @
f2666e19
This diff is collapsed.
Click to expand it.
src/GridFS/GridFSCollectionsWrapper.php
View file @
f2666e19
<?php
namespace
MongoDB\GridFS
;
use
MongoDB\Collection
;
use
MongoDB\Driver\Manager
;
use
MongoDB\Driver\ReadPreference
;
use
MongoDB\Driver\WriteConcern
;
use
MongoDB\Driver\Manager
;
use
MongoDB\Exception\InvalidArgumentTypeException
;
/**
* @internal
* GridFSCollectionsWrapper abstracts the GridFS files and chunks collections.
*
* @internal
*/
class
GridFSCollectionsWrapper
{
private
$filesCollection
;
private
$chunksCollection
;
private
$ensuredIndexes
=
false
;
private
$filesCollection
;
/**
* Constructs a GridFS bucket.
*
* Supported options:
*
* * bucketName (string): The bucket name, which will be used as a prefix
* for the files and chunks collections. Defaults to "fs".
*
* * chunkSizeBytes (integer): The chunk size in bytes. Defaults to
* 261120 (i.e. 255 KiB).
*
* * readPreference (MongoDB\Driver\ReadPreference): Read preference.
*
* * writeConcern (MongoDB\Driver\WriteConcern): Write concern.
* Constructs a GridFS collection wrapper.
*
* @see Collection::__construct() for supported options
* @param Manager $manager Manager instance from the driver
* @param string $databaseName Database name
* @param array $options Bucket options
* @param string $bucketName Bucket name
* @param array $collectionOptions Collection options
* @throws InvalidArgumentException
*/
public
function
__construct
(
Manager
$manager
,
$databaseName
,
$
options
)
public
function
__construct
(
Manager
$manager
,
$databaseName
,
$
bucketName
,
array
$collectionOptions
=
[]
)
{
$collectionOptions
=
[];
if
(
isset
(
$options
[
'bucketName'
])
&&
!
is_string
(
$options
[
'bucketName'
]))
{
throw
new
InvalidArgumentTypeException
(
'"bucketName" option'
,
$options
[
'bucketName'
],
'string'
);
}
if
(
isset
(
$options
[
'chunkSizeBytes'
])
&&
!
is_integer
(
$options
[
'chunkSizeBytes'
]))
{
throw
new
InvalidArgumentTypeException
(
'"chunkSizeBytes" option'
,
$options
[
'chunkSizeBytes'
],
'integer'
);
}
if
(
isset
(
$options
[
'readPreference'
]))
{
if
(
!
$options
[
'readPreference'
]
instanceof
ReadPreference
)
{
throw
new
InvalidArgumentTypeException
(
'"readPreference" option'
,
$options
[
'readPreference'
],
'MongoDB\Driver\ReadPreference'
);
}
else
{
$collectionOptions
[
'readPreference'
]
=
$options
[
'readPreference'
];
}
}
if
(
isset
(
$options
[
'writeConcern'
]))
{
if
(
!
$options
[
'writeConcern'
]
instanceof
WriteConcern
)
{
throw
new
InvalidArgumentTypeException
(
'"writeConcern" option'
,
$options
[
'writeConcern'
],
'MongoDB\Driver\WriteConcern'
);
}
else
{
$collectionOptions
[
'writeConcern'
]
=
$options
[
'writeConcern'
];
}
}
$this
->
filesCollection
=
new
Collection
(
$manager
,
sprintf
(
'%s.%s.files'
,
$databaseName
,
$options
[
'bucketName'
]),
$collectionOptions
);
$this
->
chunksCollection
=
new
Collection
(
$manager
,
sprintf
(
'%s.%s.chunks'
,
$databaseName
,
$options
[
'bucketName'
]),
$collectionOptions
);
$this
->
filesCollection
=
new
Collection
(
$manager
,
sprintf
(
'%s.%s.files'
,
$databaseName
,
$bucketName
),
$collectionOptions
);
$this
->
chunksCollection
=
new
Collection
(
$manager
,
sprintf
(
'%s.%s.chunks'
,
$databaseName
,
$bucketName
),
$collectionOptions
);
}
public
function
chunkInsert
(
$toUpload
)
{
$this
->
ensureIndexes
();
$this
->
chunksCollection
->
insertOne
(
$toUpload
);
}
public
function
fileInsert
(
$toUpload
)
{
$this
->
ensureIndexes
();
$this
->
filesCollection
->
insertOne
(
$toUpload
);
}
public
function
getChunksCollection
()
{
return
$this
->
chunksCollection
;
}
public
function
getFilesCollection
()
{
return
$this
->
filesCollection
;
}
private
function
ensureIndexes
()
public
function
insertChunk
(
$chunk
)
{
if
(
$this
->
ensuredIndexes
)
{
return
;
}
if
(
!
$this
->
isFilesCollectionEmpty
())
{
return
;
$this
->
ensureIndexes
();
$this
->
chunksCollection
->
insertOne
(
$chunk
);
}
$this
->
ensureFilesIndex
();
$this
->
ensureChunksIndex
();
$this
->
ensuredIndexes
=
true
;
public
function
insertFile
(
$file
)
{
$this
->
ensureIndexes
();
$this
->
filesCollection
->
insertOne
(
$file
);
}
private
function
ensureChunksIndex
()
{
foreach
(
$this
->
chunksCollection
->
listIndexes
()
as
$index
)
{
...
...
@@ -110,8 +64,10 @@ class GridFSCollectionsWrapper
return
;
}
}
$this
->
chunksCollection
->
createIndex
([
'files_id'
=>
1
,
'n'
=>
1
],
[
'unique'
=>
true
]);
}
private
function
ensureFilesIndex
()
{
foreach
(
$this
->
filesCollection
->
listIndexes
()
as
$index
)
{
...
...
@@ -119,8 +75,25 @@ class GridFSCollectionsWrapper
return
;
}
}
$this
->
filesCollection
->
createIndex
([
'filename'
=>
1
,
'uploadDate'
=>
1
]);
}
private
function
ensureIndexes
()
{
if
(
$this
->
ensuredIndexes
)
{
return
;
}
if
(
!
$this
->
isFilesCollectionEmpty
())
{
return
;
}
$this
->
ensureFilesIndex
();
$this
->
ensureChunksIndex
();
$this
->
ensuredIndexes
=
true
;
}
private
function
isFilesCollectionEmpty
()
{
return
null
===
$this
->
filesCollection
->
findOne
([],
[
...
...
src/GridFS/GridF
s
Download.php
→
src/GridFS/GridF
S
Download.php
View file @
f2666e19
<?php
namespace
MongoDB\GridFS
;
use
MongoDB\
Collec
tion
;
use
\
MongoDB\Exception\GridFSCorruptFileException
;
use
\MongoDB\Exception\InvalidArgumentTypeException
;
use
MongoDB\
Driver\Exception\Excep
tion
;
use
MongoDB\Exception\GridFSCorruptFileException
;
use
stdClass
;
/**
* GridFSDownload abstracts the process of reading a GridFS file.
*
* @internal
* GridFSDownload abstracts the processes of downloading from a GridFSBucket
*/
class
GridF
s
Download
class
GridF
S
Download
{
private
$buffer
;
private
$bufferEmpty
=
true
;
private
$bufferFresh
=
true
;
private
$bytesSeen
=
0
;
private
$chunkOffset
=
0
;
private
$chunksIterator
;
private
$bytesSeen
=
0
;
private
$numChunks
;
private
$iteratorEmpty
=
false
;
private
$firstCheck
=
true
;
private
$bufferFresh
=
true
;
private
$bufferEmpty
=
true
;
private
$collectionsWrapper
;
private
$chunkOffset
=
0
;
private
$buffer
;
private
$file
;
private
$firstCheck
=
true
;
private
$iteratorEmpty
=
false
;
private
$numChunks
;
/**
* Constructs a GridFS download stream
* Constructs a GridFS download stream
.
*
*
* @param GridFSCollectionsWrapper $collectionsWrapper File options
* @param \stdClass $file GridFS file to use
* @throws GridFSCorruptFileException, InvalidArgumentTypeException
* @param GridFSCollectionsWrapper $collectionsWrapper GridFS collections wrapper
* @param stdClass $file GridFS file document
* @throws GridFSCorruptFileException
*/
public
function
__construct
(
GridFSCollectionsWrapper
$collectionsWrapper
,
$file
)
public
function
__construct
(
GridFSCollectionsWrapper
$collectionsWrapper
,
stdClass
$file
)
{
if
(
!
(
$file
instanceof
\stdClass
)){
throw
new
\MongoDB\Exception\InvalidArgumentTypeException
(
'"file"'
,
$file
,
'stdClass'
);
}
$this
->
collectionsWrapper
=
$collectionsWrapper
;
$this
->
file
=
$file
;
try
{
$cursor
=
$this
->
collectionsWrapper
->
getChunksCollection
()
->
find
([
'files_id'
=>
$this
->
file
->
_id
],
[
'sort'
=>
[
'n'
=>
1
]]);
}
catch
(
\MongoDB\Exception
$e
){
throw
new
\MongoDB\Exception\GridFSCorruptFileException
();
try
{
$cursor
=
$this
->
collectionsWrapper
->
getChunksCollection
()
->
find
(
[
'files_id'
=>
$this
->
file
->
_id
],
[
'sort'
=>
[
'n'
=>
1
]]
);
}
catch
(
Exception
$e
)
{
// TODO: Why do we replace a driver exception with GridFSCorruptFileException here?
throw
new
GridFSCorruptFileException
();
}
$this
->
chunksIterator
=
new
\IteratorIterator
(
$cursor
);
if
(
$this
->
file
->
length
>=
0
)
{
$this
->
numChunks
=
ceil
(
$this
->
file
->
length
/
$this
->
file
->
chunkSize
);
}
else
{
$this
->
numChunks
=
0
;
}
$this
->
numChunks
=
(
$file
->
length
>=
0
)
?
ceil
(
$file
->
length
/
$file
->
chunkSize
)
:
0
;
$this
->
buffer
=
fopen
(
'php://temp'
,
'w+'
);
}
public
function
downloadToStream
(
$destination
)
public
function
close
()
{
while
(
$this
->
advanceChunks
())
{
fwrite
(
$destination
,
$this
->
chunksIterator
->
current
()
->
data
->
getData
());
}
fclose
(
$this
->
buffer
);
}
public
function
downloadNumBytes
(
$numToRead
)
{
public
function
downloadNumBytes
(
$numToRead
)
{
$output
=
""
;
if
(
$this
->
bufferFresh
)
{
rewind
(
$this
->
buffer
);
$this
->
bufferFresh
=
false
;
$this
->
bufferFresh
=
false
;
}
// TODO: Should we be checking for fread errors here?
$output
=
fread
(
$this
->
buffer
,
$numToRead
);
if
(
strlen
(
$output
)
==
$numToRead
)
{
return
$output
;
}
fclose
(
$this
->
buffer
);
$this
->
buffer
=
fopen
(
"php://temp"
,
"w+"
);
$this
->
bufferFresh
=
true
;
$this
->
bufferEmpty
=
true
;
$this
->
bufferFresh
=
true
;
$this
->
bufferEmpty
=
true
;
$bytesLeft
=
$numToRead
-
strlen
(
$output
);
while
(
strlen
(
$output
)
<
$numToRead
&&
$this
->
advanceChunks
())
{
while
(
strlen
(
$output
)
<
$numToRead
&&
$this
->
advanceChunks
())
{
$bytesLeft
=
$numToRead
-
strlen
(
$output
);
$output
.=
substr
(
$this
->
chunksIterator
->
current
()
->
data
->
getData
(),
0
,
$bytesLeft
);
}
if
(
!
$this
->
iteratorEmpty
&&
$this
->
file
->
length
>
0
&&
$bytesLeft
<
strlen
(
$this
->
chunksIterator
->
current
()
->
data
->
getData
()))
{
if
(
!
$this
->
iteratorEmpty
&&
$this
->
file
->
length
>
0
&&
$bytesLeft
<
strlen
(
$this
->
chunksIterator
->
current
()
->
data
->
getData
()))
{
fwrite
(
$this
->
buffer
,
substr
(
$this
->
chunksIterator
->
current
()
->
data
->
getData
(),
$bytesLeft
));
$this
->
bufferEmpty
=
false
;
$this
->
bufferEmpty
=
false
;
}
return
$output
;
}
public
function
getSize
()
public
function
downloadToStream
(
$destination
)
{
return
$this
->
file
->
length
;
while
(
$this
->
advanceChunks
())
{
// TODO: Should we be checking for fwrite errors here?
fwrite
(
$destination
,
$this
->
chunksIterator
->
current
()
->
data
->
getData
());
}
}
public
function
getFile
()
{
return
$this
->
file
;
}
public
function
getId
()
{
return
$this
->
file
->
_id
;
}
public
function
getFile
()
public
function
getSize
()
{
return
$this
->
file
;
return
$this
->
file
->
length
;
}
public
function
isEOF
()
{
return
(
$this
->
iteratorEmpty
&&
$this
->
bufferEmpty
);
}
private
function
advanceChunks
()
{
if
(
$this
->
chunkOffset
>=
$this
->
numChunks
)
{
$this
->
iteratorEmpty
=
true
;
if
(
$this
->
chunkOffset
>=
$this
->
numChunks
)
{
$this
->
iteratorEmpty
=
true
;
return
false
;
}
if
(
$this
->
firstCheck
)
{
if
(
$this
->
firstCheck
)
{
$this
->
chunksIterator
->
rewind
();
$this
->
firstCheck
=
false
;
$this
->
firstCheck
=
false
;
}
else
{
$this
->
chunksIterator
->
next
();
}
if
(
!
$this
->
chunksIterator
->
valid
())
{
throw
new
\MongoDB\Exception\GridFSCorruptFileException
();
if
(
!
$this
->
chunksIterator
->
valid
())
{
throw
new
GridFSCorruptFileException
();
}
if
(
$this
->
chunksIterator
->
current
()
->
n
!=
$this
->
chunkOffset
)
{
throw
new
\MongoDB\Exception\GridFSCorruptFileException
();
}
$chunkSizeIs
=
strlen
(
$this
->
chunksIterator
->
current
()
->
data
->
getData
());
if
(
$this
->
chunkOffset
==
$this
->
numChunks
-
1
)
{
$chunkSizeShouldBe
=
$this
->
file
->
length
-
$this
->
bytesSeen
;
if
(
$chunkSizeShouldBe
!=
$chunkSizeIs
)
{
throw
new
\MongoDB\Exception\GridFSCorruptFileException
();
}
}
else
if
(
$this
->
chunkOffset
<
$this
->
numChunks
-
1
)
{
if
(
$chunkSizeIs
!=
$this
->
file
->
chunkSize
)
{
throw
new
\MongoDB\Exception\GridFSCorruptFileException
();
throw
new
GridFSCorruptFileException
();
}
$actualChunkSize
=
strlen
(
$this
->
chunksIterator
->
current
()
->
data
->
getData
());
$expectedChunkSize
=
(
$this
->
chunkOffset
==
$this
->
numChunks
-
1
)
?
(
$this
->
file
->
length
-
$this
->
bytesSeen
)
:
$this
->
file
->
chunkSize
;
if
(
$actualChunkSize
!=
$expectedChunkSize
)
{
throw
new
GridFSCorruptFileException
();
}
$this
->
bytesSeen
+=
$chunkSizeIs
;
$this
->
bytesSeen
+=
$actualChunkSize
;
$this
->
chunkOffset
++
;
return
true
;
}
public
function
close
()
{
fclose
(
$this
->
buffer
);
}
public
function
isEOF
()
{
$eof
=
$this
->
iteratorEmpty
&&
$this
->
bufferEmpty
;
return
$eof
;
return
true
;
}
}
src/GridFS/GridF
s
Upload.php
→
src/GridFS/GridF
S
Upload.php
View file @
f2666e19
This diff is collapsed.
Click to expand it.
src/GridFS/StreamWrapper.php
View file @
f2666e19
<?php
namespace
MongoDB\GridFS
;
use
MongoDB\Collection
;
namespace
MongoDB\GridFS
;
/**
* Stream wrapper for reading and writing a GridFS file.
*
* @internal
* @see MongoDB\GridFS\Bucket::openUploadStream(), MongoDB\GridFS\Bucket::openDownloadStream()
* @see Bucket::openUploadStream()
* @see Bucket::openDownloadStream()
*/
class
StreamWrapper
{
public
$context
;
private
$filename
;
private
$protocol
=
'gridfs'
;
private
$mode
;
private
$gridFsStream
;
private
$collectionsWrapper
;
public
$id
;
private
$collectionsWrapper
;
private
$gridFSStream
;
private
$mode
;
public
function
openReadStream
()
{
$context
=
stream_context_get_options
(
$this
->
context
);
$this
->
gridFSStream
=
new
GridFSDownload
(
$this
->
collectionsWrapper
,
$context
[
'gridfs'
][
'file'
]);
$this
->
id
=
$this
->
gridFSStream
->
getId
();
return
true
;
}
public
function
openWriteStream
()
{
$context
=
stream_context_get_options
(
$this
->
context
);
$options
=
$context
[
'gridfs'
][
'uploadOptions'
];
$this
->
gridFSStream
=
new
GridFSUpload
(
$this
->
collectionsWrapper
,
$this
->
identifier
,
$options
);
$this
->
id
=
$this
->
gridFSStream
->
getId
();
return
true
;
}
/**
* Register the GridFS stream wrapper.
*/
...
...
@@ -27,56 +45,52 @@ class StreamWrapper
if
(
in_array
(
'gridfs'
,
stream_get_wrappers
()))
{
stream_wrapper_unregister
(
'gridfs'
);
}
stream_wrapper_register
(
'gridfs'
,
get_called_class
(),
STREAM_IS_URL
);
}
public
function
stream_write
(
$data
)
{
$this
->
gridFsStream
->
insertChunks
(
$data
);
return
strlen
(
$data
);
}
public
function
stream_read
(
$count
)
{
return
$this
->
gridFsStream
->
downloadNumBytes
(
$count
);
}
public
function
stream_eof
()
{
return
$this
->
gridFsStream
->
isEOF
();
stream_wrapper_register
(
'gridfs'
,
get_called_class
(),
\STREAM_IS_URL
);
}
public
function
stream_close
()
{
$this
->
gridF
s
Stream
->
close
();
$this
->
gridF
S
Stream
->
close
();
}
public
function
stream_stat
()
public
function
stream_eof
()
{
$stat
=
$this
->
getStatTemplate
();
$stat
[
7
]
=
$stat
[
'size'
]
=
$this
->
gridFsStream
->
getSize
();
return
$stat
;
return
$this
->
gridFSStream
->
isEOF
();
}
public
function
stream_open
(
$path
,
$mode
,
$options
,
&
$openedPath
)
{
$this
->
initProtocol
(
$path
);
$context
=
stream_context_get_options
(
$this
->
context
);
$this
->
collectionsWrapper
=
$context
[
'gridfs'
][
'collectionsWrapper'
];
$this
->
collectionsWrapper
=
$context
[
'gridfs'
][
'collectionsWrapper'
];
$this
->
mode
=
$mode
;
switch
(
$this
->
mode
)
{
case
'
w'
:
return
$this
->
openWrite
Stream
();
case
'
r'
:
return
$this
->
openRead
Stream
();
case
'
r'
:
return
$this
->
openRead
Stream
();
case
'
w'
:
return
$this
->
openWrite
Stream
();
default
:
return
false
;
}
}
public
function
openWriteStream
()
{
$context
=
stream_context_get_options
(
$this
->
context
);
$options
=
$context
[
'gridfs'
][
'uploadOptions'
];
$this
->
gridFsStream
=
new
GridFsUpload
(
$this
->
collectionsWrapper
,
$this
->
identifier
,
$options
);
$this
->
id
=
$this
->
gridFsStream
->
getId
();
return
true
;
public
function
stream_read
(
$count
)
{
return
$this
->
gridFSStream
->
downloadNumBytes
(
$count
);
}
public
function
openReadStream
()
{
$context
=
stream_context_get_options
(
$this
->
context
);
$this
->
gridFsStream
=
new
GridFsDownload
(
$this
->
collectionsWrapper
,
$context
[
'gridfs'
][
'file'
]);
$this
->
id
=
$this
->
gridFsStream
->
getId
();
return
true
;
public
function
stream_stat
()
{
$stat
=
$this
->
getStatTemplate
();
$stat
[
7
]
=
$stat
[
'size'
]
=
$this
->
gridFSStream
->
getSize
();
return
$stat
;
}
public
function
stream_write
(
$data
)
{
$this
->
gridFSStream
->
insertChunks
(
$data
);
return
strlen
(
$data
);
}
/**
...
...
@@ -102,10 +116,10 @@ class StreamWrapper
12
=>
-
1
,
'blocks'
=>
-
1
,
];
}
private
function
initProtocol
(
$path
)
{
$parsed_path
=
parse_url
(
$path
);
$this
->
databaseName
=
$parsed_path
[
"host"
];
$this
->
identifier
=
substr
(
$parsed_path
[
"path"
],
1
);
$this
->
identifier
=
substr
(
$parsed_path
[
'path'
],
1
);
}
}
tests/GridFS/BucketFunctionalTest.php
View file @
f2666e19
...
...
@@ -176,25 +176,25 @@ class BucketFunctionalTest extends FunctionalTestCase
$this
->
bucket
->
uploadFromStream
(
"test"
,
$this
->
generateStream
(
"bar"
));
$this
->
bucket
->
uploadFromStream
(
"test"
,
$this
->
generateStream
(
"baz"
));
$this
->
assertEquals
(
"foo"
,
stream_get_contents
(
$this
->
bucket
->
openDownloadStreamByName
(
"test"
,
0
)));
$this
->
assertEquals
(
"bar"
,
stream_get_contents
(
$this
->
bucket
->
openDownloadStreamByName
(
"test"
,
1
)));
$this
->
assertEquals
(
"baz"
,
stream_get_contents
(
$this
->
bucket
->
openDownloadStreamByName
(
"test"
,
2
)));
$this
->
assertEquals
(
"foo"
,
stream_get_contents
(
$this
->
bucket
->
openDownloadStreamByName
(
"test"
,
[
'revision'
=>
0
]
)));
$this
->
assertEquals
(
"bar"
,
stream_get_contents
(
$this
->
bucket
->
openDownloadStreamByName
(
"test"
,
[
'revision'
=>
1
]
)));
$this
->
assertEquals
(
"baz"
,
stream_get_contents
(
$this
->
bucket
->
openDownloadStreamByName
(
"test"
,
[
'revision'
=>
2
]
)));
$this
->
assertEquals
(
"baz"
,
stream_get_contents
(
$this
->
bucket
->
openDownloadStreamByName
(
"test"
,
-
1
)));
$this
->
assertEquals
(
"bar"
,
stream_get_contents
(
$this
->
bucket
->
openDownloadStreamByName
(
"test"
,
-
2
)));
$this
->
assertEquals
(
"foo"
,
stream_get_contents
(
$this
->
bucket
->
openDownloadStreamByName
(
"test"
,
-
3
)));
$this
->
assertEquals
(
"baz"
,
stream_get_contents
(
$this
->
bucket
->
openDownloadStreamByName
(
"test"
,
[
'revision'
=>
-
1
]
)));
$this
->
assertEquals
(
"bar"
,
stream_get_contents
(
$this
->
bucket
->
openDownloadStreamByName
(
"test"
,
[
'revision'
=>
-
2
]
)));
$this
->
assertEquals
(
"foo"
,
stream_get_contents
(
$this
->
bucket
->
openDownloadStreamByName
(
"test"
,
[
'revision'
=>
-
3
]
)));
$fileNotFound
=
'\MongoDB\Exception\GridFSFileNotFoundException'
;
$error
=
null
;
try
{
$this
->
bucket
->
openDownloadStreamByName
(
"test"
,
3
);
$this
->
bucket
->
openDownloadStreamByName
(
"test"
,
[
'revision'
=>
3
]
);
}
catch
(
\MongoDB\Exception\Exception
$e
)
{
$error
=
$e
;
}
$this
->
assertTrue
(
$error
instanceof
$fileNotFound
);
$error
=
null
;
try
{
$this
->
bucket
->
openDownloadStreamByName
(
"test"
,
-
4
);
$this
->
bucket
->
openDownloadStreamByName
(
"test"
,
[
'revision'
=>
-
4
]
);
}
catch
(
\MongoDB\Exception\Exception
$e
)
{
$error
=
$e
;
}
...
...
tests/GridFS/GridF
s
StreamTest.php
→
tests/GridFS/GridF
S
StreamTest.php
View file @
f2666e19
...
...
@@ -7,12 +7,12 @@ use MongoDB\GridFS;
/**
* Functional tests for the Bucket class.
*/
class
GridF
s
StreamTest
extends
FunctionalTestCase
class
GridF
S
StreamTest
extends
FunctionalTestCase
{
public
function
testBasic
()
{
$upload
=
new
\MongoDB\GridFS\GridF
s
Upload
(
$this
->
collectionsWrapper
,
"test"
);
$upload
=
new
\MongoDB\GridFS\GridF
S
Upload
(
$this
->
collectionsWrapper
,
"test"
);
$upload
->
insertChunks
(
"hello world"
);
$id
=
$upload
->
getId
();
$upload
->
close
();
...
...
@@ -22,7 +22,7 @@ class GridFsStreamTest extends FunctionalTestCase
$file
=
$this
->
collectionsWrapper
->
getFilesCollection
()
->
findOne
([
"_id"
=>
$id
]);
$download
=
new
\MongoDB\GridFS\GridF
s
Download
(
$this
->
collectionsWrapper
,
$file
);
$download
=
new
\MongoDB\GridFS\GridF
S
Download
(
$this
->
collectionsWrapper
,
$file
);
$stream
=
fopen
(
'php://temp'
,
'w+'
);
$download
->
downloadToStream
(
$stream
);
rewind
(
$stream
);
...
...
@@ -31,7 +31,7 @@ class GridFsStreamTest extends FunctionalTestCase
fclose
(
$stream
);
#make sure it's still there!
$download
=
new
\MongoDB\GridFS\GridF
s
Download
(
$this
->
collectionsWrapper
,
$file
);
$download
=
new
\MongoDB\GridFS\GridF
S
Download
(
$this
->
collectionsWrapper
,
$file
);
$stream
=
fopen
(
'php://temp'
,
'w+'
);
$download
->
downloadToStream
(
$stream
);
rewind
(
$stream
);
...
...
@@ -39,7 +39,7 @@ class GridFsStreamTest extends FunctionalTestCase
$this
->
assertEquals
(
"hello world"
,
$contents
);
fclose
(
$stream
);
$upload
=
new
\MongoDB\GridFS\GridF
s
Upload
(
$this
->
collectionsWrapper
,
"test"
);
$upload
=
new
\MongoDB\GridFS\GridF
S
Upload
(
$this
->
collectionsWrapper
,
"test"
);
$id
=
$upload
->
getId
();
$upload
->
close
();
...
...
@@ -47,7 +47,7 @@ class GridFsStreamTest extends FunctionalTestCase
$this
->
assertEquals
(
1
,
$this
->
collectionsWrapper
->
getChunksCollection
()
->
count
());
$file
=
$this
->
collectionsWrapper
->
getFilesCollection
()
->
findOne
([
"_id"
=>
$id
]);
$download
=
new
\MongoDB\GridFS\GridF
s
Download
(
$this
->
collectionsWrapper
,
$file
);
$download
=
new
\MongoDB\GridFS\GridF
S
Download
(
$this
->
collectionsWrapper
,
$file
);
$stream
=
fopen
(
'php://temp'
,
'w+'
);
$download
->
downloadToStream
(
$stream
);
rewind
(
$stream
);
...
...
@@ -58,7 +58,7 @@ class GridFsStreamTest extends FunctionalTestCase
public
function
testMd5
()
{
$upload
=
new
\MongoDB\GridFS\GridF
s
Upload
(
$this
->
collectionsWrapper
,
"test"
);
$upload
=
new
\MongoDB\GridFS\GridF
S
Upload
(
$this
->
collectionsWrapper
,
"test"
);
$upload
->
insertChunks
(
"hello world
\n
"
);
$id
=
$upload
->
getId
();
$upload
->
close
();
...
...
@@ -68,7 +68,7 @@ class GridFsStreamTest extends FunctionalTestCase
}
public
function
testUploadDefaultOpts
()
{
$upload
=
new
\MongoDB\GridFS\GridF
s
Upload
(
$this
->
collectionsWrapper
,
"test"
);
$upload
=
new
\MongoDB\GridFS\GridF
S
Upload
(
$this
->
collectionsWrapper
,
"test"
);
$this
->
assertTrue
(
$upload
->
getId
()
instanceof
\MongoDB\BSON\ObjectId
);
$this
->
assertTrue
(
$upload
->
getFile
()[
"uploadDate"
]
instanceof
\MongoDB\BSON\UTCDateTime
);
...
...
@@ -89,7 +89,7 @@ class GridFsStreamTest extends FunctionalTestCase
"aliases"
=>
[
"foo"
,
"bar"
],
"metadata"
=>
[
"foo"
=>
1
,
"bar"
=>
2
]
];
$upload
=
new
\MongoDB\GridFS\GridF
s
Upload
(
$this
->
collectionsWrapper
,
"test"
,
$options
);
$upload
=
new
\MongoDB\GridFS\GridF
S
Upload
(
$this
->
collectionsWrapper
,
"test"
,
$options
);
$this
->
assertEquals
(
$upload
->
getChunkSize
(),
1
);
$this
->
assertEquals
(
$upload
->
getFile
()[
"contentType"
],
"text/html"
);
$this
->
assertEquals
(
$upload
->
getFile
()[
"aliases"
],
[
"foo"
,
"bar"
]);
...
...
@@ -97,11 +97,11 @@ class GridFsStreamTest extends FunctionalTestCase
}
public
function
testDownloadDefaultOpts
()
{
$upload
=
new
\MongoDB\GridFS\GridF
s
Upload
(
$this
->
collectionsWrapper
,
"test"
);
$upload
=
new
\MongoDB\GridFS\GridF
S
Upload
(
$this
->
collectionsWrapper
,
"test"
);
$upload
->
close
();
$file
=
$this
->
collectionsWrapper
->
getFilesCollection
()
->
findOne
([
"_id"
=>
$upload
->
getId
()]);
$download
=
new
\MongoDB\GridFS\GridF
s
Download
(
$this
->
collectionsWrapper
,
$file
);
$download
=
new
\MongoDB\GridFS\GridF
S
Download
(
$this
->
collectionsWrapper
,
$file
);
$download
->
close
();
$this
->
assertEquals
(
$upload
->
getId
(),
$download
->
getId
());
...
...
@@ -120,12 +120,12 @@ class GridFsStreamTest extends FunctionalTestCase
"aliases"
=>
[
"foo"
,
"bar"
],
"metadata"
=>
[
"foo"
=>
1
,
"bar"
=>
2
]
];
$upload
=
new
\MongoDB\GridFS\GridF
s
Upload
(
$this
->
collectionsWrapper
,
"test"
,
$options
);
$upload
=
new
\MongoDB\GridFS\GridF
S
Upload
(
$this
->
collectionsWrapper
,
"test"
,
$options
);
$upload
->
insertChunks
(
"hello world"
);
$upload
->
close
();
$file
=
$this
->
collectionsWrapper
->
getFilesCollection
()
->
findOne
([
"_id"
=>
$upload
->
getId
()]);
$download
=
new
\MongoDB\GridFS\GridF
s
Download
(
$this
->
collectionsWrapper
,
$file
);
$download
=
new
\MongoDB\GridFS\GridF
S
Download
(
$this
->
collectionsWrapper
,
$file
);
$this
->
assertEquals
(
"test"
,
$download
->
getFile
()
->
filename
);
$this
->
assertEquals
(
$upload
->
getId
(),
$download
->
getId
());
...
...
@@ -141,7 +141,7 @@ class GridFsStreamTest extends FunctionalTestCase
*/
public
function
testInsertChunks
(
$data
)
{
$upload
=
new
\MongoDB\GridFS\GridF
s
Upload
(
$this
->
collectionsWrapper
,
"test"
);
$upload
=
new
\MongoDB\GridFS\GridF
S
Upload
(
$this
->
collectionsWrapper
,
"test"
);
$upload
->
insertChunks
(
$data
);
$upload
->
close
();
$stream
=
$this
->
bucket
->
openDownloadStream
(
$upload
->
getId
());
...
...
@@ -154,7 +154,7 @@ class GridFsStreamTest extends FunctionalTestCase
for
(
$i
=
0
;
$i
<
255
*
1024
+
1000
;
$i
++
){
$toUpload
.=
"a"
;
}
$upload
=
new
\MongoDB\GridFS\GridF
s
Upload
(
$this
->
collectionsWrapper
,
"test"
);
$upload
=
new
\MongoDB\GridFS\GridF
S
Upload
(
$this
->
collectionsWrapper
,
"test"
);
$upload
->
insertChunks
(
$toUpload
);
$upload
->
close
();
...
...
@@ -170,7 +170,7 @@ class GridFsStreamTest extends FunctionalTestCase
public
function
testSmallChunks
(
$data
)
{
$options
=
[
"chunkSizeBytes"
=>
1
];
$upload
=
new
\MongoDB\GridFS\GridF
s
Upload
(
$this
->
collectionsWrapper
,
"test"
,
$options
);
$upload
=
new
\MongoDB\GridFS\GridF
S
Upload
(
$this
->
collectionsWrapper
,
"test"
,
$options
);
$upload
->
insertChunks
(
$data
);
$upload
->
close
();
...
...
@@ -182,11 +182,11 @@ class GridFsStreamTest extends FunctionalTestCase
}
public
function
testMultipleReads
()
{
$upload
=
new
\MongoDB\GridFS\GridF
s
Upload
(
$this
->
collectionsWrapper
,
"test"
,
[
"chunkSizeBytes"
=>
3
]);
$upload
=
new
\MongoDB\GridFS\GridF
S
Upload
(
$this
->
collectionsWrapper
,
"test"
,
[
"chunkSizeBytes"
=>
3
]);
$upload
->
insertChunks
(
"hello world"
);
$upload
->
close
();
$file
=
$this
->
collectionsWrapper
->
getFilesCollection
()
->
findOne
([
"_id"
=>
$upload
->
getId
()]);
$download
=
new
\MongoDB\GridFS\GridF
s
Download
(
$this
->
collectionsWrapper
,
$file
);
$download
=
new
\MongoDB\GridFS\GridF
S
Download
(
$this
->
collectionsWrapper
,
$file
);
$this
->
assertEquals
(
"he"
,
$download
->
downloadNumBytes
(
2
));
$this
->
assertEquals
(
"ll"
,
$download
->
downloadNumBytes
(
2
));
$this
->
assertEquals
(
"o "
,
$download
->
downloadNumBytes
(
2
));
...
...
@@ -202,11 +202,11 @@ class GridFsStreamTest extends FunctionalTestCase
*/
public
function
testProvidedMultipleReads
(
$data
)
{
$upload
=
new
\MongoDB\GridFS\GridF
s
Upload
(
$this
->
collectionsWrapper
,
"test"
,
[
"chunkSizeBytes"
=>
rand
(
1
,
5
)]);
$upload
=
new
\MongoDB\GridFS\GridF
S
Upload
(
$this
->
collectionsWrapper
,
"test"
,
[
"chunkSizeBytes"
=>
rand
(
1
,
5
)]);
$upload
->
insertChunks
(
$data
);
$upload
->
close
();
$file
=
$this
->
collectionsWrapper
->
getFilesCollection
()
->
findOne
([
"_id"
=>
$upload
->
getId
()]);
$download
=
new
\MongoDB\GridFS\GridF
s
Download
(
$this
->
collectionsWrapper
,
$file
);
$download
=
new
\MongoDB\GridFS\GridF
S
Download
(
$this
->
collectionsWrapper
,
$file
);
$readPos
=
0
;
while
(
$readPos
<
strlen
(
$data
)){
...
...
@@ -227,7 +227,7 @@ class GridFsStreamTest extends FunctionalTestCase
*/
public
function
testUploadConstructorOptionTypeChecks
(
array
$options
)
{
new
\MongoDB\GridFS\GridF
s
Upload
(
$this
->
collectionsWrapper
,
"test"
,
$options
);
new
\MongoDB\GridFS\GridF
S
Upload
(
$this
->
collectionsWrapper
,
"test"
,
$options
);
}
public
function
provideInvalidUploadConstructorOptions
()
...
...
@@ -248,22 +248,4 @@ class GridFsStreamTest extends FunctionalTestCase
}
return
$options
;
}
/**
* @expectedException \MongoDB\Exception\InvalidArgumentTypeException
* @dataProvider provideInvalidDownloadConstructorFile
*/
public
function
testDownloadConstructorFileCheck
(
$file
)
{
$download
=
new
\MongoDB\GridFS\GridFsDownload
(
$this
->
collectionsWrapper
,
$file
);
}
public
function
provideInvalidDownloadConstructorFile
()
{
$files
=
[];
$invalidFiles
=
[
123
,
3.14
,
true
,
[]];
foreach
(
$invalidFiles
as
$value
)
{
$files
[][]
=
$value
;
}
return
$files
;
}
}
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