Permalink
Browse files

Merge pull request #177 from sellingsource/master

Base64 encode binary data before logging
  • Loading branch information...
2 parents dd38bdf + e3c9482 commit 00568cc51fac4430856a362de250f30f6254e5da @jmikola jmikola committed Mar 18, 2013
@@ -216,7 +216,7 @@ private function bsonEncode($query, $array = true)
} elseif ($value instanceof \MongoMaxKey) {
$formatted = 'new MaxKey()';
} elseif ($value instanceof \MongoBinData) {
- $formatted = 'new BinData("'.$value->bin.'", "'.$value->type.'")';
+ $formatted = 'new BinData('.$value->type.', "'.base64_encode($value->bin).'")';
} elseif ($value instanceof \MongoGridFSFile || $value instanceof GridFSFile) {
$formatted = 'new MongoGridFSFile("'.$value->getFilename().'")';
} elseif ($value instanceof \stdClass) {
View
@@ -48,6 +48,12 @@ public function logQuery(array $query)
$query['data'] = '**'.$query['num'].' item(s)**';
}
+ array_walk_recursive($query, function(&$value, $key) {
+ if ($value instanceof \MongoBinData) {
+ $value = base64_encode($value->bin);
+ }
+ });
+
$this->logger->info($this->prefix.json_encode($query));
}
}
@@ -36,16 +36,18 @@ public function testCollect($query, $formatted)
public function getQueries()
{
return array(
- // batchInsert
- array(
+ 'batch insert' => array(
array('db' => 'foo', 'collection' => 'bar', 'batchInsert' => true, 'num' => 1, 'data' => array('foo' => 'bar'), 'options' => array()),
array('use foo;', 'db.bar.insert({ "foo": "bar" });'),
),
- // find
- array(
+ 'find' => array(
array('db' => 'foo', 'collection' => 'bar', 'find' => true, 'query' => array('foo' => null), 'fields' => array()),
array('use foo;', 'db.bar.find({ "foo": null });'),
),
+ 'bin data' => array(
+ array('db' => 'foo', 'collection' => 'bar', 'update' => true, 'query' => array('_id' => 'foo'), 'newObj' => array('foo' => new \MongoBinData('junk data', \MongoBinData::BYTE_ARRAY))),
+ arraY('use foo;', 'db.bar.update({ "_id": "foo" }, { "foo": new BinData(2, "' . base64_encode('junk data') . '") });'),
+ )
);
}
}
@@ -42,4 +42,32 @@ public function testLogQuery()
$logger = new Logger($this->logger);
$logger->logQuery(array('foo' => 'bar'));
}
+
+ public function testMongoBinDataBase64Encoded()
+ {
+ $binData = new \MongoBinData('junk data', \MongoBinData::BYTE_ARRAY);
+ $query = array('foo' => base64_encode($binData->bin));
+ $log = json_encode($query);
+
+ $this->logger->expects($this->once())
+ ->method('info')
+ ->with('MongoDB query: '.$log);
+
+ $logger = new Logger($this->logger);
+ $logger->logQuery(array('foo' => new \MongoBinData('junk data', \MongoBinData::BYTE_ARRAY)));
+ }
+
+ public function testMongoBinDataBase64EncodedRecursively()
+ {
+ $binData = new \MongoBinData('junk data', \MongoBinData::BYTE_ARRAY);
+ $query = array('foo' => array('bar' => base64_encode($binData->bin)));
+ $log = json_encode($query);
+
+ $this->logger->expects($this->once())
+ ->method('info')
+ ->with('MongoDB query: '.$log);
+
+ $logger = new Logger($this->logger);
+ $logger->logQuery(array('foo' => array('bar' => new \MongoBinData('junk data', \MongoBinData::BYTE_ARRAY))));
+ }
}

0 comments on commit 00568cc

Please sign in to comment.