Skip to content

Commit

Permalink
Simple getSchema function from IsotopeOrmModelSchema
Browse files Browse the repository at this point in the history
  • Loading branch information
isofarro committed Apr 10, 2010
1 parent b0714af commit 97d7de3
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 13 deletions.
14 changes: 10 additions & 4 deletions IsotopeOrm.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

require_once dirname(__FILE__) . '/IsotopeOrmSchema.php';
require_once dirname(__FILE__) . '/IsotopeOrmModelSchema.php';

class IsotopeOrm {
Expand Down Expand Up @@ -43,10 +44,15 @@ public function setConfig($config) {
@returns a new instance of IsotopeOrmModelSchema
**/
public function createModelSchema($modelName, $schema=false) {
// TODO: Check the modelName doesn't exist first.
$modelSchema = new IsotopeOrmModelSchema($modelName);
// TODO: If the schema exists, process it
return $modelSchema;
if (empty($this->_dbScheme[$modelName])) {
$modelSchema = new IsotopeOrmModelSchema($modelName);
// TODO: If the schema exists, process it

$this->_dbScheme[$modelName] = $modelSchema;
return $modelSchema;
} else {
throw new InvalidArgumentException('IsotopeOrm Model already exists');
}
}


Expand Down
14 changes: 10 additions & 4 deletions IsotopeOrmModelSchema.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
<?php

class IsotopeOrmModelSchema {
var $name;
var $schema = array();
// The schema for this model
var $schema = NULL;

public function __construct($name) {
$this->name = $name;
$this->schema = (object) array(
'model' => $name,
'fields' => array(
Expand Down Expand Up @@ -34,7 +33,14 @@ public function addField($name, $definition=false) {
return false;
}


/**
getSchema - returns the schema definition of the current Model
@returns the model schema data object
**/
public function getSchema() {
return $this->schema;
}
}

?>
30 changes: 25 additions & 5 deletions tests/IsotopeOrmModelSchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ function testInitIsotopeOrm() {
$this->assertTrue(is_a($class, 'IsotopeOrmModelSchema'));
}

function testModelSchemaName() {
$this->assertEquals($this->schema->name, $this->modelName);
}

function testAddDefaultField() {
$ret = $this->schema->addField('field1');
$this->assertTrue($ret);
Expand All @@ -34,7 +30,7 @@ function testAddDefaultField() {
function testAddPredefinedField() {
$ret = $this->schema->addField('description', IsotopeOrmSchema::TEXTAREA);
$this->assertTrue($ret);
print_r($this->schema->schema);
//print_r($this->schema->schema);
}

function testAddDuplicateDefaultField() {
Expand All @@ -44,6 +40,30 @@ function testAddDuplicateDefaultField() {
$this->assertFalse($ret);
}

function testEmptyGetSchema() {
$schema = $this->schema->getSchema();

$this->assertNotNull($schema);
//print_r($schema);

$this->assertEquals($this->modelName, $schema->model);
$this->assertTrue(is_array($schema->fields));
$this->assertTrue(is_array($schema->indexes));
$this->assertTrue(is_array($schema->subtables));


$this->assertEquals(1, count($schema->fields));
$this->assertEquals(0, count($schema->indexes));
$this->assertEquals(0, count($schema->subtables));

$this->assertFalse(empty($schema->fields['_id']));
$definition = $schema->fields['_id'];
$this->assertNotNull($definition);
$this->assertTrue(is_string($definition));
$this->assertEquals('PRIMARY_KEY', $definition);
}


}

?>
9 changes: 9 additions & 0 deletions tests/IsotopeOrmTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ public function testProceduralCreateModelSchema() {
$this->assertTrue(is_a($model, 'IsotopeOrmModelSchema'));
}

public function testDuplicateCreateModelSchema() {
$model = $this->orm->createModelSchema('testSchema');
$this->assertNotNull($model);
$this->assertTrue(is_a($model, 'IsotopeOrmModelSchema'));

$this->setExpectedException('InvalidArgumentException');
$model = $this->orm->createModelSchema('testSchema');
}

public function testCreateExistingModelSchema() {

}
Expand Down

0 comments on commit 97d7de3

Please sign in to comment.