Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

New pluggable type system for Entities

 * New 'typeHanlder' method on \Spot\Config to set/get type handlers
 * Defaults: string, int, float, boolean, datetime, date, timestamp,
   year, month, day
 * Whitespace trimming in various files
  • Loading branch information...
commit a353f2d590ccb0167664a57c8448a1afd96911fd 1 parent 4668589
@vlucas authored
View
242 lib/Spot/Config.php
@@ -3,115 +3,161 @@
/**
* @package Spot
- * @link http://spot.os.ly
*/
class Config implements \Serializable
{
- protected $_defaultConnection;
- protected $_connections = array();
-
-
- /**
- * Add database connection
- *
- * @param string $name Unique name for the connection
- * @param string $dsn DSN string for this connection
- * @param array $options Array of key => value options for adapter
- * @param boolean $defaut Use this connection as the default? The first connection added is automatically set as the default, even if this flag is false.
- * @return Spot_Adapter_Interface Spot adapter instance
- * @throws Spot_Exception
- */
- public function addConnection($name, $dsn, array $options = array(), $default = false)
- {
- // Connection name must be unique
- if(isset($this->_connections[$name])) {
- throw new Exception("Connection for '" . $name . "' already exists. Connection name must be unique.");
+ protected $_defaultConnection;
+ protected $_connections = array();
+ protected static $_typeHandlers = array();
+
+ public function __construct()
+ {
+ // Setup default type hanlders
+ $this->typeHandler('string', '\Spot\Type\String');
+ $this->typeHandler('text', '\Spot\Type\String');
+
+ $this->typeHandler('int', '\Spot\Type\Integer');
+ $this->typeHandler('integer', '\Spot\Type\Integer');
+
+ $this->typeHandler('float', '\Spot\Type\Float');
+ $this->typeHandler('double', '\Spot\Type\Float');
+ $this->typeHandler('decimal', '\Spot\Type\Float');
+
+ $this->typeHandler('bool', '\Spot\Type\Boolean');
+ $this->typeHandler('boolean', '\Spot\Type\Boolean');
+
+ $this->typeHandler('datetime', '\Spot\Type\DateTime');
+ $this->typeHandler('date', '\Spot\Type\DateTime');
+ $this->typeHandler('timestamp', '\Spot\Type\Integer');
+ $this->typeHandler('year', '\Spot\Type\Integer');
+ $this->typeHandler('month', '\Spot\Type\Integer');
+ $this->typeHandler('day', '\Spot\Type\Integer');
}
-
- $dsnp = \Spot\Adapter\AdapterAbstract::parseDSN($dsn);
- $adapterClass = "\\Spot\\Adapter\\" . ucfirst($dsnp['adapter']);
- $adapter = new $adapterClass($dsn, $options);
-
- // Set as default connection?
- if(true === $default || null === $this->_defaultConnection) {
- $this->_defaultConnection = $name;
+
+ /**
+ * Add database connection
+ *
+ * @param string $name Unique name for the connection
+ * @param string $dsn DSN string for this connection
+ * @param array $options Array of key => value options for adapter
+ * @param boolean $defaut Use this connection as the default? The first connection added is automatically set as the default, even if this flag is false.
+ * @return Spot_Adapter_Interface Spot adapter instance
+ * @throws Spot_Exception
+ */
+ public function addConnection($name, $dsn, array $options = array(), $default = false)
+ {
+ // Connection name must be unique
+ if(isset($this->_connections[$name])) {
+ throw new Exception("Connection for '" . $name . "' already exists. Connection name must be unique.");
+ }
+
+ $dsnp = \Spot\Adapter\AdapterAbstract::parseDSN($dsn);
+ $adapterClass = "\\Spot\\Adapter\\" . ucfirst($dsnp['adapter']);
+ $adapter = new $adapterClass($dsn, $options);
+
+ // Set as default connection?
+ if(true === $default || null === $this->_defaultConnection) {
+ $this->_defaultConnection = $name;
+ }
+
+ // Store connection and return adapter instance
+ $this->_connections[$name] = $adapter;
+ return $adapter;
}
-
- // Store connection and return adapter instance
- $this->_connections[$name] = $adapter;
- return $adapter;
- }
-
-
- /**
- * Get connection by name
- *
- * @param string $name Unique name of the connection to be returned
- * @return Spot_Adapter_Interface Spot adapter instance
- * @throws Spot_Exception
- */
- public function connection($name = null)
- {
- if(null === $name) {
- return $this->defaultConnection();
+
+
+ /**
+ * Get connection by name
+ *
+ * @param string $name Unique name of the connection to be returned
+ * @return Spot_Adapter_Interface Spot adapter instance
+ * @throws Spot_Exception
+ */
+ public function connection($name = null)
+ {
+ if(null === $name) {
+ return $this->defaultConnection();
+ }
+
+ // Connection name must be unique
+ if(!isset($this->_connections[$name])) {
+ return false;
+ }
+
+ return $this->_connections[$name];
}
-
- // Connection name must be unique
- if(!isset($this->_connections[$name])) {
- return false;
+
+
+ /**
+ * Get type handler class by type
+ *
+ * @param string $type Field type (i.e. 'string' or 'int', etc.)
+ * @return Spot_Adapter_Interface Spot adapter instance
+ */
+ public function typeHandler($type, $class = null)

Since this isn't static, and it is called from a static context, PHP provides warnings about using $this implicitly (unsafely).

@vlucas Owner
vlucas added a note

This is been fixed in the latest commit 4f2a2a0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ {
+ if(null === $class) {
+ if(!isset(self::$_typeHandlers[$type])) {
+ throw new \InvalidArgumentException("Type '$type' not registered. Register the type class handler with \Spot\Config::typeHanlder('$type', '\Namespaced\Path\Class').");
+ }
+ return self::$_typeHandlers[$type];
+ }
+
+ if(!class_exists($class)) {
+ throw new \InvalidArgumentException("Second parameter must be valid className with full namespace. Check the className and ensure the class is loaded before registering it as a type handler.");
+ }
+
+ return self::$_typeHandlers[$type] = $class;
+ }
+
+
+ /**
+ * Get default connection
+ *
+ * @return Spot_Adapter_Interface Spot adapter instance
+ * @throws Spot_Exception
+ */
+ public function defaultConnection()
+ {
+ return $this->_connections[$this->_defaultConnection];
+ }
+
+
+ /**
+ * Class loader
+ *
+ * @param string $className Name of class to load
+ */
+ public static function loadClass($className)
+ {
+ $loaded = false;
+
+ // Require Spot namespaced files by assumed folder structure (naming convention)
+ if(false !== strpos($className, "Spot\\")) {
+ $classFile = trim(str_replace("\\", "/", str_replace("_", "/", str_replace('Spot\\', '', $className))), '\\');
+ $loaded = require_once(__DIR__ . "/" . $classFile . ".php");
+ }
+
+ return $loaded;
}
-
- return $this->_connections[$name];
- }
-
-
- /**
- * Get default connection
- *
- * @return Spot_Adapter_Interface Spot adapter instance
- * @throws Spot_Exception
- */
- public function defaultConnection()
- {
- return $this->_connections[$this->_defaultConnection];
- }
-
-
- /**
- * Class loader
- *
- * @param string $className Name of class to load
- */
- public static function loadClass($className)
- {
- $loaded = false;
-
- // Require Spot namespaced files by assumed folder structure (naming convention)
- if(false !== strpos($className, "Spot\\")) {
- $classFile = trim(str_replace("\\", "/", str_replace("_", "/", str_replace('Spot\\', '', $className))), '\\');
- $loaded = require_once(__DIR__ . "/" . $classFile . ".php");
+
+
+ /**
+ * Default serialization behavior is to not attempt to serialize stored
+ * adapter connections at all (thanks @TheSavior re: Issue #7)
+ */
+ public function serialize()
+ {
+ return serialize(array());
+ }
+
+ public function unserialize($serialized)
+ {
}
-
- return $loaded;
- }
-
-
- /**
- * Default serialization behavior is to not attempt to serialize stored
- * adapter connections at all (thanks @TheSavior re: Issue #7)
- */
- public function serialize()
- {
- return serialize(array());
- }
-
- public function unserialize($serialized)
- {
- }
}
/**
* Register 'spot_load_class' function as an autoloader for files prefixed with 'Spot_'
*/
-spl_autoload_register(array('\Spot\Config', 'loadClass'));
+spl_autoload_register(array('\Spot\Config', 'loadClass'));
View
96 lib/Spot/Entity.php
@@ -5,36 +5,35 @@
* Entity object
*
* @package Spot
-* @link http://spot.os.ly
*/
abstract class Entity
{
protected static $_datasource;
protected static $_datasourceOptions = array();
protected static $_connection;
-
+
// Entity data storage
protected $_data = array();
protected $_dataModified = array();
-
+
// Entity error messages (may be present after save attempt)
protected $_errors = array();
-
+
/**
* Constructor - allows setting of object properties with array on construct
*/
public function __construct(array $data = array())
{
$this->initFields();
-
+
// Set given data
if($data) {
$this->data($data, false);
}
}
-
-
+
+
/**
* Set all field values to their defualts or null
*/
@@ -47,8 +46,8 @@ protected function initFields()
}
}
}
-
-
+
+
/**
* Datasource getter/setter
*/
@@ -73,8 +72,8 @@ public static function datasourceOptions($dsOpts = null)
}
return static::$_datasourceOptions;
}
-
-
+
+
/**
* Named connection getter/setter
*/
@@ -86,8 +85,8 @@ public static function connection($connection = null)
}
return static::$_connection;
}
-
-
+
+
/**
* Return defined fields of the entity
*/
@@ -95,8 +94,8 @@ public static function fields()
{
return array();
}
-
-
+
+
/**
* Return defined fields of the entity
*/
@@ -104,8 +103,8 @@ public static function relations()
{
return array();
}
-
-
+
+
/**
* Gets and sets data on the current entity
*/
@@ -115,10 +114,17 @@ public function data($data = null, $modified = true)
if(null === $data || !$data) {
return array_merge($this->_data, $this->_dataModified);
}
-
+
// SET
if(is_object($data) || is_array($data)) {
+ $fields = $this->fields();
foreach($data as $k => $v) {
+ // Ensure value is set with type handler if Entity field type
+ if(isset($fields[$k])) {
+ $typeHandler = Config::typeHandler($fields[$k]['type']);
+ $v = $typeHandler::set($this, $v);
+ }
+
if(true === $modified) {
$this->_dataModified[$k] = $v;
} else {
@@ -131,7 +137,7 @@ public function data($data = null, $modified = true)
}
}
-
+
/**
* Return array of field data with data from the field names listed removed
*
@@ -174,8 +180,8 @@ public function hasErrors($field = null)
}
return count($this->_errors) > 0;
}
-
-
+
+
/**
* Error message getter/setter
*
@@ -187,15 +193,15 @@ public function errors($msgs = null)
// Return errors for given field
if(is_string($msgs)) {
return isset($this->_errors[$msgs]) ? $this->_errors[$msgs] : array();
-
+
// Set error messages from given array
} elseif(is_array($msgs)) {
$this->_errors = $msgs;
}
return $this->_errors;
}
-
-
+
+
/**
* Add an error to error messages array
*
@@ -214,8 +220,8 @@ public function error($field, $msg)
$this->_errors[$field][] = $msg;
}
}
-
-
+
+
/**
* Enable isset() for object properties
*/
@@ -223,31 +229,49 @@ public function __isset($key)
{
return isset($this->_data[$key]) || isset($this->_dataModified[$key]);
}
-
-
+
+
/**
* Getter for field properties
*/
public function __get($field)
{
+ $v = null;
+
if(isset($this->_dataModified[$field])) {
- return $this->_dataModified[$field];
+ $v = $this->_dataModified[$field];
} elseif(isset($this->_data[$field])) {
- return $this->_data[$field];
+ $v = $this->_data[$field];
}
- return null;
+
+ if(null !== $v) {
+ $fields = $this->fields();
+ if(isset($fields[$field])) {
+ // Ensure value is get with type handler
+ $typeHandler = Config::typeHandler($fields[$field]['type']);
+ $v = $typeHandler::get($this, $v);
+ }
+ }
+
+ return $v;
}
-
-
+
+
/**
* Setter for field properties
*/
public function __set($field, $value)
{
+ $fields = $this->fields();
+ if(isset($fields[$field])) {
+ // Ensure value is set with type handler
+ $typeHandler = Config::typeHandler($fields[$field]['type']);
+ $value = $typeHandler::set($this, $value);
+ }
$this->_dataModified[$field] = $value;
}
-
-
+
+
/**
* String representation of the class
*/
@@ -255,4 +279,4 @@ public function __toString()
{
return __CLASS__;
}
-}
+}
View
67 lib/Spot/Entity/Manager.php
@@ -6,7 +6,6 @@
* Entity Manager for storing information about entities
*
* @package Spot
- * @link http://spot.os.ly
*/
class Manager
{
@@ -17,13 +16,13 @@ class Manager
protected static $_fieldDefaultValues = array();
protected static $_relations = array();
protected static $_primaryKeyField = array();
-
+
// Connection and datasource info
protected static $_connection = array();
protected static $_datasource = array();
protected static $_datasourceOptions = array();
-
-
+
+
/**
* Get formatted fields with all neccesary array keys and values.
* Merges defaults with defined field values to ensure all options exist for each field.
@@ -36,11 +35,11 @@ public function fields($entityName)
if(!is_string($entityName)) {
throw new \Spot\Exception(__METHOD__ . " only accepts a string. Given (" . gettype($entityName) . ")");
}
-
+
if(!is_subclass_of($entityName, '\Spot\Entity')) {
throw new \Spot\Exception($entityName . " must be subclass of '\Spot\Entity'.");
}
-
+
if(isset(self::$_fields[$entityName])) {
$returnFields = self::$_fields[$entityName];
} else {
@@ -56,12 +55,12 @@ public function fields($entityName)
// Datasource Options
$entityDatasourceOptions = $entityName::datasourceOptions();
self::$_datasourceOptions[$entityName] = $entityDatasourceOptions;
-
+
// Connection info
$entityConnection = $entityName::connection();
// If no adapter specified, Spot will use default one from config object (or first one set if default is not explicitly set)
self::$_connection[$entityName] = ($entityConnection) ? $entityConnection : false;
-
+
// Default settings for all fields
$fieldDefaults = array(
'type' => 'string',
@@ -71,15 +70,15 @@ public function fields($entityName)
'null' => true,
'unsigned' => false,
'fulltext' => false,
-
+
'primary' => false,
'index' => false,
'unique' => false,
'serial' => false,
-
+
'relation' => false
);
-
+
// Type default overrides for specific field types
$fieldTypeDefaults = array(
'string' => array(
@@ -93,14 +92,14 @@ public function fields($entityName)
'unsigned' => true
)
);
-
+
// Get entity fields from entity class
$entityFields = false;
$entityFields = $entityName::fields();
if(!is_array($entityFields) || count($entityFields) < 1) {
throw new \InvalidArgumentException($entityName . " Must have at least one field defined.");
}
-
+
$returnFields = array();
self::$_fieldDefaultValues[$entityName] = array();
foreach($entityFields as $fieldName => $fieldOpts) {
@@ -108,7 +107,7 @@ public function fields($entityName)
if($fieldOpts['type'] != 'relation') {
self::$_fieldsDefined[$entityName][$fieldName] = $fieldOpts;
}
-
+
// Format field will full set of default options
if(isset($fieldOpts['type']) && isset($fieldTypeDefaults[$fieldOpts['type']])) {
// Include type defaults
@@ -117,7 +116,7 @@ public function fields($entityName)
// Merge with defaults
$fieldOpts = array_merge($fieldDefaults, $fieldOpts);
}
-
+
// Store primary key
if(true === $fieldOpts['primary']) {
self::$_primaryKeyField[$entityName] = $fieldName;
@@ -128,11 +127,11 @@ public function fields($entityName)
} else {
self::$_fieldDefaultValues[$entityName][$fieldName] = null;
}
-
+
$returnFields[$fieldName] = $fieldOpts;
}
self::$_fields[$entityName] = $returnFields;
-
+
// Relations
$entityRelations = array();
$entityRelations = $entityName::relations();
@@ -145,8 +144,8 @@ public function fields($entityName)
}
return $returnFields;
}
-
-
+
+
/**
* Get field information exactly how it is defined in the class
*
@@ -160,8 +159,8 @@ public function fieldsDefined($entityName)
}
return self::$_fieldsDefined[$entityName];
}
-
-
+
+
/**
* Get field default values as defined in class field definitons
*
@@ -175,8 +174,8 @@ public function fieldDefaultValues($entityName)
}
return self::$_fieldDefaultValues[$entityName];
}
-
-
+
+
/**
* Get defined relations
*
@@ -190,8 +189,8 @@ public function relations($entityName)
}
return self::$_relations[$entityName];
}
-
-
+
+
/**
* Get value of primary key for given row result
*
@@ -204,8 +203,8 @@ public function primaryKeyField($entityName)
}
return self::$_primaryKeyField[$entityName];
}
-
-
+
+
/**
* Check if field exists in defined fields
*
@@ -216,8 +215,8 @@ public function fieldExists($entityName, $field)
{
return array_key_exists($field, $this->fields($entityName));
}
-
-
+
+
/**
* Return field type
*
@@ -230,8 +229,8 @@ public function fieldType($entityName, $field)
$fields = $this->fields($entityName);
return $this->fieldExists($entityName, $field) ? $fields[$field]['type'] : false;
}
-
-
+
+
/**
* Get defined connection to use for entity
*
@@ -245,8 +244,8 @@ public function connection($entityName)
}
return self::$_connection[$entityName];
}
-
-
+
+
/**
* Get name of datasource for given entity class
*
@@ -275,4 +274,4 @@ public function datasourceOptions($entityName)
}
return self::$_datasourceOptions[$entityName];
}
-}
+}
View
30 lib/Spot/Type/Boolean.php
@@ -0,0 +1,30 @@
+<?php
+namespace Spot\Type;
+use Spot\Entity;
+
+class Boolean implements TypeInterface
+{
+ /**
+ * Cast given value to type required
+ */
+ public static function cast($value)
+ {
+ return (bool) $value;
+ }
+
+ /**
+ * Geting value off Entity object
+ */
+ public static function get(Entity $entity, $value)
+ {
+ return self::cast($value);
+ }
+
+ /**
+ * Setting value on Entity object
+ */
+ public static function set(Entity $entity, $value)
+ {
+ return self::cast($value);
+ }
+}
View
34 lib/Spot/Type/Date.php
@@ -0,0 +1,34 @@
+<?php
+namespace Spot\Type;
+use Spot\Entity;
+
+class Datetime implements TypeInterface

You have a Date and Datetime type, which are both registered to the Datetime class in the Config file. You also have both of these type files, however this one (Date)'s class name is Datetime, so when code ever looks up /Spot/Type/Date and loads this file and can't find this class, it would fail. This class should probably be renamed or deleted if it is unused.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+{
+ /**
+ * Cast given value to type required
+ */
+ public static function cast($value)
+ {
+ if(is_string($value) || is_numeric($value)) {
+ // Create new \DateTime instance from string value
+ $value = new \DateTime('@' . strtotime($value));
+ }
+ return $value;
+ }
+
+ /**
+ * Geting value off Entity object
+ */
+ public static function get(Entity $entity, $value)
+ {
+ return self::cast($value);
+ }
+
+ /**
+ * Setting value on Entity object
+ */
+ public static function set(Entity $entity, $value)
+ {
+ return self::cast($value);
+ }
+}
View
34 lib/Spot/Type/Datetime.php
@@ -0,0 +1,34 @@
+<?php
+namespace Spot\Type;
+use Spot\Entity;
+
+class Datetime implements TypeInterface
+{
+ /**
+ * Cast given value to type required
+ */
+ public static function cast($value)
+ {
+ if(is_string($value) || is_numeric($value)) {
+ // Create new \DateTime instance from string value
+ $value = new \DateTime('@' . strtotime($value));
+ }
+ return $value;
+ }
+
+ /**
+ * Geting value off Entity object
+ */
+ public static function get(Entity $entity, $value)
+ {
+ return self::cast($value);
+ }
+
+ /**
+ * Setting value on Entity object
+ */
+ public static function set(Entity $entity, $value)
+ {
+ return self::cast($value);
+ }
+}
View
30 lib/Spot/Type/Float.php
@@ -0,0 +1,30 @@
+<?php
+namespace Spot\Type;
+use Spot\Entity;
+
+class Float implements TypeInterface
+{
+ /**
+ * Cast given value to type required
+ */
+ public static function cast($value)
+ {
+ return (float) $value;
+ }
+
+ /**
+ * Geting value off Entity object
+ */
+ public static function get(Entity $entity, $value)
+ {
+ return self::cast($value);
+ }
+
+ /**
+ * Setting value on Entity object
+ */
+ public static function set(Entity $entity, $value)
+ {
+ return self::cast($value);
+ }
+}
View
30 lib/Spot/Type/Integer.php
@@ -0,0 +1,30 @@
+<?php
+namespace Spot\Type;
+use Spot\Entity;
+
+class Integer implements TypeInterface
+{
+ /**
+ * Cast given value to type required
+ */
+ public static function cast($value)
+ {
+ return (int) $value;
+ }
+
+ /**
+ * Geting value off Entity object
+ */
+ public static function get(Entity $entity, $value)
+ {
+ return self::cast($value);
+ }
+
+ /**
+ * Setting value on Entity object
+ */
+ public static function set(Entity $entity, $value)
+ {
+ return self::cast($value);
+ }
+}
View
33 lib/Spot/Type/String.php
@@ -0,0 +1,33 @@
+<?php
+namespace Spot\Type;
+use Spot\Entity;
+
+class String implements TypeInterface
+{
+ /**
+ * Cast given value to type required
+ */
+ public static function cast($value)
+ {
+ if(null !== $value) {
+ return (string) $value;
+ }
+ return $value;
+ }
+
+ /**
+ * Geting value off Entity object
+ */
+ public static function get(Entity $entity, $value)
+ {
+ return self::cast($value);
+ }
+
+ /**
+ * Setting value on Entity object
+ */
+ public static function set(Entity $entity, $value)
+ {
+ return self::cast($value);
+ }
+}
View
10 lib/Spot/Type/TypeInterface.php
@@ -0,0 +1,10 @@
+<?php
+namespace Spot\Type;
+use Spot\Entity;
+
+interface TypeInterface
+{
+ public static function cast($value);
+ public static function get(Entity $entity, $name);
+ public static function set(Entity $entity, $name);
+}
View
18 phpunit.xml
@@ -0,0 +1,18 @@
+<?xml version="1.0" encoding="utf-8"?>
+<phpunit backupGlobals="false"
+ backupStaticAttributes="false"
+ colors="true"
+ convertErrorsToExceptions="true"
+ convertNoticesToExceptions="true"
+ convertWarningsToExceptions="true"
+ processIsolation="false"
+ stopOnFailure="false"
+ syntaxCheck="false"
+ bootstrap="./tests/init.php"
+>
+ <testsuites>
+ <testsuite name="Spot ORM Tests">
+ <directory>./tests/</directory>
+ </testsuite>
+ </testsuites>
+</phpunit>
View
7 tests/Entity/Post.php
@@ -1,9 +1,8 @@
<?php
/**
- * Post
+ * Post
*
* @package Spot
- * @link http://spot.os.ly
*/
class Entity_Post extends \Spot\Entity
{
@@ -19,7 +18,7 @@ public static function fields()
'date_created' => array('type' => 'datetime')
);
}
-
+
public static function relations()
{
return array(
@@ -32,4 +31,4 @@ public static function relations()
)
);
}
-}
+}
View
5 tests/Entity/Post/Comment.php
@@ -4,12 +4,11 @@
* @todo implement 'BelongsTo' relation for linking back to blog post object
*
* @package Spot
- * @link http://spot.os.ly
*/
class Entity_Post_Comment extends \Spot\Entity
{
protected static $_datasource = 'test_post_comments';
-
+
public static function fields()
{
return array(
@@ -21,4 +20,4 @@ public static function fields()
'date_created' => array('type' => 'datetime')
);
}
-}
+}
View
30 tests/Test/CRUD.php
@@ -24,7 +24,7 @@ public function testSampleNewsInsert()
$post = $mapper->get('Entity_Post');
$post->title = "Test Post";
$post->body = "<p>This is a really awesome super-duper post.</p><p>It's really quite lovely.</p>";
- $post->date_created = $mapper->connection('Entity_Post')->date();
+ $post->date_created = new \DateTime();
$result = $mapper->insert($post); // returns an id
$this->assertTrue($result !== false);
@@ -54,6 +54,26 @@ public function testSelect()
$this->assertTrue($post instanceof Entity_Post);
}
+ public function testInsertThenSelectReturnsProperTypes()
+ {
+ // Insert Post into database
+ $mapper = test_spot_mapper();
+ $post = $mapper->get('Entity_Post');
+ $post->title = "Types Test";
+ $post->body = "<p>This is a really awesome super-duper post.</p><p>It's really quite lovely.</p>";
+ $post->status = 1;
+ $post->date_created = new \DateTime();
+ $result = $mapper->insert($post); // returns an id
+
+ // Read Post from database
+ $post = $mapper->get('Entity_Post', $result);
+
+ // Strict equality
+ $this->assertSame(1, $post->status);
+ $postData = $post->data();
+ $this->assertSame(1, $postData['status']);
+ }
+
public function testSampleNewsUpdate()
{
$mapper = test_spot_mapper();
@@ -62,11 +82,7 @@ public function testSampleNewsUpdate()
$post->title = "Test Post Modified";
$result = $mapper->update($post); // returns boolean
-
- // TESTING
- //var_dump(\Spot\Log::lastQuery());
- //exit();
-
+
$postu = $mapper->first('Entity_Post', array('title' => "Test Post Modified"));
$this->assertTrue($postu instanceof Entity_Post);
}
@@ -79,4 +95,4 @@ public function testSampleNewsDelete()
$this->assertTrue((boolean) $result);
}
-}
+}
View
20 tests/Test/Entity.php
@@ -7,19 +7,19 @@
class Test_Entity extends PHPUnit_Framework_TestCase
{
protected $backupGlobals = false;
-
+
public function testEntitySetDataProperties()
{
$mapper = test_spot_mapper();
$post = new Entity_Post();
-
+
// Set data
$post->title = "My Awesome Post";
$post->body = "<p>Body</p>";
-
+
$data = $post->data();
ksort($data);
-
+
$testData = array(
'id' => null,
'title' => 'My Awesome Post',
@@ -28,10 +28,10 @@ public function testEntitySetDataProperties()
'date_created' => null
);
ksort($testData);
-
+
$this->assertEquals($testData, $data);
}
-
+
public function testEntitySetDataConstruct()
{
$mapper = test_spot_mapper();
@@ -39,10 +39,10 @@ public function testEntitySetDataConstruct()
'title' => 'My Awesome Post',
'body' => '<p>Body</p>'
));
-
+
$data = $post->data();
ksort($data);
-
+
$testData = array(
'id' => null,
'title' => 'My Awesome Post',
@@ -51,7 +51,7 @@ public function testEntitySetDataConstruct()
'date_created' => null
);
ksort($testData);
-
+
$this->assertEquals($testData, $data);
}
@@ -80,4 +80,4 @@ public function testEntityErrors()
// Errors for one key only
$this->assertEquals($postErrors['title'], $post->errors('title'));
}
-}
+}
Please sign in to comment.
Something went wrong with that request. Please try again.