Changeset 10576
- Timestamp:
- 08/01/08 15:12:39 (1 year ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
branches/1.1/test/unit/storage/sfMySQLStorageTest.php
r9172 r10576 3 3 /* 4 4 * This file is part of the symfony package. 5 * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com> 5 6 * (c) 2008 Dejan Spasic <spasic.dejan@yahoo.de> 6 7 * … … 11 12 require_once(dirname(__FILE__).'/../../bootstrap/unit.php'); 12 13 14 ob_start(); 15 $plan = 12; 16 $t = new lime_test($plan, new lime_output_color()); 17 13 18 if (!extension_loaded('mysql')) 14 19 { 15 return false; 20 $t->skip('Mysql extension must be loaded', $plan); 21 exit(0); 16 22 } 17 23 18 / *19 $ sfTestMysqlSessionStorage_DatabaseName = 'sf_unit_test';20 $sfTestMysqlSessionStorage_MysqlParameters = array( 21 'database' => $sfTestMysqlSessionStorage_DatabaseName, 22 'username' => 'root', 'password' => '', 'method' => 'normal');23 */ 24 // Configure your database with the settings below in order to run the test 25 $mysql_config = array( 26 'host' => 'localhost', 27 'username' => 'root', 28 'password' => '', 29 ); 24 30 25 ob_start(); 26 $t = new lime_test(5, new lime_output_color()); 31 if (!isset($mysql_config)) 32 { 33 $t->skip('Mysql credentials needed to run these tests', $plan); 34 exit(0); 35 } 27 36 28 if(isset($sfTestMysqlSessionStorage_DatabaseName, $sfTestMysqlSessionStorage_MysqlParameters)) 37 try 29 38 { 30 $connection = mysql_connect('localhost', 31 $sfTestMysqlSessionStorage_MysqlParameters['username'], 32 $sfTestMysqlSessionStorage_MysqlParameters['password']) 33 or $t->fail('Can not connect to mysql server'); 39 // Creating mysql database connection 40 $database = new sfMySQLDatabase($mysql_config); 41 $connection = $database->getResource(); 42 } 43 catch (sfDatabaseException $e) 44 { 45 $t->diag($e->getMessage()); 46 $t->skip('Unable to connect to MySQL database, skipping', $plan); 47 exit(0); 48 } 34 49 35 mysql_query('DROP DATABASE ' . $sfTestMysqlSessionStorage_DatabaseName, $connection); 36 mysql_query('CREATE DATABASE ' . $sfTestMysqlSessionStorage_DatabaseName, $connection) 37 or $t->fail('Can not create database ' . $sfTestMysqlSessionStorage_DatabaseName); 50 // Creates test database 51 mysql_query('DROP DATABASE IF EXISTS sf_mysql_storage_unit_test', $connection); 52 mysql_query('CREATE DATABASE sf_mysql_storage_unit_test', $connection) or $t->fail('Cannot create database sf_mysql_storage_unit_test'); 53 mysql_select_db('sf_mysql_storage_unit_test', $connection); 54 mysql_query("CREATE TABLE `session` ( 55 `sess_id` varchar(40) NOT NULL PRIMARY KEY, 56 `sess_time` int(10) unsigned NOT NULL default '0', 57 `sess_data` text collate utf8_unicode_ci 58 ) ENGINE=MyISAM", $connection) 59 or $t->fail('Can not create table session'); 38 60 39 mysql_select_db($sfTestMysqlSessionStorage_DatabaseName, $connection);40 mysql_close($connection);61 ini_set('session.use_cookies', 0); 62 $session_id = "1"; 41 63 42 unset($connection); 64 $storage = new sfMySQLSessionStorage(array( 65 'db_table' => 'session', 66 'session_id' => $session_id, 67 'database' => $database) 68 ); 43 69 44 // initialize the storage 45 $database = new sfMySQLDatabase($sfTestMysqlSessionStorage_MysqlParameters);70 $t->ok($storage instanceof sfStorage, 'sfMySQLSessionStorage is an instance of sfStorage'); 71 $t->ok($storage instanceof sfDatabaseSessionStorage, 'sfMySQLSessionStorage is an instance of sfDatabaseSessionStorage'); 46 72 47 mysql_query("CREATE TABLE `session` ( 48 `sess_id` varchar(40) NOT NULL PRIMARY KEY, 49 `sess_time` int(10) unsigned NOT NULL default '0', 50 `sess_data` text collate utf8_unicode_ci 51 ) ENGINE=MyISAM", $database->getResource()) 52 or $t->fail('Can not create table session'); 73 // regenerate() 74 $storage->regenerate(false); 75 $t->isnt(session_id(), $session_id, 'regenerate() regenerated the session id'); 76 $session_id = session_id(); 53 77 54 ini_set('session.use_cookies', 0); 55 $sessionId = "1"; 78 // do some session operations 79 $_SESSION['foo'] = 'bar'; 80 $_SESSION['bar'] = 'foo'; 81 unset($_SESSION['foo']); 82 $session_data = session_encode(); 56 83 57 $storage = new sfMySQLSessionStorage(array('db_table' => 'session', 58 'session_id' => $sessionId, 59 'database' => $database)); 84 // end of session 85 session_write_close(); 60 86 61 $t->ok($storage instanceof sfStorage, 'sfMySQLSessionStorage is an instance of sfStorage'); 62 $t->ok($storage instanceof sfDatabaseSessionStorage, 'sfMySQLSessionStorage is an instance of sfDatabaseSessionStorage'); 87 // check session data in the database 88 $result = mysql_query(sprintf('SELECT sess_data FROM session WHERE sess_id = "%s"', $session_id), $connection); 89 list($thisSessData) = mysql_fetch_row($result); 90 $t->is(mysql_num_rows($result), 1, 'session is stored in the database'); 91 $t->is($thisSessData, $session_data, 'session variables are stored in the database'); 63 92 64 // do some session operations 65 $_SESSION['foo'] = 'bar'; 66 $_SESSION['bar'] = 'foo'; 67 unset($_SESSION['foo']); 68 $sessionData = session_encode(); 93 mysql_free_result($result); 94 unset($thisSessData, $result); 69 95 70 // end of session 71 session_write_close(); 96 // sessionRead() 97 try 98 { 99 $retrieved_data = $storage->sessionRead($session_id); 100 $t->pass('sessionRead() does not throw an exception'); 101 } 102 catch (Exception $e) 103 { 104 $t->fail('sessionRead() does not throw an exception'); 105 } 106 $t->is($retrieved_data, $session_data, 'sessionRead() reads session data'); 72 107 73 // check session data in the database 74 $result = mysql_query(sprintf('SELECT sess_data FROM session WHERE sess_id = "%s"', $sessionId), $database->getResource()); 75 list($thisSessData) = mysql_fetch_row($result); 76 $t->is(mysql_num_rows($result), 1, 'session is stored in the database'); 77 $t->is($thisSessData, $sessionData, 'session variables are stored in the database'); 108 // sessionWrite() 109 $_SESSION['baz'] = 'woo'; 110 $session_data = session_encode(); 111 try 112 { 113 $write = $storage->sessionWrite($session_id, $session_data); 114 $t->pass('sessionWrite() does not throw an exception'); 115 } 116 catch (Exception $e) 117 { 118 $t->fail('sessionWrite() does not throw an exception'); 119 } 78 120 79 mysql_free_result($result);80 unset($thisSessData, $result);121 $t->ok($write, 'sessionWrite() returns true'); 122 $t->is($storage->sessionRead($session_id), $session_data, 'sessionWrite() wrote session data'); 81 123 82 // destroy the session 83 $storage->sessionDestroy($sessionId); 84 $result = mysql_query(sprintf('SELECT COUNT(sess_id) FROM session WHERE sess_id = "%s"', $sessionId), $database->getResource()); 124 // sessionDestroy() 125 try 126 { 127 $storage->sessionDestroy($session_id); 128 $t->pass('sessionDestroy() does not throw an exception'); 129 } 130 catch (Exception $e) 131 { 132 $t->fail('sessionDestroy() does not throw an exception'); 133 } 85 134 86 list($count) = mysql_fetch_row($result); 87 $t->is($count, 0, 'session is removed from the database'); 135 $result = mysql_query(sprintf('SELECT COUNT(sess_id) FROM session WHERE sess_id = "%s"', $session_id), $connection); 88 136 89 mysql_free_result($result);90 unset($count, $result);137 list($count) = mysql_fetch_row($result); 138 $t->is($count, 0, 'session is removed from the database'); 91 139 92 mysql_query('DROP DATABASE ' . $sfTestMysqlSessionStorage_DatabaseName, $database->getResource()); 140 mysql_free_result($result); 141 unset($count, $result); 93 142 94 // shutdown the storage 95 $storage->shutdown(); 143 mysql_query('DROP DATABASE sf_mysql_storage_unit_test', $connection); 96 144 97 // shutdown the database98 $database->shutdown();145 // shutdown the storage 146 $storage->shutdown(); 99 147 100 unset($sfTestMysqlSessionStorage_DatabaseName, $sfTestMysqlSessionStorage_MysqlParameters); 101 } 102 else 103 { 104 $t->skip('Mysql credentials needed to run these tests', 5); 105 } 148 // shutdown the database 149 $database->shutdown(); 150 151 unset($mysql_config); branches/1.1/test/unit/storage/sfMySQLiStorageTest.php
r9172 r10576 3 3 /* 4 4 * This file is part of the symfony package. 5 * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com> 5 6 * (c) 2008 Dejan Spasic <spasic.dejan@yahoo.de> 6 7 * … … 11 12 require_once(dirname(__FILE__).'/../../bootstrap/unit.php'); 12 13 14 ob_start(); 15 $plan = 12; 16 $t = new lime_test($plan, new lime_output_color()); 17 13 18 if (!extension_loaded('mysqli')) 14 19 { 15 return false; 20 $t->skip('Mysqli extension must be loaded', $plan); 21 exit(0); 16 22 } 17 23 18 / *19 $ sfTestMysqlSessionStorage_DatabaseName = 'sf_unit_test';20 $sfTestMysqlSessionStorage_MysqlParameters = array( 21 'database' => $sfTestMysqlSessionStorage_DatabaseName, 22 'username' => 'root', 'password' => '', 'method' => 'normal');23 */ 24 // Configure your database with the settings below in order to run the test 25 $mysqli_config = array( 26 'host' => 'localhost', 27 'username' => 'root', 28 'password' => '', 29 ); 24 30 25 ob_start(); 26 $t = new lime_test(5, new lime_output_color()); 31 if (!isset($mysqli_config)) 32 { 33 $t->skip('Mysql credentials needed to run these tests', $plan); 34 exit(0); 35 } 27 36 28 if(isset($sfTestMysqlSessionStorage_DatabaseName, $sfTestMysqlSessionStorage_MysqlParameters)) 37 try 29 38 { 30 $connection = mysqli_connect('localhost', 31 $sfTestMysqlSessionStorage_MysqlParameters['username'], 32 $sfTestMysqlSessionStorage_MysqlParameters['password']) 33 or $t->fail('Can not connect to mysql server'); 39 // Creating mysql database connection 40 $database = new sfMySQLiDatabase($mysqli_config); 41 $connection = $database->getResource(); 42 } 43 catch (sfDatabaseException $e) 44 { 45 $t->diag($e->getMessage()); 46 $t->skip('Unable to connect to MySQL database, skipping', $plan); 47 exit(0); 48 } 34 49 35 mysqli_query($connection, 'DROP DATABASE ' . $sfTestMysqlSessionStorage_DatabaseName); 36 mysqli_query($connection, 'CREATE DATABASE ' . $sfTestMysqlSessionStorage_DatabaseName) 37 or $t->fail('Can not create database ' . $sfTestMysqlSessionStorage_DatabaseName); 50 // Creates test database 51 mysqli_query($connection, 'DROP DATABASE IF EXISTS sf_mysqli_storage_unit_test'); 52 mysqli_query($connection, 'CREATE DATABASE sf_mysqli_storage_unit_test') or $t->fail('Cannot create database sf_mysqli_storage_unit_test'); 53 mysqli_select_db($connection, 'sf_mysqli_storage_unit_test'); 54 mysqli_query($connection, "CREATE TABLE `session` ( 55 `sess_id` varchar(40) NOT NULL PRIMARY KEY, 56 `sess_time` int(10) unsigned NOT NULL default '0', 57 `sess_data` text collate utf8_unicode_ci 58 ) ENGINE=MyISAM") 59 or $t->fail('Can not create table session'); 38 60 39 mysqli_select_db($connection,$sfTestMysqlSessionStorage_DatabaseName);40 mysqli_close($connection);61 ini_set('session.use_cookies', 0); 62 $session_id = "1"; 41 63 42 unset($connection); 64 $storage = new sfMySQLiSessionStorage(array( 65 'db_table' => 'session', 66 'session_id' => $session_id, 67 'database' => $database) 68 ); 43 69 44 // initialize the storage 45 $database = new sfMySQLiDatabase($sfTestMysqlSessionStorage_MysqlParameters);70 $t->ok($storage instanceof sfStorage, 'sfMySQLSessionStorage is an instance of sfStorage'); 71 $t->ok($storage instanceof sfDatabaseSessionStorage, 'sfMySQLSessionStorage is an instance of sfDatabaseSessionStorage'); 46 72 47 mysqli_query($database->getResource(), 48 "CREATE TABLE `session` ( 49 `sess_id` varchar(40) NOT NULL PRIMARY KEY, 50 `sess_time` int(10) unsigned NOT NULL default '0', 51 `sess_data` text collate utf8_unicode_ci 52 ) ENGINE=MyISAM") 53 or $t->fail('Can not create table session'); 73 // regenerate() 74 $storage->regenerate(false); 75 $t->isnt(session_id(), $session_id, 'regenerate() regenerated the session id'); 76 $session_id = session_id(); 54 77 55 ini_set('session.use_cookies', 0); 56 $sessionId = "1"; 78 // do some session operations 79 $_SESSION['foo'] = 'bar'; 80 $_SESSION['bar'] = 'foo'; 81 unset($_SESSION['foo']); 82 $session_data = session_encode(); 57 83 58 $storage = new sfMySQLiSessionStorage(array('db_table' => 'session', 59 'session_id' => $sessionId, 60 'database' => $database)); 84 // end of session 85 session_write_close(); 61 86 62 $t->ok($storage instanceof sfStorage, 'sfMySQLSessionStorage is an instance of sfStorage'); 63 $t->ok($storage instanceof sfDatabaseSessionStorage, 'sfMySQLSessionStorage is an instance of sfDatabaseSessionStorage'); 87 // check session data in the database 88 $result = mysqli_query($connection, sprintf('SELECT sess_data FROM session WHERE sess_id = "%s"', $session_id)); 89 list($thisSessData) = mysqli_fetch_row($result); 90 $t->is(mysqli_num_rows($result), 1, 'session is stored in the database'); 91 $t->is($thisSessData, $session_data, 'session variables are stored in the database'); 64 92 65 // do some session operations 66 $_SESSION['foo'] = 'bar'; 67 $_SESSION['bar'] = 'foo'; 68 unset($_SESSION['foo']); 69 $sessionData = session_encode(); 93 mysqli_free_result($result); 94 unset($thisSessData, $result); 70 95 71 // end of session 72 session_write_close(); 96 // sessionRead() 97 try 98 { 99 $retrieved_data = $storage->sessionRead($session_id); 100 $t->pass('sessionRead() does not throw an exception'); 101 } 102 catch (Exception $e) 103 { 104 $t->fail('sessionRead() does not throw an exception'); 105 } 106 $t->is($retrieved_data, $session_data, 'sessionRead() reads session data'); 73 107 74 // check session data in the database 75 $result = mysqli_query($database->getResource(), sprintf('SELECT sess_data FROM session WHERE sess_id = "%s"', $sessionId)); 76 list($thisSessData) = mysqli_fetch_row($result); 77 $t->is(mysqli_num_rows($result), 1, 'session is stored in the database'); 78 $t->is($thisSessData, $sessionData, 'session variables are stored in the database'); 108 // sessionWrite() 109 $_SESSION['baz'] = 'woo'; 110 $session_data = session_encode(); 111 try 112 { 113 $write = $storage->sessionWrite($session_id, $session_data); 114 $t->pass('sessionWrite() does not throw an exception'); 115 } 116 catch (Exception $e) 117 { 118 $t->fail('sessionWrite() does not throw an exception'); 119 } 79 120 80 mysqli_free_result($result);81 unset($thisSessData, $result);121 $t->ok($write, 'sessionWrite() returns true'); 122 $t->is($storage->sessionRead($session_id), $session_data, 'sessionWrite() wrote session data'); 82 123 83 // destroy the session 84 $storage->sessionDestroy($sessionId); 85 $result = mysqli_query($database->getResource(), sprintf('SELECT COUNT(sess_id) FROM session WHERE sess_id = "%s"', $sessionId)); 124 // sessionDestroy() 125 try 126 { 127 $storage->sessionDestroy($session_id); 128 $t->pass('sessionDestroy() does not throw an exception'); 129 } 130 catch (Exception $e) 131 { 132 $t->fail('sessionDestroy() does not throw an exception'); 133 } 86 134 87 list($count) = mysqli_fetch_row($result); 88 $t->is($count, 0, 'session is removed from the database'); 135 $result = mysqli_query($connection, sprintf('SELECT COUNT(sess_id) FROM session WHERE sess_id = "%s"', $session_id)); 89 136 90 mysqli_free_result($result);91 unset($count, $result);137 list($count) = mysqli_fetch_row($result); 138 $t->is($count, 0, 'session is removed from the database'); 92 139 93 mysqli_query($database->getResource(), 'DROP DATABASE ' . $sfTestMysqlSessionStorage_DatabaseName); 140 mysqli_free_result($result); 141 unset($count, $result); 94 142 95 // shutdown the storage 96 $storage->shutdown(); 143 mysqli_query($connection, 'DROP DATABASE sf_mysqli_storage_unit_test'); 97 144 98 // shutdown the database99 $database->shutdown();145 // shutdown the storage 146 $storage->shutdown(); 100 147 101 unset($sfTestMysqlSessionStorage_DatabaseName, $sfTestMysqlSessionStorage_MysqlParameters); 102 } 103 else 104 { 105 $t->skip('Mysql credentials needed to run these tests', 5); 106 } 148 // shutdown the database 149 $database->shutdown(); 150 151 unset($mysqli_config); branches/1.1/test/unit/storage/sfPDOSessionStorageTest.php
r9662 r10576 12 12 13 13 ob_start(); 14 $t = new lime_test( 5, new lime_output_color());14 $t = new lime_test(13, new lime_output_color()); 15 15 16 16 if (!extension_loaded('SQLite')) 17 17 { 18 18 $t->skip('SQLite needed to run these tests', 5); 19 exit(0); 19 20 } 20 else 21 22 // initialize the storage 23 $database = new sfPDODatabase(array('dsn' => 'sqlite::memory:')); 24 $connection = $database->getConnection(); 25 $connection->exec('CREATE TABLE session (sess_id, sess_data, sess_time)'); 26 27 ini_set('session.use_cookies', 0); 28 $session_id = "1"; 29 30 $storage = new sfPDOSessionStorage(array('db_table' => 'session', 'session_id' => $session_id, 'database' => $database)); 31 $t->ok($storage instanceof sfStorage, 'sfPDOSessionStorage is an instance of sfStorage'); 32 $t->ok($storage instanceof sfDatabaseSessionStorage, 'sfPDOSessionStorage is an instance of sfDatabaseSessionStorage'); 33 34 // regenerate() 35 $storage->regenerate(false); 36 $t->isnt(session_id(), $session_id, 'regenerate() regenerated the session id'); 37 $session_id = session_id(); 38 39 // do some session operations 40 $_SESSION['foo'] = 'bar'; 41 $_SESSION['bar'] = 'foo'; 42 unset($_SESSION['foo']); 43 $session_data = session_encode(); 44 45 // end of session 46 session_write_close(); 47 48 // check session data in the database 49 $result = $connection->query(sprintf('SELECT sess_id, sess_data FROM session WHERE sess_id = "%s"', $session_id)); 50 $data = $result->fetchAll(); 51 $t->is(count($data), 1, 'session is stored in the database'); 52 $t->is($data[0]['sess_data'], $session_data, 'session variables are stored in the database'); 53 54 // sessionRead() 55 try 21 56 { 22 // initialize the storage 23 $database = new sfPDODatabase(array('dsn' => 'sqlite::memory:')); 24 $connection = $database->getConnection(); 25 $connection->exec('CREATE TABLE session (sess_id, sess_data, sess_time)'); 57 $retrieved_data = $storage->sessionRead($session_id); 58 $t->pass('sessionRead() does not throw an exception'); 59 } 60 catch (Exception $e) 61 { 62 $t->fail('sessionRead() does not throw an exception'); 63 } 64 $t->is($retrieved_data, $session_data, 'sessionRead() reads session data'); 26 65 27 ini_set('session.use_cookies', 0); 28 $session_id = "1"; 66 // sessionWrite() 67 $_SESSION['baz'] = 'woo'; 68 $session_data = session_encode(); 69 try 70 { 71 $write = $storage->sessionWrite($session_id, $session_data); 72 $t->pass('sessionWrite() does not throw an exception'); 73 } 74 catch (Exception $e) 75 { 76 $t->fail('sessionWrite() does not throw an exception'); 77 } 29 78 30 $storage = new sfPDOSessionStorage(array('db_table' => 'session', 'session_id' => $session_id, 'database' => $database)); 31 $t->ok($storage instanceof sfStorage, 'sfPDOSessionStorage is an instance of sfStorage'); 32 $t->ok($storage instanceof sfDatabaseSessionStorage, 'sfPDOSessionStorage is an instance of sfDatabaseSessionStorage'); 79 $t->ok($write, 'sessionWrite() returns true'); 80 $t->is($storage->sessionRead($session_id), $session_data, 'sessionWrite() wrote session data'); 33 81 34 // do some session operations 35 $_SESSION['foo'] = 'bar'; 36 $_SESSION['bar'] = 'foo'; 37 unset($_SESSION['foo']); 38 $session_data = session_encode(); 82 // sessionGC() 83 try 84 { 85 $storage->sessionGC(0); 86 $t->pass('sessionGC() does not throw an exception'); 87 } 88 catch (Exception $e) 89 { 90 $t->fail('sessionGC() does not throw an exception'); 91 } 39 92 40 // end of session 41 session_write_close(); 93 // destroy the session 94 try 95 { 96 $storage->sessionDestroy($session_id); 97 $t->pass('sessionDestroy() does not throw an exception'); 98 } 99 catch (Exception $e) 100 { 101 $t->fail('sessionClose() does not throw an exception'); 102 } 103 $result = $connection->query(sprintf('SELECT sess_id, sess_data FROM session WHERE sess_id = "%s"', $session_id)); 104 $data = $result->fetchAll(); 105 $t->is(count($data), 0, 'session is removed from the database'); 42 106 43 // check session data in the database 44 $result = $connection->query(sprintf('SELECT sess_id, sess_data FROM session WHERE sess_id = "%s"', $session_id)); 45 $data = $result->fetchAll(); 46 $t->is(count($data), 1, 'session is stored in the database'); 47 $t->is($data[0]['sess_data'], $session_data, 'session variables are stored in the database'); 48 49 // destroy the session 50 $storage->sessionDestroy($session_id); 51 $result = $connection->query(sprintf('SELECT sess_id, sess_data FROM session WHERE sess_id = "%s"', $session_id)); 52 $data = $result->fetchAll(); 53 $t->is(count($data), 0, 'session is removed from the database'); 54 55 // shutdown the storage 56 $storage->shutdown(); 57 } 107 // shutdown the storage 108 $storage->shutdown();

