Skip to content
Browse files

adding JSON database loader

git-svn-id: http://mongodb-morph.googlecode.com/svn/trunk@111 406649b4-5d4b-11de-8abc-d1c337b90d21
  • Loading branch information...
1 parent 8ef8bc1 commit d3580526d0974021ed04cdeef41b334e8710e1d8 XirisR committed Feb 1, 2010
View
47 mongodb-morph/integration-tests/MongoUnit/JsonLoader.php
@@ -0,0 +1,47 @@
+<?php
+/**
+ * @package MongoUnit
+ * @author Jonathan Moss <jonathan.moss@tangentlabs.co.uk>
+ * @copyright 2010 Tangent Labs
+ * @version SVN: $Id$
+ */
+
+/**
+ * Loads data into a collection
+ *
+ * @package MongoUnit
+ */
+class MongoUnit_JsonLoader
+{
+
+ private $collection;
+
+ public function __construct(MongoCollection $collection)
+ {
+ $this->collection = $collection;
+ }
+
+ public function loadFromJsonFile($filePath)
+ {
+ if (!file_exists($filePath)) {
+ throw new InvalidArgumentException("Could not locate $filePath");
+ }
+ $json = file_get_contents($filePath);
+ return $this->loadJsonFromString($json);
+ }
+
+ public function loadJsonFromString($json)
+ {
+ if (!$this->isValidJson($json)) {
+ throw new InvalidArgumentException("Invalid Json");
+ }
+ $data = json_decode($json, true);
+ $this->collection->drop();
+ return $this->collection->batchInsert($data);
+ }
+
+ private function isValidJson($json)
+ {
+ return (json_decode($json) !== null) ? true : false;
+ }
+}
View
38 mongodb-morph/integration-tests/MongoUnit/TestCase.php
@@ -6,7 +6,7 @@
*/
require_once 'PHPUnit/Framework/TestCase.php';
-
+require_once dirname(__FILE__).'/JsonLoader.php';
require_once dirname(__FILE__).'/Constraint/CollectionExists.php';
require_once dirname(__FILE__).'/Constraint/CollectionDoesNotExist.php';
require_once dirname(__FILE__).'/Constraint/DocumentExists.php';
@@ -192,4 +192,40 @@ public function assertDocumentPropertyEquals($expected, $collection, $property,
self::assertThat($id, $constraint, $message);
}
+ //////////////////////////////////
+ // Collection Content Functions //
+ //////////////////////////////////
+
+ /**
+ * Clear the specified collection and populates it with the content of $filePath
+ *
+ * The specified file should contain a JSON encoded array of documents
+ *
+ * @param string $collectionName
+ * @param string $filePath
+ * @return boolean
+ */
+ public function loadJsonFileDatasetIntoCollection($collectionName, $filePath)
+ {
+ $collection = $this->getDatabase()->selectCollection($collectionName);
+ $loader = new MongoUnit_JsonLoader($collection);
+ return $loader->loadFromJsonFile($filePath);
+ }
+
+ /**
+ * Clear the specified collection and populates it with the $json
+ *
+ * $json should be a JSON encoded array of documents
+ *
+ * @param string $collectionName
+ * @param string $json
+ * @return boolean
+ */
+ public function loadJsonStringDatasetIntoCollection($collectionName, $json)
+ {
+ $collection = $this->getDatabase()->selectCollection($collectionName);
+ $loader = new MongoUnit_JsonLoader($collection);
+ return $loader->loadJsonFromString($json);
+ }
+
}

0 comments on commit d358052

Please sign in to comment.
Something went wrong with that request. Please try again.