Permalink
Browse files

Added Unit tests + fixed bugs, moved columfamily into ColumnContainer…

… (ColumnFamily + ColumnFamilySuper extend ColumnContainer)
  • Loading branch information...
1 parent b1a6fec commit 33a065f20ceeb67419542b096812739296210f0e @mjpearson committed Jan 16, 2010
View
9 config.php
@@ -15,7 +15,7 @@
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
-The pandra homepage is :
+The Pandra homepage is :
http://www.phpgrease.net/projects/pandra
*/
/**
@@ -30,6 +30,9 @@
require_once $GLOBALS['THRIFT_ROOT'].'/transport/TFramedTransport.php';
require_once $GLOBALS['THRIFT_ROOT'].'/transport/TBufferedTransport.php';
+// Config xml path for Cassandra
+define('CASSANDRA_CONF_PATH', '/usr/local/src/apache-cassandra-incubating-0.4.0/conf/storage-conf.xml');
+
// Default Thrift port
define('PANDRA_PORT_DEFAULT', 9160);
@@ -39,6 +42,10 @@
define('PANDRA_MODE_ROUND_APC', 1); // sequentially select between interpreter instances w/APC
define('PANDRA_MODE_RANDOM', 2); // select random node
+// Column Family Types
+define('PANDRA_CF_STANDARD', 0);
+define('PANDRA_CF_SUPER', 1);
+
function pandraAutoLoad($className) {
if (!preg_match("/^pandra/i", $className)) return;
View
214 lib/Column.class.php
@@ -4,109 +4,153 @@
* @package Pandra
*/
class PandraColumn {
+ /* @var string column name */
+ public $name = NULL;
- /* @var string column name */
- public $name = NULL;
+ /* @var string column value */
+ private $_value = NULL;
- /* @var string column value */
- public $value = NULL;
+ /* @var int last changed timestamp */
+ public $timestamp = NULL;
- /* @var int last changed timestamp */
- public $timestamp = NULL;
+ /* @var array validator type definitions for this colun */
+ public $typeDef = array();
- /* @var array validator type definitions for this colun */
- public $typeDef = array();
+ /* @var string last processing error */
+ public $lastError = array();
- /* @var string last processing error */
- public $lastError = array();
+ /* @var string callback function for this column pre-save */
+ public $callback = NULL;
- /* @var string callback function for this column pre-save */
- public $callback = NULL;
+ /* @var bool column value has been modified since load() or init */
+ private $_modified = FALSE;
- /* @var bool column value has been modified since load() or init */
- private $_modified = FALSE;
+ /* @var $delete column is marked for deletion */
+ private $_delete = FALSE;
- /* @var $delete column is marked for deletion */
- private $_delete = FALSE;
+ /* @var PandraColumnFamily column family parent reference */
+ private $_parentCF = NULL;
- /* @var PandraColumnFamily column family parent reference */
- private $_parentCF = NULL;
+ public function __construct($name, $parentCF, $typeDef = array()) {
+ //parent::__construct(array('name' => $name));
+ $this->_parentCF = $parentCF;
+ $this->typeDef = $typeDef;
+ }
- public function __construct($name, &$parentCF) {
- $this->name = $name;
- $this->_parentCF = $parentCF;
- }
+ public function bindTime($timeOverride = NULL) {
+ $this->timestamp = ($timeOverride === NULL) ? time() : $timeOverride;
+ return $this->timestamp;
+ }
- public function bindTime($timeOverride = NULL) {
- $this->timestamp = ($timeOverride === NULL) ? time() : $timeOverride;
- return $timestamp;
- }
-
- public function setValue($value, $validate = TRUE) {
- if ($validate && !empty($this->typeDef)) {
- if (!PandraValidator::check($value, $this->name, $this->typeDef, $this->lastError)) {
- $this->_parentCF->errors[] = $this->lastError[0];
- return FALSE;
- }
+ public function setvalue($value, $validate = TRUE) {
+ if ($validate && !empty($this->typeDef)) {
+ if (!PandraValidator::check($value, $this->name, $this->typeDef, $this->lastError)) {
+ $this->_parentCF->errors[] = $this->lastError[0];
+ return FALSE;
}
-
- $this->value = $value;
- $this->_modified = TRUE;
- return TRUE;
}
- public function reset() {
- $this->_modified = FALSE;
- $this->_delete = FALSE;
- }
-
- public function markDelete() {
- $this->_delete = TRUE;
- $this->_modified = TRUE;
- }
-
- public function callbackValue() {
- $value = '';
- eval('$value = '.$cObj->callback.'("'.$cObj->value.'");');
- return $value;
+ $this->_value = $value;
+ $this->_modified = TRUE;
+ return TRUE;
+ }
+
+ /**
+ * Magic getter
+ * @param string $colName field name to get
+ * @return string value
+ */
+ public function __get($colName) {
+ if ($colName == 'value') {
+ return $this->_value;
}
+ return NULL;
+ }
+
+ /**
+ * Magic setter
+ * @todo propogate an exception for setcolumn if it returns false. magic __set's are void return type
+ * @param string $colName field name to set
+ * @param string $value value to set for field
+ * @return bool field set ok
+ */
+ public function __set($colName, $value) {
+ if ($colName == 'value') {
+ return $this->setvalue($value, $this->_validator);
+ }
+ return NULL;
+ }
+
+ public function reset() {
+ $this->_modified = FALSE;
+ $this->_delete = FALSE;
+ }
+
+ public function markDelete() {
+ $this->_delete = TRUE;
+ $this->_modified = TRUE;
+ }
+
+ public function callbackvalue() {
+ $value = '';
+ eval('$value = '.$this->callback.'("'.$this->_value.'");');
+ return $value;
+ }
+
+ public function cast($object, $parentCF = NULL) {
+ if (get_class($object) == 'cassandra_Column') {
+ $newObj = new PandraColumn($object->name, ($parentCF === NULL) ? $this->_parentCF : $parentCF);
+ $newObj->value = $object->value;
+ $newObj->timestamp = $object->timestamp;
+ return $newObj;
+ }
+ return NULL;
+ }
- public function cast(PandraColumn $object) {
- return $object;
- }
-
- public function save() {
-
- if (!$this->_modified) return FALSE;
-
- // @todo configurable consistency
- $consistencyLevel = cassandra_ConsistencyLevel::ONE;
-
- $this->_parentCF->checkCFState();
+ public function save(cassandra_ColumnPath $columnPath = NULL, $consistencyLevel = cassandra_ConsistencyLevel::ONE) {
- $client = Pandra::getClient(TRUE);
+ //if (!$this->_modified) return FALSE;
+ $this->_parentCF->checkCFState();
- // build the column path
- $columnPath = new cassandra_ColumnPath();
- $columnPath->column_family = $this->_parentCF->columnFamily;
- $columnPath->column = $this->name;
+ $client = Pandra::getClient(TRUE);
- // @todo super writes
- if ($this->_parentSuper !== NULL) {
-// $columnPath->super_column = $this->_parentSuper->name;
- }
+ // build the column path
+ if ($columnPath == NULL) {
+ $columnPath = new cassandra_ColumnPath();
+ }
+ $columnPath->column_family = $this->_parentCF->name;
+ $columnPath->column = $this->name;
- if ($this->_delete) {
- $client->remove($this->_parentCF->keySpace, $this->_parentCF->keyID, $columnPath, $this->bindTime(), $consistencyLevel);
- } else {
- $client->insert($this->_parentCF->keySpace, $this->_parentCF->keyID, $columnPath, $this->value, $this->bindTime(), $consistencyLevel);
+ // @todo super writes
+ /*
+ if ($this->_parentCF !== NULL && $this->_parentCF instanceof PandraSuperColumn) {
+ $columnPath->super_column = $this->_parentCF->superColumn;
}
-
- return TRUE;
- }
-
- public function delete() {
- $this->markDelete();
- $this->save();
- }
-}
+ */
+ try {
+ if ($this->_delete) {
+ $client->remove($this->_parentCF->keySpace, $this->_parentCF->keyID, $columnPath, $this->bindTime(), $consistencyLevel);
+ } else {
+ $client->insert($this->_parentCF->keySpace, $this->_parentCF->keyID, $columnPath, $this->_value, $this->bindTime(), $consistencyLevel);
+ }
+ } catch (TException $te) {
+ array_push($this->lastError, $te->getMessage());
+ $this->_parentCF->errors[] = $this->lastError[0];
+ return FALSE;
+ }
+ return TRUE;
+ }
+
+ public function delete() {
+ $this->markDelete();
+ //$this->save();
+ }
+
+ public function isDeleted() {
+ return $this->_delete;
+ }
+
+ public function isModified() {
+ return $this->_modified;
+ }
+}
View
174 lib/ColumnContainer.class.php
@@ -0,0 +1,174 @@
+<?php
+
+abstract class PandraColumnContainer {
+
+ /* @var this column families name (table name) */
+ public $name = NULL;
+
+ /* @var mixed keyID key for the working row */
+ public $keyID = NULL;
+
+ /* @var string magic set/get prefixes for Columns */
+ const _columnNamePrefix = 'column_'; // magic __get/__set column prefix in column famliy
+
+ /* @var string magic get/set prefix for Super Columns */
+ const _superNamePrefix = 'super_';
+
+ /* @var array container for column objects, indexed to field name */
+ protected $columns = array();
+
+ /* @var array complete list of errors for this object instance */
+ public $errors = array();
+
+ /* var bool columnfamily marked for deletion */
+ private $_delete = FALSE;
+
+ /**
+ * Constructor, builds column structures
+ */
+ public function __construct($keyID = NULL) {
+ $this->constructColumns();
+ if ($keyID !== NULL) $this->load($keyID);
+ }
+
+ /**
+ * constructFields builds column objects via addColumn/addSuper methods for defined schemas
+ */
+ public function constructColumns() {}
+
+ public function lastError() {
+ if (!empty($this->errors)) {
+ return $this->errors[0];
+ }
+ return NULL;
+ }
+
+ public function addColumn($colName, $typeDef = array(), $callbackOnSave = NULL) {
+ if (!array_key_exists($colName, $this->columns)) {
+ $this->columns[$colName] = new PandraColumn($colName, $this, $typeDef);
+ }
+
+ // pre-save callback
+ if (!empty($callbackOnSave)) $this->columns[$colName]->callback = $callbackOnSave;
+
+ return $this->getColumn($colName);
+ }
+
+ public function getColumn($colName) {
+ if (array_key_exists($colName, $this->columns)) {
+ return $this->columns[$colName];
+ }
+ return NULL;
+ }
+
+ /**
+ * Sets a columns value for this slice
+ * @param string $colName Column name to set
+ * @param string $value New value for column
+ * @param bool $validate opt validate from typeDef (default TRUE)
+ * @return bool column set ok
+ */
+ public function setColumn($colName, $value, $validate = TRUE) {
+ return (array_key_exists($colName, $this->columns) && $this->columns[$colName]->setValue($value, $validate));
+ }
+
+ public function listColumns() {
+ return array_keys($this->columns);
+ }
+
+ /**
+ * Gets complete slice of Thrift cassandra_Column objects for keyID
+ *
+ * @return array cassandra_Column objects
+ */
+ public function getRawSlice($keyID, $consistencyLevel = cassandra_ConsistencyLevel::ONE) {
+ $this->keyID = $keyID;
+
+ $this->_parentCF->checkCFState();
+
+ $client = Pandra::getClient();
+
+ // build the column path
+ $columnParent = new cassandra_ColumnParent();
+ $columnParent->column_family = $this->name;
+ //$columnParent->super_column = $this->superColumn;
+ $columnParent->super_column = null;
+
+ $predicate = new cassandra_SlicePredicate();
+ $predicate->slice_range = new cassandra_SliceRange();
+ $predicate->slice_range->start = '';
+ $predicate->slice_range->finish = '';
+
+ return $client->get_slice($this->keySpace, $keyID, $columnParent, $predicate, $consistencyLevel);
+ }
+
+ /**
+ * Resets deletion states for the column family
+ */
+ public function reset() {
+ // undo any deletion marks
+ $this->_delete = FALSE;
+ foreach ($this->columns as &$cObj) {
+ $cObj->reset();
+ }
+ }
+
+ /*
+ * Populates object from $data array. Bool false on validation error error, check $this->errors for messages
+ * @param array key/value pair of column => value
+ * @return bool populated without validation errors
+ */
+ public function populate($data) {
+ if (is_string($data)) {
+ $data = json_decode($data, TRUE);
+ }
+
+ if (is_array($data)) {
+ foreach ($data as $key => $value) {
+ if (array_key_exists($key, $this->columns)) {
+ $this->columns[$key]->setValue($value);
+ }
+ }
+ } else {
+ return FALSE;
+ }
+
+ return empty($this->errors);
+ }
+
+ /**
+ * determine if get/set field exists/is mutable, strips field prefix from magic get/setters
+ * @param string $colName field name to check
+ * @return bool field exists
+ */
+ private function gsMutable(&$colName) {
+ $colName = preg_replace("/^".self::_columnNamePrefix."/", "", strtolower($colName));
+ return array_key_exists($colName, $this->columns);
+ }
+
+ /**
+ * Magic getter
+ * @param string $colName field name to get
+ * @return string value
+ */
+ protected function __get($colName) {
+ if ($this->gsMutable($colName)) {
+ return $this->columns[$colName]->value;
+ } else {
+ return NULL;
+ }
+ }
+
+ /**
+ * Magic setter
+ * @todo propogate an exception for setcolumn if it returns false. magic __set's are void return type
+ * @param string $colName field name to set
+ * @param string $value value to set for field
+ * @return bool field set ok
+ */
+ protected function __set($colName, $value) {
+ if ($this->gsMutable($colName)) {
+ $this->setColumn($colName, $value);
+ }
+ }
+}
View
202 lib/ColumnFamily.class.php
@@ -1,96 +1,59 @@
<?php
/**
- *
+ * ColumnFamily container class
* @package Pandra
* @abstract
*/
-abstract class PandraColumnFamily extends cassandra_ColumnParent {
+abstract class PandraColumnFamily extends PandraColumnContainer {
- /* @var string keyspace for this column family */
+ /* @var string keyspace (or database) for this column family */
public $keySpace = NULL;
- /* @var string child table name */
- public $columnFamily = NULL;
-
- /* @var string super column name for this columnfamily (supers may encapsulate columns and column families) */
- public $superColumn = NULL;
+ /* @var this column families name (table name) */
+ public $name = NULL;
/* @var mixed keyID key for the working row */
public $keyID = NULL;
- /* @var string magic set/get prefixes */
- private $_cFieldPrefix = 'column_'; // magic __get/__set column prefix in column famliy
-
- /* @var string magic set/get prefixes */
- private $_sFieldPrefix = 'super_'; // magic __get/__set super prefix in column family
-
- /* @var array container for column objects, indexed to field name */
- protected $columns = array();
-
- /* @var array container for supers (column container objects), indexed to supercolumn name */
- protected $supers = array();
+ /* @var int column family type (standard or super) */
+ private $_type = PANDRA_CF_STANDARD;
- /* @var string last error encountered */
- //public $lastError = NULL;
-
- /* @var array complete list of errors for this object instance */
- public $errors = array();
-
- /* var bool columnfamily marked for deletion */
+ /* var bool columnfamily marked for deletion */
private $_delete = FALSE;
+ private $_loaded = FALSE;
+
/**
* Constructor, builds column structures
*/
- public function __construct($keyID = NULL) {
+ public function ___construct($keyID = NULL) {
$this->constructColumns();
if ($keyID !== NULL) $this->load($keyID);
}
- public function lastError() {
- if (!empty($this->errors)) {
- return $this->errors[0];
- }
- return NULL;
- }
-
- public function addColumn($colName, $typeDef = array(), $callbackOnSave = NULL) {
- if (!array_key_exists($colName, $this->columns)) {
- $this->columns[$colName] = new PandraColumn($colName, $this);
- }
-
- // array of validation functions
- if (!empty($typeDef)) $this->columns[$colName]->typeDef = $typeDef;
-
- // pre-save callback
- if (!empty($callbackOnSave)) $this->columns[$colName]->callback = $callbackOnSave;
-
- return $this->getColumn($colName);
- }
-
- public function getColumn($colName) {
- if (array_key_exists($colName, $this->columns)) {
- return $this->columns[$colName];
+ /**
+ * deletes the loaded object from the keyspace, or optionally the supplied columns for the key
+ */
+ public function markDelete() {
+ $this->_delete = TRUE;
+ foreach ($this->columns as &$column) {
+ $column->delete();
}
- return NULL;
}
- public function listColumns() {
- return array_keys($this->columns);
+ public function delete() {
+ $this->markDelete();
}
- public function addSuper($superName) {
- if (!array_key_exists($superName, $this->supers)) {
- $this->supers[$superName] = new PandraSuperColumn($superName, $this);
+ public function reset() {
+ $this->_delete = FALSE;
+ foreach ($this->columns as &$column) {
+ $column->reset();
}
- return $this->getSuper($superName);
}
- public function getSuper($superName) {
- if (array_key_exists($superName, $this->supers)) {
- return $this->supers[$superName];
- }
- return NULL;
+ public function isDeleted() {
+ return $this->_delete;
}
/**
@@ -107,8 +70,9 @@ public function getRawSlice($keyID, $consistencyLevel = cassandra_ConsistencyLev
// build the column path
$columnParent = new cassandra_ColumnParent();
- $columnParent->column_family = $this->columnFamily;
- $columnParent->super_column = $this->superColumn;
+ $columnParent->column_family = $this->name;
+ //$columnParent->super_column = $this->superColumn;
+ $columnParent->super_column = null;
$predicate = new cassandra_SlicePredicate();
$predicate->slice_range = new cassandra_SliceRange();
@@ -125,16 +89,18 @@ public function getRawSlice($keyID, $consistencyLevel = cassandra_ConsistencyLev
* @return bool this object has loaded its fields
*/
public function load($keyID, $colAutoCreate = FALSE, $consistencyLevel = cassandra_ConsistencyLevel::ONE) {
+ $this->_loaded = FALSE;
+
$result = $this->getRawSlice($keyID, $consistencyLevel);
if (!empty($result)) {
foreach ($result as $cObj) {
// populate self, skip validators - self trusted
if ($colAutoCreate && !array_key_exists($cObj->column->name, $this->columns)) $this->addColumn($cObj->column->name);
$this->columns[$cObj->column->name]->value = $cObj->column->value;
}
- return TRUE;
+ $this->_loaded = TRUE;
}
- return FALSE;
+ return $this->_loaded;
}
/**
@@ -149,14 +115,14 @@ public function checkCFState() {
if ($this->keySpace === NULL) throw new RuntimeException('NULL keySpace defined, cannot insert');
// check a column family is defined
- if ($this->columnFamily === NULL) throw new RuntimeException('NULL columnFamliy defined, cannot insert');
+ if ($this->name === NULL) throw new RuntimeException('NULL name defined, cannot insert');
}
/**
* Save all columns in this loaded columnfamily
* @return void
*/
- public function save() {
+ public function save(cassandra_ColumnPath $columnPath = NULL, $consistencyLevel = cassandra_ConsistencyLevel::ONE) {
$this->checkCFState();
@@ -165,103 +131,25 @@ public function save() {
if ($columnPath === NULL) {
$columnPath = new cassandra_ColumnPath();
- $columnPath->column_family = $this->columnFamily;
+ $columnPath->column_family = $this->name;
}
-
+
+ // Delete this column family
$client->remove($this->keySpace, $this->keyID, $columnPath, time(), $consistencyLevel);
}
foreach ($this->columns as &$cObj) {
- $cObj->save();
+ if (!$cObj->save()) return FALSE;
}
+ return TRUE;
}
- /**
- * deletes the loaded object from the keyspace, or optionally the supplied columns for the key
- */
- public function markDelete() {
- $this->_delete = TRUE;
- return;
+ public function getType() {
+ return $this->_type;
}
- public function unDelete() {
- $this->_delete = FALSE;
- return;
- }
-
- public function reset() {
- foreach ($this->columns as &$cObj) {
- $cObj->reset();
- }
- }
-
- /*
- * Populates object from $data array. Bool false on validation error error, check $this->errors for messages
- * @param array key/value pair of column => value
- * @return bool populated without validation errors
- */
- public function populate($data) {
- // $errors = NULL;
- if (is_array($data)) {
- foreach ($data as $key => $value) {
- if (array_key_exists($key, $this->columns)) {
- $this->columns[$key]->setValue($value);
- }
- }
- }
- // return empty($errors);
- return empty($this->errors);
- }
-
- /**
- * determine if get/set field exists/is mutable, strips field prefix from magic get/setters
- * @param string $colName field name to check
- * @return bool field exists
- */
- private function gsMutable(&$colName) {
- $colName = preg_replace("/^".$this->_cFieldPrefix."/", "", strtolower($colName));
- return array_key_exists($colName, $this->columns);
- }
-
- /**
- * Magic getter
- * @param string $colName field name to get
- * @return string value
- */
- protected function __get($colName) {
- if ($this->gsMutable($colName)) {
- return $this->columns[$colName]->value;
- } else {
- return NULL;
- }
- }
-
- /**
- * Magic setter
- * @todo propogate an exception for setcolumn if it returns false. magic __set's are void return type
- * @param string $colName field name to set
- * @param string $value value to set for field
- * @return bool field set ok
- */
- protected function __set($colName, $value) {
- if ($this->gsMutable($colName)) {
- $this->setColumn($colName, $value);
- }
+ public function isLoaded() {
+ return $this->_loaded;
}
-
- /**
- * Sets a columns value for this slice
- * @param string $colName Column name to set
- * @param string $value New value for column
- * @param bool $validate opt validate from typeDef (default TRUE)
- * @return bool column set ok
- */
- public function setColumn($colName, $value, $validate = TRUE) {
- return (array_key_exists($colName, $this->columns) && $this->columns[$colName]->setValue($value, $validate));
- }
-
- /**
- * constructFields builds column objects via addColumn/addSuper methods
- */
- abstract public function constructColumns();
}
+?>
View
18 lib/ColumnFamilySuper.class.php
@@ -0,0 +1,18 @@
+<?php
+
+abstract class PandraColumnFamilySuper extends PandraColumnFamily {
+
+ /* @var int column family type (standard or super) */
+ private $_type = PANDRA_CF_SUPER;
+
+ public function addSuper(PandraSuperColumn $scObj) {
+ $superName = $scObj->name;
+
+ $scObj->setParentCF($this);
+ $this->columns[$superName] = $scObj;
+
+ return $this->getColumn($superName);
+ }
+
+
+}
View
231 lib/Pandra.class.php
@@ -4,69 +4,87 @@
*/
class Pandra {
- const FORMAT_OBJ = 1;
- const FORMAT_ASSOC = 2;
- const FORMAT_XML = 3;
- const FORMAT_JSON = 4;
+ const FORMAT_OBJ = 1;
+ const FORMAT_ASSOC = 2;
+ const FORMAT_XML = 3;
+ const FORMAT_JSON = 4;
- //const APC_EXPIRE_SECONDS = 60;
+ //const APC_EXPIRE_SECONDS = 60;
- static public $lastError = '';
- static public $consistencyLevel = cassandra_ConsistencyLevel::ZERO;
+ static public $lastError = '';
+ static public $consistencyLevel = cassandra_ConsistencyLevel::ZERO;
- static private $_nodeConns = array();
- static private $_activeNode = NULL;
+ static private $_nodeConns = array();
+ static private $_activeNode = NULL;
- static private $readMode = PANDRA_MODE_ACTIVE;
- static private $writeMode = PANDRA_MODE_ACTIVE;
+ static private $readMode = PANDRA_MODE_ACTIVE;
+ static private $writeMode = PANDRA_MODE_ACTIVE;
- static private $supportedModes = array(
- PANDRA_MODE_ACTIVE,
- PANDRA_MODE_ROUND,
- //PANDRA_MODE_ROUND_APC,
- PANDRA_MODE_RANDOM,
- );
+ static private $_supportedModes = array(
+ PANDRA_MODE_ACTIVE,
+ PANDRA_MODE_ROUND,
+ //PANDRA_MODE_ROUND_APC,
+ PANDRA_MODE_RANDOM,
+ );
- static public function setReadMode($newMode) {
- if (!array_key_exists($newMode, self::$supportedModes)) throw new RuntimeExcpetion("Unsupported Read Mode");
- self::$readMode = $newMode;
- }
+ static public function getSupportedModes() {
+ return self::$_supportedModes;
+ }
+
+ static public function setReadMode($newMode) {
+ if (!array_key_exists($newMode, self::$_supportedModes)) throw new RuntimeExcpetion("Unsupported Read Mode");
+ self::$readMode = $newMode;
+ }
+
+ static public function getReadMode() {
+ return self::$readMode;
+ }
- static public function setWriteMode($newMode) {
- if (!array_key_exists($newMode, self::$supportedModes)) throw new RuntimeExcpetion("Unsupported Write Mode");
- self::$writeMode = $newMode;
- }
+ static public function setWriteMode($newMode) {
+ if (!array_key_exists($newMode, self::$_supportedModes)) throw new RuntimeExcpetion("Unsupported Write Mode");
+ self::$writeMode = $newMode;
+ }
+
+ static public function getWriteMode() {
+ return self::$writeMode;
+ }
/**
- *
+ *
*/
- public function setActiveNode($connectionID) {
- if (array_key_exists($connectionID, self::$_nodeConns) && self::$_nodeConns[$connectionID]['transport']->isOpen()) {
- self::$_activeNode = $connectionID;
- return TRUE;
- }
- return FALSE;
- }
+ static public function setActiveNode($connectionID) {
+ if (array_key_exists($connectionID, self::$_nodeConns) && self::$_nodeConns[$connectionID]['transport']->isOpen()) {
+ self::$_activeNode = $connectionID;
+ return TRUE;
+ }
+ return FALSE;
+ }
/**
- *
+ *
*/
static public function disconnect($connectionID) {
if (array_key_exists($connectionID, self::$_nodeConns)) {
if (self::$_nodeConns[$connectionID]['transport']->isOpen()) {
self::$_nodeConns[$connectionID]['transport']->close();
+ return TRUE;
}
}
+ return FALSE;
}
/**
- *
+ *
*/
static public function disconnectAll() {
- while ($connectionID = each(self::$_nodeConns)) {
- self::disconnect($connectionID);
- }
+
+ $connections = array_keys(self::$_nodeConns);
+
+ foreach ($connections as $connectionID) {
+ if (!self::disconnect($connectionID)) throw new RuntimeException($connectionID.' could not be closed');
+ }
+ return TRUE;
}
/**
@@ -76,75 +94,104 @@ static public function disconnectAll() {
* @param int $port TCP port of connecting node
* @return bool connected ok
*/
- static public function connect($connectionID, $host = NULL, $port = PANDRA_PORT_DEFAULT) {
- try {
- // if the connection exists but it is closed, then re-open
- if (array_key_exists($connectionID, self::$_nodeConns)) {
- if (!self::$_nodeConns[$connectionID]['transport']->isOpen()) {
- self::$_nodeConns[$connectionID]['transport']->open();
- }
- return TRUE;
- }
-
- // Create Thrift transport and binary protocol cassandra client
- $transport = new TBufferedTransport(new TSocket($host, $port), 1024, 1024);
- $transport->open();
-
- self::$_nodeConns[$connectionID] = array(
- 'transport' => $transport,
- 'client' => new CassandraClient(new TBinaryProtocol($transport))
- );
-
- // set new connection the active, working master
- self::setActiveNode($connectionID);
- return TRUE;
- } catch (TException $tx) {
- self::$lastError = 'TException: '.$tx->getMessage() . "\n";
- return FALSE;
- }
- }
+ static public function connect($connectionID, $host = NULL, $port = PANDRA_PORT_DEFAULT) {
+ try {
+ // if the connection exists but it is closed, then re-open
+ if (array_key_exists($connectionID, self::$_nodeConns)) {
+ if (!self::$_nodeConns[$connectionID]['transport']->isOpen()) {
+ self::$_nodeConns[$connectionID]['transport']->open();
+ }
+ return TRUE;
+ }
+
+ // Create Thrift transport and binary protocol cassandra client
+ $transport = new TBufferedTransport(new TSocket($host, $port), 1024, 1024);
+ $transport->open();
+
+ self::$_nodeConns[$connectionID] = array(
+ 'transport' => $transport,
+ 'client' => new CassandraClient(new TBinaryProtocol($transport))
+ );
+
+ // set new connection the active, working master
+ self::setActiveNode($connectionID);
+ return TRUE;
+ } catch (TException $tx) {
+ self::$lastError = 'TException: '.$tx->getMessage() . "\n";
+ }
+ return FALSE;
+
+ }
/**
* get current working node, recursive, trims disconnected clients
*/
static public function getClient($writeMode = FALSE) {
if (empty(self::$_activeNode)) throw new Exception('Not Connected');
- $useMode = ($writeMode) ? self::$writeMode : self::$readMode;
- switch ($useMode) {
- case PANDRA_MODE_ROUND_APC :
- // @todo, APC store of activeNode
- case PANDRA_MODE_ROUND :
- if (!current(self::$_nodeConns)) reset(self::$_nodeConns);
- $curConn = each(self::$_nodeConns);
- self::$_activeNode = $curConn['key']; // store current working node
- return self::$_nodeConns[self::$_activeNode]['client'];
- break;
- case PANDRA_MODE_RANDOM :
- $randConn =& array_rand(self::$_nodeConns);
- return $randConn['client'];
- break;
- case PANDRA_MODE_ACTIVE :
- default :
- return self::$_nodeConns[self::$_activeNode]['client'];
- break;
- }
+ $useMode = ($writeMode) ? self::$writeMode : self::$readMode;
+ switch ($useMode) {
+ case PANDRA_MODE_ROUND_APC :
+ // @todo, APC store of activeNode
+ case PANDRA_MODE_ROUND :
+ if (!current(self::$_nodeConns)) reset(self::$_nodeConns);
+ $curConn = each(self::$_nodeConns);
+ self::$_activeNode = $curConn['key']; // store current working node
+ return self::$_nodeConns[self::$_activeNode]['client'];
+ break;
+ case PANDRA_MODE_RANDOM :
+ $randConn =& array_rand(self::$_nodeConns);
+ return self::$_nodeConns[$randConn]['client'];
+ break;
+ case PANDRA_MODE_ACTIVE :
+ default :
+ return self::$_nodeConns[self::$_activeNode]['client'];
+ break;
+ }
}
static public function getKeyspace($keySpace) {
$client = self::getClient();
return $client->describe_keyspace($keySpace);
}
- static public function toJSON(&$results) {
- }
+ static public function loadConfigXML() {
+ if (!file_exists(CASSANDRA_CONF_PATH)) {
+ throw new RuntimeException('Cannot build models, file not found ('.CASSANDRA_CONF_PATH.')\n');
+ }
+
+ $conf = simplexml_load_file(CASSANDRA_CONF_PATH);
+ return $conf;
+ }
+
+ /*
+ static public function buildModels($ks = NULL) {
+ $conf = self::loadConfigXML();
+
+ foreach ($conf->Keyspaces as $keySpace) {
+
+ var_dump($keySpace);
+
+ $ksName = $keySpace->attributes()->Name;
+ var_dump($ksName);
- static public function toXML(&$results) {
- }
+ $columnFamilies = $keySpace->xpath('Keyspace/ColumnFamily');
+ var_dump($columnFamilies);
- static public function toSerialised(&$results) {
- }
+ // var_dump($keySpace);
+ }
- static public function toArray(&$results) {
- }
+ }
+ static public function toJSON(&$results) {
+ }
+
+ static public function toXML(&$results) {
+ }
+
+ static public function toSerialised(&$results) {
+ }
+
+ static public function toArray(&$results) {
+ }
+ */
}
View
66 lib/SuperColumn.class.php
@@ -4,51 +4,51 @@
*
* @package pandra
*/
-class PandraSuperColumn extends PandraColumnFamily {
-
- /* @var array container for child column family objects, indexed to cf name */
- protected $columnFamilies = array();
+class PandraSuperColumn extends PandraColumnContainer {
/* @var PandraColumnFamily column family parent reference */
private $_parentCF = NULL;
- public function __construct($superName, $parentCF) {
- $this->superColumn = $superName;
- $this->_parentCF = $parentCF;
- }
-
- public function constructColumns() { }
+ public function __construct($superName, PandraColumnFamilySuper $parentCF = NULL) {
+ // SuperColumn name
+ $this->name = $superName;
- public function addColumnFamily($cfName, PandraColumnFamily $cfObj = NULL) {
-
- if ($this->_parentCF instanceof PandraColumnFamily) {
- // Will get here if dimensions is key > SuperColumn > ColumnFamily > SuperColumn
- throw new RuntimeException('Cannot add ColumnFamily: Dimensionality Exceeded');
+ // Reference parent ColumnFamilySuper
+ if ($parentCF !== NULL) {
+ $this->_parentCF = $parentCF;
}
+ parent::__construct();
+ }
- if ($cfObj === NULL) {
- $cfObj = new PandraColumnFamily($this->keyID);
- }
+ public function setParentCF($parentCF) {
+ $this->_parentCF = $parentCF;
+ }
- $this->columnFamilies[$cfName] = $cfObj;
+ /**
+ * Save all columns in this loaded columnfamily
+ * @return void
+ */
+ public function save() {
- return $this->getColumnFamily($cfName);
- }
+ $this->checkCFState();
- public function removeColumnFamily() {
- }
+ if ($this->_delete === TRUE) {
+ $client = Pandra::getClient(TRUE);
- public function getColumnFamily($cfName) {
- if (array_key_exists($cfName, $this->columnFamilies)) return $this->columnFamilies[$cfName];
- return NULL;
- }
+ if ($columnPath === NULL) {
+ $columnPath = new cassandra_ColumnPath();
+ $columnPath->column_family = $this->name;
+ }
- public function addSuper($superName) {
- throw new RuntimeException("SuperColumns cannot be containers for SuperColumns");
- }
+ //$client->remove($this->keySpace, $this->keyID, $columnPath, time(), $consistencyLevel);
+ echo 'REMOVING WHOLE COLUMNFAMILY FOR '.$this->keyID;
+ }
- public function getSuper($superName) {
- return NULL;
+ foreach ($this->columns as &$cObj) {
+ echo 'SC SAVING '. $cObj->name;
+ //var_dump($cObj);
+ //$cObj->save();
+ }
}
-
+
}
View
7 lib/Validator.class.php
@@ -113,9 +113,12 @@ static public function check($value, $label, $typeDefs, &$errors) {
case 'int' :
case 'float' :
case 'numeric' :
+ $error = !is_numeric($value);
+ if ($error) $errorMsg[] = "Invalid URL";
+ break;
case 'string' :
case 'bool' :
- eval('$error != is_'.$type.'("'.$value.'");');
+ eval('$error != is_'.$type.'($value);');
if ($error) $errorMsg[] = "Field error, expected ".$type;
break;
case 'maxlength' :
@@ -142,4 +145,4 @@ static public function check($value, $label, $typeDefs, &$errors) {
if (!empty($errorMsg)) $errors[] = array($label => $errorMsg);
return empty($errorMsg);
}
-}
+}
View
195 tests/lib/PandraColumnFamilyTest.class.php
@@ -0,0 +1,195 @@
+<?php
+require_once 'PHPUnit/Framework.php';
+require_once(dirname(__FILE__).'/../../config.php');
+require_once dirname(__FILE__).'/../../lib/ColumnFamily.class.php';
+
+class ColumnFamilyTestObject extends PandraColumnFamily {
+
+ var $keySpace = 'Keyspace1';
+ var $name = 'Standard1';
+
+ public function constructColumns() {
+ $this->addColumn('column1', 'notempty');
+ $this->column_column1 = 'VALUE';
+ }
+}
+
+/**
+ * Test class for PandraColumnFamily.
+ * Generated by PHPUnit on 2010-01-09 at 11:52:22.
+ */
+class PandraColumnFamilyTest extends PHPUnit_Framework_TestCase {
+ /**
+ * @var PandraColumnFamily
+ * @access protected
+ */
+ protected $obj;
+
+ private $_keyID = 'PandraCFTest';
+
+ /**
+ * Sets up the fixture, for example, opens a network connection.
+ * This method is called before a test is executed.
+ *
+ * @access protected
+ */
+ protected function setUp() {
+ $this->obj = new ColumnFamilyTestObject();
+ $this->obj->keyID = $this->_keyID;
+ Pandra::connect('default', 'localhost');
+ }
+
+ /**
+ * Tears down the fixture, for example, closes a network connection.
+ * This method is called after a test is executed.
+ *
+ * @access protected
+ */
+ protected function tearDown() {
+
+ Pandra::disconnectAll();
+ }
+
+ /**
+ * @todo Implement testLastError().
+ */
+ public function testLastError() {
+
+ $this->obj->column_column1 = '';
+ $lastError = $this->obj->lastError();
+
+ $this->assertTrue(isset($lastError['column1']) && !empty($lastError['column1']));
+ }
+
+ /**
+ * @todo Implement testAddColumn().
+ */
+ public function testAddColumn() {
+
+ $columnName = 'newColumn';
+
+ $column = $this->obj->addColumn($columnName);
+ $this->assertTrue(get_class($column) == 'PandraColumn', 'addColumn did not return a PandraColumn');
+
+ $column = $this->obj->getColumn($columnName);
+ $this->assertTrue( ($column->name == $columnName), 'Incorrect column name returned' );
+ }
+
+ /**
+ * @todo Implement testGetColumn().
+ */
+ public function testGetColumn() {
+
+ $columnName = 'column1';
+
+ $column = $this->obj->getColumn($columnName);
+ $this->assertTrue(get_class($column) == 'PandraColumn', 'getColumn did not return a PandraColumn');
+ $this->assertTrue($column->name == $columnName, 'Column name mismatch, expected '.$columnName.', received ',$column->name);
+ }
+
+ /**
+ * @todo Implement testListColumns().
+ */
+ public function testListColumns() {
+ $columns = $this->obj->listColumns();
+ $this->assertTrue(is_array($columns) && !empty($columns));
+ }
+
+ /**
+ * @todo Implement testGetRawSlice().
+ */
+ public function testGetRawSlice() {
+ $this->assertTrue($this->obj->save(), $this->obj->lastError());
+ $slice = $this->obj->getRawSlice($this->_keyID);
+
+ $this->assertTrue(is_array($slice));
+ $this->assertEquals(get_class($slice[0]), 'cassandra_ColumnOrSuperColumn');
+ }
+
+ /**
+ * @todo Implement testSave().
+ */
+ public function testSave() {
+ $this->assertTrue($this->obj->save(), $this->obj->lastError());
+
+ // reload the object, test that column1 is the same
+ $newObj = new ColumnFamilyTestObject($this->_keyID);
+ $this->assertTrue($this->obj->column_column1 == $newObj->column_column1,
+ "Column didn't match after reload. Expected '".$this->obj->column_column1."', received '".$newObj->column_column1."'");
+
+ $this->assertTrue($newObj->isLoaded());
+
+ $this->obj->delete();
+ $this->obj->save();
+ }
+
+ /**
+ * Tests that resets() after deletes or column changes revert flags
+ */
+ public function testReset() {
+
+ $this->obj->reset();
+ $this->obj->delete();
+
+ $columns = $this->obj->listColumns();
+ foreach ($columns as $column) {
+ $this->obj->getColumn($column)->setValue('NEW VALUE');
+ $this->obj->getColumn($column)->delete();
+ }
+
+ // make sure everything is marked for delete
+ $columns = $this->obj->listColumns();
+ foreach ($columns as $column) {
+ $this->assertTrue($this->obj->getColumn($column)->isModified());
+ $this->assertTrue($this->obj->getColumn($column)->isDeleted());
+ }
+
+ $this->assertTrue($this->obj->isDeleted());
+
+ $this->obj->reset();
+
+ $columns = $this->obj->listColumns();
+ foreach ($columns as $column) {
+ $this->assertFalse($this->obj->getColumn($column)->isDeleted());
+ $this->assertFalse($this->obj->getColumn($column)->isModified());
+ }
+
+ $this->assertFalse($this->obj->isDeleted());
+ }
+
+ /**
+ * Tests that the ColumnFamily columns can be populated by a two dimensional
+ * associative array or from a JSON string
+ */
+ public function testPopulate() {
+ $c1NewValue = 'TEST POST VALUE';
+
+ $_POST = array(
+ 'column1' => $c1NewValue
+ );
+
+ $this->assertTrue($this->obj->populate($_POST));
+ $this->assertEquals($this->obj->column_column1, $c1NewValue);
+
+ $c1NewValue = 'TEST JSON VALUE';
+
+ $json = '{"column1":"'.$c1NewValue.'"}';
+ $this->assertTrue($this->obj->populate($json));
+ $this->assertEquals($this->obj->column_column1, $c1NewValue);
+ }
+
+ /**
+ * Tests column is correctly set and modified flag is true
+ */
+ public function testSetColumn() {
+ $c1NewValue = 'TEST POST VALUE';
+
+ $this->obj->getColumn('column1')->reset();
+ $this->assertFalse($this->obj->getColumn('column1')->isModified());
+
+ $this->obj->setColumn('column1', $c1NewValue);
+ $this->assertEquals($this->obj->column_column1, $c1NewValue);
+ $this->assertTrue($this->obj->getColumn('column1')->isModified());
+ }
+}
+?>
View
129 tests/lib/PandraColumnTest.php
@@ -0,0 +1,129 @@
+<?php
+require_once 'PHPUnit/Framework.php';
+require_once(dirname(__FILE__).'/../../config.php');
+
+class ColumnFamilyTestObject extends PandraColumnFamily {
+
+}
+
+/**
+ * Test class for PandraColumn.
+ * Generated by PHPUnit on 2010-01-09 at 11:52:23.
+ */
+class PandraColumnTest extends PHPUnit_Framework_TestCase {
+
+ public $obj = NULL;
+ public $parentCF = NULL;
+
+ public $columnName = 'mycolumn';
+
+ /**
+ * Sets up the fixture, for example, opens a network connection.
+ * This method is called before a test is executed.
+ *
+ * To test multiple nodes, add their connection strings here
+ *
+ * @access protected
+ */
+ protected function setUp() {
+ $this->parentCF = new ColumnFamilyTestObject();
+ $this->obj = new PandraColumn($this->columnName, $this->parentCF);
+ }
+
+ /**
+ * Tears down the fixture, for example, closes a network connection.
+ * This method is called after a test is executed.
+ *
+ * @access protected
+ */
+ protected function tearDown() {
+
+ }
+
+ public function testSetValue() {
+ $this->assertTrue($this->obj->setValue('NEW VALUE'));
+ $this->assertTrue($this->obj->isModified());
+ }
+
+ public function testBindTime() {
+return;
+ $time = $this->obj->bindTime();
+ $this->assertType(PHPUnit_Framework_Constraint_IsType::TYPE_INT, $time);
+ $this->assertEquals($time, $this->obj->timestamp);
+
+ $time = time();
+ sleep(1);
+ $colTime = $this->obj->bindTime($time);
+ $this->assertType(PHPUnit_Framework_Constraint_IsType::TYPE_INT, $colTime);
+ $this->assertEquals($time, $this->obj->timestamp);
+ }
+
+ public function testReset() {
+ $this->assertFalse($this->obj->isModified());
+ $this->obj->setValue('NEW VALUE');
+ $this->assertTrue($this->obj->isModified());
+
+ $this->obj->reset();
+ $this->assertFalse($this->obj->isModified());
+ }
+
+ public function testMarkDelete() {
+ $this->assertFalse($this->obj->isModified());
+ $this->assertFalse($this->obj->isDeleted());
+
+ $this->obj->markDelete();
+
+ $this->assertTrue($this->obj->isModified());
+ $this->assertTrue($this->obj->isDeleted());
+ }
+
+ public function testCallbackValue() {
+ $this->obj->callback = 'md5';
+ $value = 'NEW VALUE';
+ $this->obj->setValue($value);
+
+ $this->assertEquals($this->obj->value, $value);
+
+ $md5Value = md5($value);
+ $this->assertEquals($this->obj->callbackValue(), $md5Value);
+
+
+ }
+
+ public function testCast() {
+
+ $column = new cassandra_Column();
+ $column->value = 'THRIFT COLUMN VALUE';
+
+ $pandraColumn = $this->obj->cast($column);
+
+ $this->assertEquals(get_class($pandraColumn), 'PandraColumn');
+ $this->assertEquals($pandraColumn->value, $column->value);
+
+ // @TODO cassandra_ColumnOrSupercolumn();
+
+ }
+
+ public function testDelete() {
+ $this->assertFalse($this->obj->isModified());
+ $this->assertFalse($this->obj->isDeleted());
+
+ $this->obj->delete();
+
+ $this->assertTrue($this->obj->isModified());
+ $this->assertTrue($this->obj->isDeleted());
+ }
+
+ public function testIsModifed() {
+ $this->assertFalse($this->obj->isModified());
+
+ $this->obj->value = 'OH HI GREAT TO SEE YOU';
+
+ $this->assertTrue($this->obj->isModified());
+ }
+
+ public function testSave() {
+
+ }
+}
+?>
View
132 tests/lib/PandraTest.php
@@ -0,0 +1,132 @@
+<?php
+require_once 'PHPUnit/Framework.php';
+require_once(dirname(__FILE__).'/../../config.php');
+require_once dirname(__FILE__).'/../../lib/Pandra.class.php';
+
+/**
+ * Test class for Pandra.
+ * Generated by PHPUnit on 2010-01-09 at 11:52:23.
+ */
+class PandraTest extends PHPUnit_Framework_TestCase {
+
+ /**
+ * Sets up the fixture, for example, opens a network connection.
+ * This method is called before a test is executed.
+ *
+ * To test multiple nodes, add their connection strings here
+ *
+ * @access protected
+ */
+ protected function setUp() {
+ Pandra::connect('default', 'localhost');
+ }
+
+ /**
+ * Tears down the fixture, for example, closes a network connection.
+ * This method is called after a test is executed.
+ *
+ * @access protected
+ */
+ protected function tearDown() {
+ Pandra::disconnect('default');
+ }
+
+ /**
+ * Get a modes list
+ */
+ public function testSupportedModes() {
+ $modes = Pandra::getSupportedModes();
+ $this->assertTrue(is_array($modes) && !empty($modes));
+ }
+
+ /**
+ * Set Read Mode
+ */
+ public function testSetReadMode() {
+ $supportedModes = Pandra::getSupportedModes();
+ foreach ($supportedModes as $mode) {
+ Pandra::setReadMode($mode);
+ $this->assertEquals(Pandra::getReadMode(), $mode);
+ }
+ }
+
+ /**
+ * Set Write Mode
+ */
+ public function testSetWriteMode() {
+ $supportedModes = Pandra::getSupportedModes();
+ foreach ($supportedModes as $mode) {
+ Pandra::setWriteMode($mode);
+ $this->assertEquals(Pandra::getWriteMode(), $mode);
+ }
+ }
+
+ /**
+ * Set active node to named 'default', as well as unknown 'NOP'
+ */
+ public function testSetActiveNode() {
+ $this->assertTrue(Pandra::setActiveNode('default'));
+ $this->assertFalse(Pandra::setActiveNode('NOP'));
+ }
+
+ /**
+ * @todo Implement testDisconnect().
+ */
+ public function testDisconnect() {
+ $this->assertTrue(Pandra::disconnect('default'));
+ }
+
+ /**
+ * Disconnect all nodes
+ */
+ public function testDisconnectAll() {
+ $this->assertTrue(Pandra::disconnectAll());
+ }
+
+ /**
+ * Connect to a good and bad host
+ */
+ public function testConnect() {
+ $this->assertTrue(Pandra::connect('default', 'localhost'));
+ $this->assertFalse(Pandra::connect('default_BAD', 'ih-opethishostdoesntexist'));
+ }
+
+ /**
+ * Get client for all supported modes
+ */
+ public function testGetClient() {
+ $supportedModes = Pandra::getSupportedModes();
+ foreach ($supportedModes as $mode) {
+ Pandra::setReadMode($mode);
+ $client = Pandra::getClient();
+ $this->assertEquals(get_class($client), 'CassandraClient', "Bad Client (mode $mode)");
+ }
+ }
+
+ /**
+ * Describe a named keyspace
+ */
+ public function testGetKeyspace() {
+ $ks = Pandra::getKeyspace('Keyspace1');
+
+ $this->assertTrue(is_array($ks) && !empty($ks));
+
+ // While we don't care about the individual ColumnFamilies, we should
+ // at least be able to pull out their basic attributes
+ $expectedKeys = array('FlushPeriodInMinutes', 'Type', 'Desc');
+
+ foreach ($ks as $keySpace => $columnFamily) {
+ $diff = array_diff($expectedKeys, array_keys($columnFamily));
+ $this->assertTrue(empty($diff));
+ }
+ }
+
+ /**
+ * @todo Implement testLoadConfigXML().
+ */
+ public function testLoadConfigXML() {
+ $config = Pandra::loadConfigXML();
+ $this->assertTrue(get_class($config) == 'SimpleXMLElement');
+ }
+}
+?>
View
131 tests/lib/PandraValidatorTest.php
@@ -0,0 +1,131 @@
+<?php
+require_once 'PHPUnit/Framework.php';
+require_once(dirname(__FILE__).'/../../config.php');
+require_once dirname(__FILE__).'/../../lib/Validator.class.php';
+
+/**
+ * Test class for PandraValidator.
+ * Generated by PHPUnit on 2010-01-09 at 11:52:24.
+ */
+class PandraValidatorTest extends PHPUnit_Framework_TestCase {
+ /**
+ * @var PandraValidator
+ * @access protected
+ */
+ protected $object;
+
+ /**
+ * Sets up the fixture, for example, opens a network connection.
+ * This method is called before a test is executed.
+ *
+ * @access protected
+ */
+ protected function setUp() {
+ $this->object = new PandraValidator;
+ }
+
+ /**
+ * Tears down the fixture, for example, closes a network connection.
+ * This method is called after a test is executed.
+ *
+ * @access protected
+ */
+ protected function tearDown() {}
+
+ public function test_notempty() {
+ $errors = array();
+ $this->assertTrue(PandraValidator::check('string', 'notempty', 'notempty', $errors));
+
+ $this->assertFalse(PandraValidator::check('', 'notempty', 'notempty', $errors));
+ $this->assertTrue(!empty($errors));
+ }
+
+ public function test_isempty() {
+ $errors = array();
+ $this->assertTrue(PandraValidator::check('', 'isempty', 'isempty', $errors));
+
+ $this->assertFalse(PandraValidator::check('string', 'isempty', 'isempty', $errors));
+ $this->assertTrue(!empty($errors));
+ }
+
+ public function test_int() {
+ $errors = array();
+ $this->assertTrue(PandraValidator::check(1, 'int', 'int', $errors));
+
+ $this->assertFalse(PandraValidator::check('string', 'int', 'int', $errors));
+ $this->assertTrue(!empty($errors));
+ }
+
+ public function test_float() {
+ $errors = array();
+ $this->assertTrue(PandraValidator::check(1, 'float', 'float', $errors));
+
+ $this->assertFalse(PandraValidator::check('string', 'float', 'float', $errors));
+ $this->assertTrue(!empty($errors));
+ }
+
+ public function test_numeric() {
+ $errors = array();
+ $this->assertTrue(PandraValidator::check(1, 'notempty', 'numeric', $errors));
+
+ $this->assertFalse(PandraValidator::check('', 'notempty', 'numeric', $errors));
+ $this->assertTrue(!empty($errors));
+ }
+
+ public function test_string() {
+ $errors = array();
+ $this->assertTrue(PandraValidator::check('string', 'string', 'string', $errors));
+
+ $this->assertFalse(PandraValidator::check(1, 'string', 'string', $errors));
+ $this->assertTrue(!empty($errors));
+ }
+
+ public function test_bool() {
+ $errors = array();
+ $this->assertTrue(PandraValidator::check(true, 'bool', 'bool', $errors));
+
+ $this->assertFalse(PandraValidator::check('NO', 'bool', 'bool', $errors));
+ $this->assertTrue(!empty($errors));
+ }
+
+ public function test_maxlength() {
+ $errors = array();
+ $this->assertTrue(PandraValidator::check('string', 'notempty', 'notempty', $errors));
+
+ $this->assertFalse(PandraValidator::check('', 'notempty', 'notempty', $errors));
+ $this->assertTrue(!empty($errors));
+ }
+
+ public function test_minlength() {
+ $errors = array();
+ $this->assertTrue(PandraValidator::check('string', 'notempty', 'notempty', $errors));
+
+ $this->assertFalse(PandraValidator::check('', 'notempty', 'notempty', $errors));
+ $this->assertTrue(!empty($errors));
+ }
+
+ public function test_enum() {
+ $errors = array();
+ $this->assertTrue(PandraValidator::check('string', 'notempty', 'notempty', $errors));
+
+ $this->assertFalse(PandraValidator::check('', 'notempty', 'notempty', $errors));
+ $this->assertTrue(!empty($errors));
+ }
+
+ public function test_email() {
+ $errors = array();
+ $this->assertTrue(PandraValidator::check('string', 'notempty', 'notempty', $errors));
+
+ $this->assertFalse(PandraValidator::check('', 'notempty', 'notempty', $errors));
+ $this->assertTrue(!empty($errors));
+ }
+
+ public function test_url() {
+ $errors = array();
+ $this->assertTrue(PandraValidator::check('string', 'notempty', 'notempty', $errors));
+
+ $this->assertFalse(PandraValidator::check('', 'notempty', 'notempty', $errors));
+ $this->assertTrue(!empty($errors));
+ }
+}
+?>

0 comments on commit 33a065f

Please sign in to comment.