Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test cases for Alternative GridFS collection names issue #766. #777

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
56 changes: 56 additions & 0 deletions tests/cases/data/source/MongoDbTest.php
Expand Up @@ -838,6 +838,62 @@ public function testDefaultSafeOptions() {
$this->assertEqual('remove', $result['method']);
$this->assertEqual($expected, $result['params'][1]);
}

public function testCreateGridfsDefaultName() {
$source = 'fs.files';

$model = $this->_model;
$model::config(array('meta' => array('source' => $source, 'connection' => 'default', 'locked' => false)));

$data = array('file' => 'xxx', 'filename' => 'xxx');

$model::create()->save($data);
$result = $this->db->connection->gridFSinstance;

$this->assertTrue(is_object($result));
$this->assertEqual($source, $result->filesName);
}

/**
* This test fails in current conditions.
*/
public function testCreateGridfsWrongPrefix() {
$source = 'somename.files';

$model = $this->_model;
$model::config(array('meta' => array('source' => $source, 'connection' => 'default', 'locked' => false)));

$data = array('file' => 'xxx', 'filename' => 'xxx');

$model::create()->save($data);
$result = $this->db->connection->gridFSinstance;

// Expected this will fail, because $result is not object.
$this->expectException('Trying to get property of non-object');

$result->fileNames;
}

/**
* This test fails in current conditions.
*/
public function testCreateGridfsAlternativeName() {
$source = 'somename.files';

$this->_testConfig['gridPrefix'] = 'somename';
$this->setUp();

$model = $this->_model;
$model::config(array('meta' => array('source' => $source, 'connection' => 'default', 'locked' => false)));

$data = array('file' => 'xxx', 'filename' => 'xxx');

$model::create()->save($data);
$result = $this->db->connection->gridFSinstance;

$this->assertTrue(is_object($result));
$this->assertEqual($source, $result->filesName);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this test really pass ?

}
}

?>
7 changes: 7 additions & 0 deletions tests/mocks/data/source/MockMongoConnection.php
Expand Up @@ -18,6 +18,8 @@ class MockMongoConnection {

protected $_collection = null;

public $gridFSinstance = null;

public function connect() {
return false;
}
Expand Down Expand Up @@ -57,6 +59,11 @@ public function find($conditions, $fields) {
public function listCollections() {
return $this->_record(__FUNCTION__);
}

public function getGridFS($prefix = "fs") {
$this->gridFSinstance = new MockMongoGridFS(null, $prefix, $prefix);
return $this->gridFSinstance;
}
}

?>
50 changes: 50 additions & 0 deletions tests/mocks/data/source/MockMongoGridFS.php
@@ -0,0 +1,50 @@
<?php
/**
* Lithium: the most rad php framework
*
* @copyright Copyright 2013, Union of RAD (http://union-of-rad.org)
* @license http://opensource.org/licenses/bsd-license.php The BSD License
*/

namespace lithium\tests\mocks\data\source;

class MockMongoGridFS {

public $queries = array();
public $results = array();

public $filesName = NULL;
public $chunksName = NULL;

protected $_collection = null;

/**
* From the official MongoDB documentation:
* GridFS places the collections in a common bucket by prefixing each with the bucket name. By default, GridFS uses two collections
* with names prefixed by fs bucket:
* fs.files
* fs.chunks
*
* From PHP documentation:
* Files as stored across two collections, the first containing file meta information, the second containing chunks of the actual file.
* By default, fs.files and fs.chunks are the collection names used
*/
public function __construct (MongoDB $db = null, $prefix = "fs", $chunks = "fs"){
$this->filesName = $prefix . '.files';
$this->chunksName = $chunks . '.chunks';

return;
}

protected function _record($type, array $data = array()) {
$collection = $this->_collection;
$this->queries[] = compact('type', 'collection') + $data;
return array_pop($this->results);
}

public function storeBytes($bytes = null, array $extra = array(), array $options = array()) {
return;
}
}

?>