PageRenderTime 39ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/symfony/lib/vendor/symfony/test/unit/storage/sfMySQLStorageTest.php

https://bitbucket.org/wildanm/orangehrm
PHP | 149 lines | 106 code | 25 blank | 18 comment | 2 complexity | 607ca121712897621189ba6cc5d30cc7 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, AGPL-3.0, BSD-3-Clause, AGPL-1.0, GPL-2.0, LGPL-2.1, LGPL-3.0
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
  5. * (c) 2008 Dejan Spasic <spasic.dejan@yahoo.de>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. require_once(dirname(__FILE__).'/../../bootstrap/unit.php');
  11. ob_start();
  12. $plan = 16;
  13. $t = new lime_test($plan, new lime_output_color());
  14. if (!extension_loaded('mysql'))
  15. {
  16. $t->skip('Mysql extension must be loaded', $plan);
  17. exit(0);
  18. }
  19. // Configure your database with the settings below in order to run the test
  20. $mysql_config = array(
  21. 'host' => 'localhost',
  22. 'username' => 'root',
  23. 'password' => '',
  24. );
  25. if (!isset($mysql_config))
  26. {
  27. $t->skip('Mysql credentials needed to run these tests', $plan);
  28. exit(0);
  29. }
  30. try
  31. {
  32. // Creating mysql database connection
  33. $database = new sfMySQLDatabase($mysql_config);
  34. $connection = $database->getResource();
  35. }
  36. catch (sfDatabaseException $e)
  37. {
  38. $t->diag($e->getMessage());
  39. $t->skip('Unable to connect to MySQL database, skipping', $plan);
  40. exit(0);
  41. }
  42. // Creates test database
  43. mysql_query('DROP DATABASE IF EXISTS sf_mysql_storage_unit_test', $connection);
  44. mysql_query('CREATE DATABASE sf_mysql_storage_unit_test', $connection) or $t->fail('Cannot create database sf_mysql_storage_unit_test');
  45. mysql_select_db('sf_mysql_storage_unit_test', $connection);
  46. mysql_query("CREATE TABLE `session` (
  47. `sess_id` varchar(40) NOT NULL PRIMARY KEY,
  48. `sess_time` int(10) unsigned NOT NULL default '0',
  49. `sess_data` text collate utf8_unicode_ci
  50. ) ENGINE=MyISAM", $connection)
  51. or $t->fail('Can not create table session');
  52. ini_set('session.use_cookies', 0);
  53. $session_id = "1";
  54. $storage = new sfMySQLSessionStorage(array(
  55. 'db_table' => 'session',
  56. 'session_id' => $session_id,
  57. 'database' => $database)
  58. );
  59. $t->ok($storage instanceof sfStorage, 'sfMySQLSessionStorage is an instance of sfStorage');
  60. $t->ok($storage instanceof sfDatabaseSessionStorage, 'sfMySQLSessionStorage is an instance of sfDatabaseSessionStorage');
  61. // regenerate()
  62. $oldSessionData = 'foo:bar';
  63. $storage->sessionWrite($session_id, $oldSessionData);
  64. $storage->regenerate(false);
  65. $newSessionData = 'foo:bar:baz';
  66. $storage->sessionWrite(session_id(), $newSessionData);
  67. $t->isnt(session_id(), $session_id, 'regenerate() regenerated the session with a different session id');
  68. // checking if the old session record still exists
  69. $result = mysql_query(sprintf('SELECT sess_data FROM session WHERE sess_id = "%s"', $session_id), $connection);
  70. $t->is(mysql_num_rows($result), 1, 'regenerate() has kept destroyed old session');
  71. $rSessionData = list($thisSessData) = mysql_fetch_row($result);
  72. $t->is($rSessionData[0], $oldSessionData, 'regenerate() has kept destroyed old session data');
  73. // checking if the new session record has been created
  74. $result = mysql_query(sprintf('SELECT sess_data FROM session WHERE sess_id = "%s"', session_id()), $connection);
  75. $t->is(mysql_num_rows($result), 1, 'regenerate() has created a new session record');
  76. $rSessionData = list($thisSessData) = mysql_fetch_row($result);
  77. $t->is($rSessionData[0], $newSessionData, 'regenerate() has created a new record with correct data');
  78. $session_id = session_id();
  79. // check session data in the database
  80. $result = mysql_query(sprintf('SELECT sess_data FROM session WHERE sess_id = "%s"', $session_id), $connection);
  81. list($thisSessData) = mysql_fetch_row($result);
  82. $t->is(mysql_num_rows($result), 1, 'session is stored in the database');
  83. $t->is($thisSessData, $newSessionData, 'session variables are stored in the database');
  84. mysql_free_result($result);
  85. unset($thisSessData, $result);
  86. // sessionRead()
  87. try
  88. {
  89. $retrieved_data = $storage->sessionRead($session_id);
  90. $t->pass('sessionRead() does not throw an exception');
  91. }
  92. catch (Exception $e)
  93. {
  94. $t->fail('sessionRead() does not throw an exception');
  95. }
  96. $t->is($retrieved_data, $newSessionData, 'sessionRead() reads session data');
  97. // sessionWrite()
  98. $otherSessionData = 'foo:foo:foo';
  99. try
  100. {
  101. $write = $storage->sessionWrite($session_id, $otherSessionData);
  102. $t->pass('sessionWrite() does not throw an exception');
  103. }
  104. catch (Exception $e)
  105. {
  106. $t->fail('sessionWrite() does not throw an exception');
  107. }
  108. $t->ok($write, 'sessionWrite() returns true');
  109. $t->is($storage->sessionRead($session_id), $otherSessionData, 'sessionWrite() wrote session data');
  110. // sessionDestroy()
  111. try
  112. {
  113. $storage->sessionDestroy($session_id);
  114. $t->pass('sessionDestroy() does not throw an exception');
  115. }
  116. catch (Exception $e)
  117. {
  118. $t->fail('sessionDestroy() does not throw an exception');
  119. }
  120. $result = mysql_query(sprintf('SELECT COUNT(sess_id) FROM session WHERE sess_id = "%s"', $session_id), $connection);
  121. list($count) = mysql_fetch_row($result);
  122. $t->is($count, 0, 'session is removed from the database');
  123. mysql_free_result($result);
  124. unset($count, $result);