From b6e936574310165e5a58202140177c68aa59073e Mon Sep 17 00:00:00 2001 From: doctrine Date: Sun, 16 Apr 2006 08:41:45 +0000 Subject: [PATCH] --- classes/DataDict.class.php | 2 + classes/Doctrine.class.php | 1 + classes/Record.class.php | 7 +- classes/Sensei/Sensei.class.php | 192 ++++++++++++++++++ classes/Session.class.php | 8 +- classes/Table.class.php | 2 - .../{Blank.class.php => Notblank.class.php} | 2 +- tests/SenseiTestCase.class.php | 109 ++++++++++ tests/UnitTestCase.class.php | 4 +- tests/run.php | 9 +- 10 files changed, 320 insertions(+), 16 deletions(-) create mode 100644 classes/Sensei/Sensei.class.php rename classes/Validator/{Blank.class.php => Notblank.class.php} (90%) create mode 100644 tests/SenseiTestCase.class.php diff --git a/classes/DataDict.class.php b/classes/DataDict.class.php index 38a39d1e1..fdea7f90b 100644 --- a/classes/DataDict.class.php +++ b/classes/DataDict.class.php @@ -50,6 +50,8 @@ class Doctrine_DataDict { return "C($length)"; elseif($length < 4000) return "X"; + else + return "X2"; break; case "mbstring": if($length < 255) diff --git a/classes/Doctrine.class.php b/classes/Doctrine.class.php index 9ef202612..f20a2bdce 100644 --- a/classes/Doctrine.class.php +++ b/classes/Doctrine.class.php @@ -214,6 +214,7 @@ final class Doctrine { case "Exception": case "Session": case "DQL": + case "Sensei": $a[] = self::$path.DIRECTORY_SEPARATOR.$entry; break; default: diff --git a/classes/Record.class.php b/classes/Record.class.php index 7dbb14475..c570e89ff 100644 --- a/classes/Record.class.php +++ b/classes/Record.class.php @@ -385,19 +385,17 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite if($this->state != Doctrine_Record::STATE_TDIRTY && $this->state != Doctrine_Record::STATE_TCLEAN && $this->state != Doctrine_Record::STATE_CLEAN) { - - $this->loaded = true; - + if( ! empty($this->collections)) { foreach($this->collections as $collection) { $collection->load($this); } } else { - $this->refresh(); } $this->state = Doctrine_Record::STATE_CLEAN; } + $this->loaded = true; } if(is_array($this->data[$name])) @@ -458,6 +456,7 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite $this->modified[] = $name; + switch($this->state): case Doctrine_Record::STATE_CLEAN: case Doctrine_Record::STATE_PROXY: diff --git a/classes/Sensei/Sensei.class.php b/classes/Sensei/Sensei.class.php new file mode 100644 index 000000000..793eaa833 --- /dev/null +++ b/classes/Sensei/Sensei.class.php @@ -0,0 +1,192 @@ +hasColumn("loginname","string",32,"unique"); + $this->hasColumn("password","string",32); + } +} +class Sensei_Variable extends Doctrine_Record { + public function setTableDefinition() { + $this->hasColumn("name","string",50); + $this->hasColumn("value","string",10000); + $this->hasColumn("session_id","integer"); + } +} + +class Sensei_Entity_Var extends Sensei_Variable { } +class Sensei_Session_Var extends Doctrine_Record { } +class Sensei_Session extends Doctrine_Record { + public function setUp() { + $this->ownsMany("Sensei_variable","Sensei_variable.session_id"); + $this->hasOne("Sensei_entity","Sensei_session.entity_id"); + } + public function setTableDefinition() { + $this->hasColumn("session_id","string",32); + $this->hasColumn("logged_in","integer",1); + $this->hasColumn("entity_id","integer"); + $this->hasColumn("user_agent","string",200); + $this->hasColumn("updated","integer"); + $this->hasColumn("created","integer"); + } +} + +class Sensei_Exception extends Exception { } + + +class Sensei extends Doctrine_Access { + const ATTR_LIFESPAN = 0; + /** + * @var Sensei_Session $record + */ + private $record; + /** + * @var Doctrine_Session $session + */ + private $session; + /** + * @var Doctrine_Table $table + */ + private $table; + /** + * @var array $attributes + */ + private $attributes = array(); + /** + * @var Doctrine_Collection $vars + */ + private $vars; + + + public function __construct() { + if(headers_sent()) + throw new Sensei_Exception("Headers already sent. Couldn't initialize session."); + + + $this->session = Doctrine_Manager::getInstance()->getCurrentSession(); + $this->table = $this->session->getTable("Sensei_session"); + $this->init(); + + + $this->gc(1); + + if( ! isset($_SESSION)) + session_start(); + } + /** + * getRecord + */ + public function getRecord() { + return $this->record; + } + /** + * init + */ + private function init() { + session_set_save_handler( + array($this,"open"), + array($this,"close"), + array($this,"read"), + array($this,"write"), + array($this,"destroy"), + array($this,"gc") + ); + } + /** + * @param string $username + * @param string $password + * @return boolean + */ + public function login($username,$password) { + $coll = $this->session->query("FROM Sensei_Entity WHERE Sensei_Entity.loginname = ? && Sensei_Entity.password = ?",array($username,$password)); + if(count($coll) > 0) { + $this->record->logged_in = 1; + $this->record->entity_id = $coll[0]->getID(); + $this->record->save(); + return true; + } + return false; + } + /** + * logout + * @return boolean + */ + public function logout() { + if( $this->record->logged_in == true) { + $this->record->logged_in = 0; + $this->record->entity_id = 0; + return true; + } else { + return false; + } + } + public function get($name) { + foreach($this->vars as $var) { + if($var->name == $name) { + return $var->value; + } + } + } + public function set($name,$value) { + foreach($this->vars as $var) { + if($var->name == $name) { + $var->value = $value; + return true; + } + } + return false; + } + public function setAttribute($attr, $value) { + switch($attr): + case Sensei::ATTR_LIFESPAN: + + break; + default: + throw new Sensei_Exception("Unknown attribute"); + endswitch; + + $this->attributes[$attr] = $value; + } + private function open($save_path,$session_name) { + return true; + } + public function close() { + return true; + } + private function read($id) { + $coll = $this->session->query("FROM Sensei_Session WHERE Sensei_Session.session_id = ?",array($id)); + $this->record = $coll[0]; + $this->record->user_agent = $_SERVER['HTTP_USER_AGENT']; + $this->record->updated = time(); + $this->record->session_id = $id; + + if($this->record->getState() == Doctrine_Record::STATE_TDIRTY) { + $this->record->created = time(); + $this->record->save(); + } + $this->vars = $this->record->Sensei_variable; + return ""; + } + public function write($id,$sess_data) { + return true; + } + private function destroy($id) { + $this->record->delete(); + return $r; + } + private function gc($maxlifetime) { + return true; + } + public function flush() { + $this->record->save(); + } + public function __destruct() { + $this->flush(); + } +} +?> diff --git a/classes/Session.class.php b/classes/Session.class.php index 5d75ab1d9..6848396d6 100644 --- a/classes/Session.class.php +++ b/classes/Session.class.php @@ -169,13 +169,15 @@ abstract class Doctrine_Session extends Doctrine_Configurable implements Countab if(isset($this->tables[$name])) return $this->tables[$name]; - + $class = $name."Table"; - if(class_exists($class, false) && in_array("Doctrine_Table", class_parents($class))) + if(class_exists($class, false) && in_array("Doctrine_Table", class_parents($class))) { return new $class($name); - else + } else { + return new Doctrine_Table($name); + } } /** * @return array -- an array of all initialized tables diff --git a/classes/Table.class.php b/classes/Table.class.php index 4ff69e85f..a5151c668 100644 --- a/classes/Table.class.php +++ b/classes/Table.class.php @@ -145,8 +145,6 @@ class Doctrine_Table extends Doctrine_Configurable { if(isset($this->columns)) { $method = new ReflectionMethod($this->name,"setTableDefinition"); $class = $method->getDeclaringClass(); - - print $class->getName(); if( ! isset($this->tableName)) $this->tableName = strtolower($class->getName()); diff --git a/classes/Validator/Blank.class.php b/classes/Validator/Notblank.class.php similarity index 90% rename from classes/Validator/Blank.class.php rename to classes/Validator/Notblank.class.php index d3fcafb47..e342ee9d7 100644 --- a/classes/Validator/Blank.class.php +++ b/classes/Validator/Notblank.class.php @@ -1,5 +1,5 @@ manager = Doctrine_Manager::getInstance(); + $this->manager->setAttribute(Doctrine::ATTR_CACHE, Doctrine::CACHE_NONE); + + if($this->manager->count() > 0) { + $this->session = $this->manager->getSession(0); + $this->session->clear(); + $this->dbh = $this->session->getDBH(); + $this->listener = $this->manager->getAttribute(Doctrine::ATTR_LISTENER); + } else { + $this->dbh = Doctrine_DB::getConnection(); + $this->session = $this->manager->openSession($this->dbh); + } + + + + $this->tables = array("sensei_group","sensei_user","sensei_entity","sensei_session","sensei_variable"); + $tables = $this->tables; + foreach($tables as $name) { + $this->dbh->query("DROP TABLE IF EXISTS $name"); + } + + $this->sensei = new Sensei(); + + $entity = new Sensei_Entity(); + + $entity->loginname = "Chuck Norris"; + $entity->password = "toughguy"; + + $entity->save(); + + $this->init = true; + + $this->record = $this->sensei->getRecord(); + } + public function setUp() { + if( ! $this->init) + $this->init(); + } + + public function testConstructor() { + + $this->assertTrue($this->record instanceof Sensei_Session); + + if(isset($_COOKIE["PHPSESSID"])) { + $this->assertEqual($this->record->session_id, $_COOKIE["PHPSESSID"]); + } + $updated = $this->record->updated; + $this->assertFalse(empty($updated)); + $created = $this->record->created; + $this->assertFalse(empty($created)); + + $this->assertEqual($this->record->user_agent, $_SERVER['HTTP_USER_AGENT']); + + // make the changes persistent + + $this->sensei->flush(); + + if(isset($_COOKIE["PHPSESSID"])) { + $this->assertEqual($this->record->session_id, $_COOKIE["PHPSESSID"]); + } + $updated = $this->record->updated; + $this->assertFalse(empty($updated)); + $created = $this->record->created; + $this->assertFalse(empty($created)); + + $this->assertEqual($this->record->user_agent, $_SERVER['HTTP_USER_AGENT']); + } + + public function testLogin() { + + $this->assertFalse($this->sensei->login('Chuck Norris','unknown')); + $this->assertEqual($this->record->logged_in, null); + + $this->assertEqual($this->record->entity_id, null); + + $this->assertTrue($this->sensei->login('Chuck Norris','toughguy')); + $this->assertEqual($this->record->logged_in, 1); + + $this->assertEqual($this->record->entity_id, 1); + } + public function testLogout() { + $this->assertTrue($this->sensei->logout()); + $this->assertEqual($this->record->logged_in, 0); + + $this->assertEqual($this->record->entity_id, 0); + + $this->sensei->flush(); + + $this->assertEqual($this->record->logged_in, 0); + + $this->assertEqual($this->record->entity_id, 0); + } +} +?> diff --git a/tests/UnitTestCase.class.php b/tests/UnitTestCase.class.php index bcc3fe7b5..d5f2b554c 100644 --- a/tests/UnitTestCase.class.php +++ b/tests/UnitTestCase.class.php @@ -30,13 +30,10 @@ class Doctrine_UnitTestCase extends UnitTestCase { protected $users; protected $tables; - private static $instances; private $init = false; public function init() { $name = get_class($this); - if( ! isset($instances[$name])) - $instances[$name] = $this; $this->manager = Doctrine_Manager::getInstance(); $this->manager->setAttribute(Doctrine::ATTR_CACHE, Doctrine::CACHE_NONE); @@ -61,6 +58,7 @@ class Doctrine_UnitTestCase extends UnitTestCase { foreach($tables as $name) { $table = $this->session->getTable($name); + $table->getCache()->deleteAll(); } diff --git a/tests/run.php b/tests/run.php index 95c50f979..b4008dfae 100644 --- a/tests/run.php +++ b/tests/run.php @@ -1,5 +1,5 @@ "; @@ -23,7 +24,7 @@ $test = new GroupTest("Doctrine Framework Unit Tests"); - +/** $test->addTestCase(new Doctrine_RecordTestCase()); $test->addTestCase(new Doctrine_SessionTestCase()); @@ -40,6 +41,8 @@ $test->addTestCase(new Doctrine_EventListenerTestCase()); $test->addTestCase(new Doctrine_DQL_ParserTestCase()); $test->addTestCase(new Doctrine_BatchIteratorTestCase()); +*/ +$test->addTestCase(new Sensei_UnitTestCase()); //$test->addTestCase(new Doctrine_Cache_FileTestCase()); //$test->addTestCase(new Doctrine_Cache_SqliteTestCase()); @@ -71,5 +74,5 @@ print "Executed queries: ".count($a)."\n"; foreach($a as $query) { print $query."\n"; } - +ob_end_flush(); ?>