Skip to content

Commit

Permalink
Starting to flesh out the IsotopeOrmModel and applying it's IsotopeOr…
Browse files Browse the repository at this point in the history
…mModelSchema to it
  • Loading branch information
isofarro committed Apr 10, 2010
1 parent 4f1833d commit 8e96215
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 11 deletions.
54 changes: 50 additions & 4 deletions IsotopeOrm.php
@@ -1,6 +1,7 @@
<?php

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

class IsotopeOrm {
Expand All @@ -11,7 +12,10 @@ class IsotopeOrm {
private $_db = NULL;

// Database schema
private $_dbSchema = NULL;
private $_dbSchema = array();

// Database models
private $_dbModels = array();


public function __construct($config=false) {
Expand All @@ -35,7 +39,8 @@ public function setConfig($config) {
}

/**
createModelSchema - creates and returns a new ModelSchema object.
createModelSchema - creates and returns a new ModelSchema object.
This method caches ModelSchemas.
Throws an exception if the ModelSchema already exists
@param $modelName - a model name (must not currently exist)
Expand All @@ -44,17 +49,58 @@ public function setConfig($config) {
@returns a new instance of IsotopeOrmModelSchema
**/
public function createModelSchema($modelName, $schema=false) {
if (empty($this->_dbScheme[$modelName])) {
if (empty($this->_dbSchema[$modelName])) {
$modelSchema = new IsotopeOrmModelSchema($modelName);
// TODO: If the schema exists, process it

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

/**
getModelSchema - get the Schema for the specified Model.
This method caches ModelSchemas.
@param $modelName - the name of the model
@returns an IsotopeOrmModelSchema if the model exists, or false if it doesn't
**/
public function getModelSchema($modelName) {
if (empty($this->_dbSchema[$modelName])) {
// TODO: Get the model schema from the datasource
} else {
return $this->_dbSchema[$modelName];
}
return false;
}


/**
getModel - get the requested model.
This method caches Models
@param $modelName - the name of the model
@returns an IsotopeOrmModel if the model exists, or false if it doesn't
**/
public function getModel($modelName) {
if (!empty($this->_dbModels[$modelName])) {
return $this->_dbModels[$modelName];
} else {
$model = new IsotopeOrmModel($modelName);
// TODO: Apply the known schema, if one is present
$schema = $this->getModelSchema($modelName);
if ($schema) {
$model->setSchema($schema);
}
$this->_dbModels[$modelName] = $model;
return $model;
}
return false;
}

}

Expand Down
7 changes: 7 additions & 0 deletions IsotopeOrmModel.php
@@ -0,0 +1,7 @@
<?php

class IsotopeOrmModel {

}

?>
16 changes: 16 additions & 0 deletions tests/IsotopeOrmModelTest.php
@@ -0,0 +1,16 @@
<?php

require_once dirname(dirname(__FILE__)) . '/IsotopeOrmModel.php';

class IsotopeOrmModelTest extends PHPUnit_Framework_TestCase {

function testInitIsotopeOrmModel() {
$this->assertTrue(class_exists('IsotopeOrmModel'));

$class = new IsotopeOrmModel();
$this->assertTrue(is_a($class, 'IsotopeOrmModel'));
}

}

?>
26 changes: 19 additions & 7 deletions tests/IsotopeOrmTest.php
Expand Up @@ -45,18 +45,30 @@ public function testIsotopeOrmSetConfig() {


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

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

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

public function testUnknownGetSchema() {
$schema = $this->orm->getModelSchema('unknownModel');
$this->assertFalse($schema);
}

public function testGetModel() {
$model = $this->orm->getModel('testModel');
$this->assertNotNull($model);
$this->assertTrue(is_a($model, 'IsotopeOrmModel'));

}

}
Expand Down

0 comments on commit 8e96215

Please sign in to comment.