Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

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

Closed
wants to merge 3 commits into from

4 participants

@3d0c

Test cases for issue #766

tests/cases/data/source/MongoDbTest.php
((19 lines not shown))
+
+ /**
+ * 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.
@jails Collaborator
jails added a note

If you expect an exception you need to use something like: $this->expectException('/The error message regexp/');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
tests/cases/data/source/MongoDbTest.php
@@ -2,7 +2,7 @@
/**
* Lithium: the most rad php framework
*
- * @copyright Copyright 2013, Union of RAD (http://union-of-rad.org)
+ * @copyright Copyright 2012, Union of RAD (http://union-of-rad.org)
@jails Collaborator
jails added a note

Any chance having this PR rebased on HEAD dev ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
tests/mocks/data/source/MockMongoConnection.php
@@ -57,6 +59,11 @@ public function find($conditions, $fields) {
public function listCollections() {
return $this->_record(__FUNCTION__);
}
+
+ public function getGridFS() {
@jails Collaborator
jails added a note

Here getGridFS() should take the same argument you have provided in your PR (e.g. getGridFS($prefix = 'fs')) imo.

@3d0c
3d0c added a note

I'm afraid — no, because this method emulates current lithium behaviour — this is a main problem, i wish to solve.
Take a look into lithium\data\source\MongoDb line 412

$grid = $this->connection->getGridFS();

This call never returns something other than default gridfs collection.

@jails Collaborator
jails added a note

What I meant here is you need to build your test upon your PR (which solve the problem of always returning the default gridfs collection). In #766 you changed:

$grid = $this->connection->getGridFS();

to:

$grid = $this->connection->getGridFS($this->_config['gridPrefix']);

So your mock should be representative of the class you are mocking. I don't think you'll be able to build valid tests otherwise. This way you only need to test if passed parameter is correct over different CRUD operations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@jails jails commented on the diff
tests/cases/data/source/MongoDbTest.php
((41 lines not shown))
+ 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);
@jails Collaborator
jails added a note

Is this test really pass ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
tests/mocks/data/source/MockMongoGridFS.php
@@ -0,0 +1,50 @@
+<?php
+/**
+ * Lithium: the most rad php framework
+ *
+ * @copyright Copyright 2012, Union of RAD (http://union-of-rad.org)
@jails Collaborator
jails added a note

Happy New Year 2013 ;-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@nateabele
Owner

@3d0c Sweet! Thanks for the comprehensive patch. @jails Thanks for going through it and making proper notations.

@nateabele
Owner

Looks like the build failed for internal Travis reasons. Anybody know how to trigger it to run again on a pull request?

@blainesch
Collaborator

Not easily on non-master branch. You can change something slightly in the last commit and force push... or do a blank commit git commit --allow-empty push it then remove it and force push? This first push will trigger a travis fail since it won't be able to find it, the second one will pass.

Sounds messy.

@nateabele
Owner

What's weird is, he just added another commit, which should have triggered another build, but I'm not seeing any.

@jails
Collaborator

Yeah sometimes Travis plays his Joker...

@blainesch
Collaborator

Travis may be lagging. Amazing free service needs more funding lol

Could you run the tests locally?

@jails
Collaborator

Anyway this test can't pass w/o #766.

@jails jails closed this
@jails
Collaborator

merged in #778

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 7, 2013
  1. @3d0c
Commits on Jan 8, 2013
  1. @3d0c

    Fixes for issue #766

    3d0c authored
  2. @3d0c

    Fixes for issue #777

    3d0c authored
This page is out of date. Refresh to see the latest.
View
56 tests/cases/data/source/MongoDbTest.php
@@ -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);
@jails Collaborator
jails added a note

Is this test really pass ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ }
+
+ /**
+ * 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);
+ }
}
?>
View
7 tests/mocks/data/source/MockMongoConnection.php
@@ -18,6 +18,8 @@ class MockMongoConnection {
protected $_collection = null;
+ public $gridFSinstance = null;
+
public function connect() {
return false;
}
@@ -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;
+ }
}
?>
View
50 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;
+ }
+}
+
+?>
Something went wrong with that request. Please try again.