Paul Klimov
11 years ago
8 changed files with 319 additions and 2 deletions
@ -0,0 +1,109 @@
|
||||
<?php |
||||
/** |
||||
* @link http://www.yiiframework.com/ |
||||
* @copyright Copyright (c) 2008 Yii Software LLC |
||||
* @license http://www.yiiframework.com/license/ |
||||
*/ |
||||
|
||||
namespace yii\mongo\file; |
||||
|
||||
use yii\mongo\Exception; |
||||
|
||||
/** |
||||
* Collection represents the Mongo GridFS collection information. |
||||
* |
||||
* @method \MongoGridFSCursor find() returns a cursor for the search results. |
||||
* |
||||
* @author Paul Klimov <klimov.paul@gmail.com> |
||||
* @since 2.0 |
||||
*/ |
||||
class Collection extends \yii\mongo\Collection |
||||
{ |
||||
/** |
||||
* @var \MongoGridFS Mongo GridFS collection instance. |
||||
*/ |
||||
public $mongoCollection; |
||||
|
||||
/** |
||||
* Removes data from the collection. |
||||
* @param array $condition description of records to remove. |
||||
* @param array $options list of options in format: optionName => optionValue. |
||||
* @return integer|boolean number of updated documents or whether operation was successful. |
||||
* @throws Exception on failure. |
||||
*/ |
||||
public function remove($condition = [], $options = []) |
||||
{ |
||||
$result = parent::remove($condition, $options); |
||||
$this->tryLastError(); // MongoGridFS::remove will return even if the remove failed |
||||
return $result; |
||||
} |
||||
|
||||
/** |
||||
* @param string $filename name of the file to store. |
||||
* @param array $metadata other metadata fields to include in the file document. |
||||
* @return mixed the "_id" of the saved file document. This will be a generated [[\MongoId]] |
||||
* unless an "_id" was explicitly specified in the metadata. |
||||
*/ |
||||
public function put($filename, $metadata = []) |
||||
{ |
||||
return $this->mongoCollection->put($filename, $metadata); |
||||
} |
||||
|
||||
/** |
||||
* @param string $bytes string of bytes to store. |
||||
* @param array $metadata other metadata fields to include in the file document. |
||||
* @param array $options list of options in format: optionName => optionValue |
||||
* @return mixed the "_id" of the saved file document. This will be a generated [[\MongoId]] |
||||
* unless an "_id" was explicitly specified in the metadata. |
||||
*/ |
||||
public function storeBytes($bytes, $metadata = [], $options = []) |
||||
{ |
||||
$options = array_merge(['w' => 1], $options); |
||||
return $this->mongoCollection->storeBytes($bytes, $metadata, $options); |
||||
} |
||||
|
||||
/** |
||||
* @param string $filename name of the file to store. |
||||
* @param array $metadata other metadata fields to include in the file document. |
||||
* @param array $options list of options in format: optionName => optionValue |
||||
* @return mixed the "_id" of the saved file document. This will be a generated [[\MongoId]] |
||||
* unless an "_id" was explicitly specified in the metadata. |
||||
*/ |
||||
public function storeFile($filename, $metadata = [], $options = []) |
||||
{ |
||||
$options = array_merge(['w' => 1], $options); |
||||
return $this->mongoCollection->storeFile($filename, $metadata, $options); |
||||
} |
||||
|
||||
/** |
||||
* @param string $name name of the uploaded file to store. This should correspond to |
||||
* the file field's name attribute in the HTML form. |
||||
* @param array $metadata other metadata fields to include in the file document. |
||||
* @return mixed the "_id" of the saved file document. This will be a generated [[\MongoId]] |
||||
* unless an "_id" was explicitly specified in the metadata. |
||||
*/ |
||||
public function storeUploads($name, $metadata = []) |
||||
{ |
||||
return $this->mongoCollection->storeUpload($name, $metadata); |
||||
} |
||||
|
||||
/** |
||||
* @param mixed $id _id of the file to find. |
||||
* @return \MongoGridFSFile|null found file, or null if file does not exist |
||||
*/ |
||||
public function get($id) |
||||
{ |
||||
return $this->mongoCollection->get($id); |
||||
} |
||||
|
||||
/** |
||||
* @param mixed $id _id of the file to find. |
||||
* @return boolean whether the operation was successful. |
||||
*/ |
||||
public function delete($id) |
||||
{ |
||||
$result = $this->mongoCollection->delete($id); |
||||
$this->tryResultError($result); |
||||
return true; |
||||
} |
||||
} |
@ -0,0 +1,87 @@
|
||||
<?php |
||||
|
||||
namespace yiiunit\extensions\mongo\file; |
||||
|
||||
use yiiunit\extensions\mongo\MongoTestCase; |
||||
|
||||
class CollectionTest extends MongoTestCase |
||||
{ |
||||
protected function tearDown() |
||||
{ |
||||
$this->dropFileCollection('fs'); |
||||
parent::tearDown(); |
||||
} |
||||
|
||||
// Tests : |
||||
|
||||
public function testFind() |
||||
{ |
||||
$collection = $this->getConnection()->getFileCollection(); |
||||
$cursor = $collection->find(); |
||||
$this->assertTrue($cursor instanceof \MongoGridFSCursor); |
||||
} |
||||
|
||||
public function testStoreFile() |
||||
{ |
||||
$collection = $this->getConnection()->getFileCollection(); |
||||
|
||||
$filename = __FILE__; |
||||
$id = $collection->storeFile($filename); |
||||
$this->assertTrue($id instanceof \MongoId); |
||||
|
||||
$files = $this->findAll($collection); |
||||
$this->assertEquals(1, count($files)); |
||||
|
||||
/** @var $file \MongoGridFSFile */ |
||||
$file = $files[0]; |
||||
$this->assertEquals($filename, $file->getFilename()); |
||||
$this->assertEquals(file_get_contents($filename), $file->getBytes()); |
||||
} |
||||
|
||||
public function testStoreBytes() |
||||
{ |
||||
$collection = $this->getConnection()->getFileCollection(); |
||||
|
||||
$bytes = 'Test file content'; |
||||
$id = $collection->storeBytes($bytes); |
||||
$this->assertTrue($id instanceof \MongoId); |
||||
|
||||
$files = $this->findAll($collection); |
||||
$this->assertEquals(1, count($files)); |
||||
|
||||
/** @var $file \MongoGridFSFile */ |
||||
$file = $files[0]; |
||||
$this->assertEquals($bytes, $file->getBytes()); |
||||
} |
||||
|
||||
/** |
||||
* @depends testStoreBytes |
||||
*/ |
||||
public function testGet() |
||||
{ |
||||
$collection = $this->getConnection()->getFileCollection(); |
||||
|
||||
$bytes = 'Test file content'; |
||||
$id = $collection->storeBytes($bytes); |
||||
|
||||
$file = $collection->get($id); |
||||
$this->assertTrue($file instanceof \MongoGridFSFile); |
||||
$this->assertEquals($bytes, $file->getBytes()); |
||||
} |
||||
|
||||
/** |
||||
* @depends testGet |
||||
*/ |
||||
public function testDelete() |
||||
{ |
||||
$collection = $this->getConnection()->getFileCollection(); |
||||
|
||||
$bytes = 'Test file content'; |
||||
$id = $collection->storeBytes($bytes); |
||||
|
||||
$this->assertTrue($collection->delete($id)); |
||||
|
||||
$file = $collection->get($id); |
||||
$this->assertNull($file); |
||||
} |
||||
} |
Loading…
Reference in new issue