Skip to content
This repository has been archived by the owner on Jan 21, 2020. It is now read-only.

Commit

Permalink
Merge pull request #1606 from fauvel/issue-1606
Browse files Browse the repository at this point in the history
Implement CM_Model_StorageAdapter_FindableInterface on CM_Model_StorageAdapter_MongoDb
  • Loading branch information
fauvel committed Jan 28, 2015
2 parents 4e7c6c2 + 9d90d4b commit b329097
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
11 changes: 10 additions & 1 deletion library/CM/Model/StorageAdapter/MongoDb.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
<?php

class CM_Model_StorageAdapter_MongoDb extends CM_Model_StorageAdapter_AbstractAdapter {
class CM_Model_StorageAdapter_MongoDb extends CM_Model_StorageAdapter_AbstractAdapter implements CM_Model_StorageAdapter_FindableInterface {

public function findByData($type, array $data) {
$result = $this->_getMongoDb()->findOne($this->_getCollectionName($type), $data, ['_id']);
if (null === $result) {
return null;
}
$mongoId = $result['_id'];
return ['id' => (string) $mongoId];
}

public function load($type, array $id) {
$type = (int) $type;
Expand Down
13 changes: 13 additions & 0 deletions tests/library/CM/Model/StorageAdapter/MongoDbTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,19 @@ public function testLoadMultiple() {
$this->assertEquals($expected, $values);
}

public function testFindByData() {
$type = 99;
$adapter = $this->_getAdapter();
$id1 = $adapter->create($type, ['foo' => 'foo1', 'bar' => 1]);
$id2 = $adapter->create($type, ['foo' => 'foo2', 'bar' => 2]);

$this->assertSame($id1, $adapter->findByData($type, array('foo' => 'foo1')));
$this->assertSame($id1, $adapter->findByData($type, array('bar' => 1)));
$this->assertSame($id1, $adapter->findByData($type, array('foo' => 'foo1', 'bar' => 1)));
$this->assertSame($id2, $adapter->findByData($type, array('foo' => 'foo2')));
$this->assertNull($adapter->findByData($type, array('foo' => 'foo2', 'bar' => 1)));
}

/**
* @return CM_Model_StorageAdapter_MongoDb
*/
Expand Down

0 comments on commit b329097

Please sign in to comment.