Skip to content

Commit

Permalink
Merge pull request UnionOfRAD#994 from jails/refactor/make-mongo-opti…
Browse files Browse the repository at this point in the history
…onal-for-tests

Skip tests which require Mongo if Mongo is not installed (close UnionOfRAD#991).
  • Loading branch information
nateabele committed Jul 22, 2013
2 parents a509aba + 1791dd9 commit 6fba0e0
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 65 deletions.
2 changes: 1 addition & 1 deletion data/Schema.php
Expand Up @@ -24,7 +24,7 @@ class Schema extends \lithium\core\Object implements \ArrayAccess {

protected $_types = array();

protected $_autoConfig = array('fields', 'meta', 'locked');
protected $_autoConfig = array('fields', 'meta', 'locked', 'types');

public function __construct(array $config = array()) {
$defaults = array('fields' => array());
Expand Down
81 changes: 51 additions & 30 deletions tests/cases/data/collection/DocumentSetTest.php
Expand Up @@ -9,22 +9,20 @@
namespace lithium\tests\cases\data\collection;

use lithium\data\Connections;
use lithium\data\source\MongoDb;
use lithium\data\source\mongo_db\Schema;
use lithium\data\DocumentSchema;
use lithium\data\entity\Document;
use lithium\data\collection\DocumentSet;
use lithium\tests\mocks\data\model\MockDocumentPost;
use lithium\tests\mocks\data\source\mongo_db\MockResult;
use lithium\tests\mocks\data\source\MockMongoConnection;
use lithium\tests\mocks\data\MockDocumentSource;
use lithium\util\Collection;

class DocumentSetTest extends \lithium\test\Unit {

protected $_model = 'lithium\tests\mocks\data\model\MockDocumentPost';

public function setUp() {
$connection = new MongoDb(array('autoConnect' => false));
$connection->connection = new MockMongoConnection();
$connection = new MockDocumentSource();
Connections::add('mockconn', array('object' => $connection));
MockDocumentPost::config(array('meta' => array('connection' => 'mockconn')));
}
Expand All @@ -36,11 +34,19 @@ public function tearDown() {

public function testInitialCasting() {
$model = $this->_model;
$schema = new Schema(array('fields' => array(
'_id' => array('type' => 'id'),
'foo' => array('type' => 'object'),
'foo.bar' => array('type' => 'int')
)));
$schema = new DocumentSchema(array(
'fields' => array(
'_id' => array('type' => 'id'),
'foo' => array('type' => 'object'),
'foo.bar' => array('type' => 'int')
),
'types' => array(
'int' => 'integer'
),
'handlers' => array(
'integer' => function($v) { return (integer) $v; },
)
));

$array = new DocumentSet(compact('model', 'schema') + array(
'pathKey' => 'foo.bar',
Expand All @@ -55,12 +61,20 @@ public function testInitialCasting() {
public function testInitialCastingOnSubObject() {
$model = $this->_model;

$schema = new Schema(array('fields' => array(
'_id' => array('type' => 'id'),
'body' => array('type' => 'string'),
'foo' => array('type' => 'object'),
'foo.bar' => array('type' => 'int')
)));
$schema = new DocumentSchema(array(
'fields' => array(
'_id' => array('type' => 'id'),
'body' => array('type' => 'string'),
'foo' => array('type' => 'object'),
'foo.bar' => array('type' => 'int')
),
'types' => array(
'int' => 'integer'
),
'handlers' => array(
'integer' => function($v) { return (integer) $v; },
)
));

$array = new DocumentSet(compact('model', 'schema') + array(
'data' => array(
Expand All @@ -83,7 +97,7 @@ public function testInitialCastingOnSubObject() {
));

foreach ($array as $document) {
$this->assertInstanceOf('MongoId', $document->_id);
$this->assertInternalType('string', $document->_id);
$this->assertInternalType('string', $document->body);
$this->assertInternalType('object', $document->foo);
$this->assertInternalType('string', $document->foo->bar);
Expand All @@ -110,7 +124,7 @@ public function testInitialCastingOnSubObject() {
));

foreach ($array as $document) {
$this->assertInstanceOf('MongoId', $document->_id);
$this->assertInternalType('string', $document->_id);
$this->assertInternalType('string', $document->body);
$this->assertInternalType('object', $document->foo);
$this->assertInternalType('int', $document->foo->bar);
Expand Down Expand Up @@ -166,7 +180,7 @@ public function testUnsetInForeach() {
}

public function testArrayOfObjects() {
$schema = new Schema();
$schema = new DocumentSchema();
$first = (object) array('name' => 'First');
$second = (object) array('name' => 'Second');
$third = (object) array('name' => 'Third');
Expand Down Expand Up @@ -196,7 +210,7 @@ public function testPopulateResourceClose() {

$result = $doc->rewind();
$this->assertInstanceOf('lithium\data\entity\Document', $result);
$this->assertInternalType('object', $result['_id']);
$this->assertInternalType('string', $result['_id']);

$expected = array('_id' => '4c8f86167675abfabdbf0300', 'title' => 'bar');
$this->assertEqual($expected, $result->data());
Expand Down Expand Up @@ -303,7 +317,7 @@ public function testTo() {

public function testParent() {
$model = $this->_model;
$schema = new Schema(array('fields' => array(
$schema = new DocumentSchema(array('fields' => array(
'_id' => array('type' => 'id'),
'bar' => array('array' => true),
'foo' => array('type' => 'object', 'array' => true),
Expand Down Expand Up @@ -332,26 +346,33 @@ public function testParent() {

public function testHandlers() {
$model = $this->_model;
$schema = new Schema(array('fields' => array(
'_id' => array('type' => 'id'),
'date' => array('type' => 'date')
)));
$schema = new DocumentSchema(array(
'fields' => array(
'_id' => array('type' => 'id'),
'date' => array('type' => 'date')
),
'types' => array(
'date' => 'date'
),
'handlers' => array(
'date' => function($v) { return (object) $v; },
)
));
$handlers = array(
'MongoId' => function($value) { return substr((string) $value, -1); },
'MongoDate' => function($value) { return date('d/m/Y H:i', $value->sec); }
'stdClass' => function($value) { return date('d/m/Y H:i', strtotime($value->scalar)); }
);
$array = new DocumentSet(compact('model', 'schema', 'handlers') + array(
'data' => array(
array(
'_id' => '4cb4ab6d7addf98506010002',
'_id' => '2',
'date' => '2013-06-06 13:00:00'
),
array(
'_id' => '4cb4ab6d7addf98506010003',
'_id' => '3',
'date' => '2013-06-06 12:00:00'
),
array(
'_id' => '4cb4ab6d7addf98506010004',
'_id' => '4',
'date' => '2013-06-06 11:00:00'
)
)
Expand Down
50 changes: 20 additions & 30 deletions tests/cases/data/entity/DocumentTest.php
Expand Up @@ -8,23 +8,19 @@

namespace lithium\tests\cases\data\entity;

use MongoId;
use MongoDate;
use lithium\data\Connections;
use lithium\data\source\MongoDb;
use lithium\data\DocumentSchema;
use lithium\data\entity\Document;
use lithium\data\collection\DocumentSet;
use lithium\data\source\mongo_db\Schema;
use lithium\tests\mocks\data\MockDocumentSource;
use lithium\tests\mocks\data\model\MockDocumentPost;
use lithium\tests\mocks\data\source\MockMongoConnection;

class DocumentTest extends \lithium\test\Unit {

protected $_model = 'lithium\tests\mocks\data\model\MockDocumentPost';

public function setUp() {
$connection = new MongoDb(array('autoConnect' => false));
$connection->connection = new MockMongoConnection();
$connection = new MockDocumentSource();
Connections::add('mockconn', array('object' => $connection));
MockDocumentPost::config(array('meta' => array('connection' => 'mockconn')));
}
Expand Down Expand Up @@ -183,7 +179,7 @@ public function testSyncModified() {
}

public function testSetAndCoerceArray() {
$schema = new Schema(array('fields' => array(
$schema = new DocumentSchema(array('fields' => array(
'forceArray' => array('type' => 'string', 'array' => true),
'array' => array('type' => 'string', 'array' => true),
'dictionary' => array('type' => 'string', 'array' => true),
Expand Down Expand Up @@ -821,19 +817,6 @@ public function testModifiedExport() {
$this->assertEqual(array('evenMore' => 'foo!') + $nested['data'], $nested['update']);
}

public function testArrayConversion() {
$this->skipIf(!MongoDb::enabled(), "MongoDB not enabled, skipping conversion tests.");

$time = time();
$doc = new Document(array('data' => array(
'_id' => new MongoId(),
'date' => new MongoDate($time)
)));
$result = $doc->data();
$this->assertPattern('/^[a-f0-9]{24}$/', $result['_id']);
$this->assertEqual($time, $result['date']);
}

public function testArrayInterface() {
$doc = new Document();
$doc->field = 'value';
Expand All @@ -850,7 +833,7 @@ public function testArrayInterface() {
* Tests that unassigned fields with default schema values are auto-populated at access time.
*/
public function testSchemaValueInitialization() {
$doc = new Document(array('schema' => new Schema(array('fields' => array(
$doc = new Document(array('schema' => new DocumentSchema(array('fields' => array(
'foo' => array('type' => 'string', 'default' => 'bar')
)))));
$this->assertEmpty($doc->data());
Expand All @@ -877,7 +860,7 @@ public function testInitializationWithNestedFields() {

public function testWithArraySchemaReusedName() {
$model = $this->_model;
$schema = new Schema(array('fields' => array(
$schema = new DocumentSchema(array('fields' => array(
'_id' => array('type' => 'id'),
'bar' => array('array' => true),
'foo' => array('type' => 'object', 'array' => true),
Expand Down Expand Up @@ -922,17 +905,24 @@ public function testEnsureArrayExportFidelity() {

public function testHandlers() {
$model = $this->_model;
$schema = new Schema(array('fields' => array(
'_id' => array('type' => 'id'),
'date' => array('type' => 'date')
)));
$schema = new DocumentSchema(array(
'fields' => array(
'_id' => array('type' => 'id'),
'date' => array('type' => 'date')
),
'types' => array(
'date' => 'date'
),
'handlers' => array(
'date' => function($v) { return (object) $v; },
)
));
$handlers = array(
'MongoId' => function($value) { return substr((string) $value, -1); },
'MongoDate' => function($value) { return date('d/m/Y H:i', $value->sec); }
'stdClass' => function($value) { return date('d/m/Y H:i', strtotime($value->scalar)); }
);
$array = new Document(compact('model', 'schema', 'handlers') + array(
'data' => array(
'_id' => '4cb4ab6d7addf98506010002',
'_id' => '2',
'date' => '2013-06-06 13:00:00'
)
));
Expand Down
12 changes: 12 additions & 0 deletions tests/cases/data/source/mongo_db/ExporterTest.php
Expand Up @@ -453,6 +453,18 @@ public function testWithArraySchema() {
$result = Exporter::get('update', $doc->export());
$this->assertEqual($result['update'], $data);
}

public function testArrayConversion() {
$time = time();
$doc = new Document(array('data' => array(
'_id' => new MongoId(),
'date' => new MongoDate($time)
)));
$result = $doc->data();
$this->assertPattern('/^[a-f0-9]{24}$/', $result['_id']);
$this->assertEqual($time, $result['date']);
}

/**
* Allow basic type field to be replaced by a `Document` / `DocumentSet` type.
*/
Expand Down
8 changes: 4 additions & 4 deletions tests/cases/data/source/mongo_db/SchemaTest.php
Expand Up @@ -14,14 +14,14 @@

class SchemaTest extends \lithium\test\Unit {

public $db;
protected $_db;

public function skip() {
$this->skipIf(!MongoDb::enabled(), 'MongoDb is not enabled');
}

public function setUp() {
$this->db = new MongoDb(array('autoConnect' => false));
$this->_db = new MongoDb(array('autoConnect' => false));
}

public function testCastingIdArray() {
Expand All @@ -31,7 +31,7 @@ public function testCastingIdArray() {
)));

$result = $schema->cast(null, null, array('users' => new MongoId()), array(
'database' => $this->db
'database' => $this->_db
));

$this->assertEqual(array('users'), array_keys($result->data()));
Expand All @@ -44,7 +44,7 @@ public function testCastingEmptyValues() {
'_id' => array('type' => 'id'),
'foo' => array('type' => 'string', 'array' => true)
)));
$result = $schema->cast(null, null, null, array('database' => $this->db));
$result = $schema->cast(null, null, null, array('database' => $this->_db));
}
}

Expand Down

0 comments on commit 6fba0e0

Please sign in to comment.