2
0

SessionTestHelper.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Session
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /** Test helper */
  23. // require_once dirname(dirname(dirname(__FILE__))) . DIRECTORY_SEPARATOR . 'TestHelper.php';
  24. // Do not include TestHelper.php since it takes too much time
  25. // Directly include part of it
  26. $zfRoot = dirname(dirname(dirname(dirname(__FILE__))));
  27. $zfCoreLibrary = "$zfRoot/library";
  28. $zfCoreTests = "$zfRoot/tests";
  29. $path = array(
  30. $zfCoreLibrary,
  31. $zfCoreTests,
  32. get_include_path()
  33. );
  34. set_include_path(implode(PATH_SEPARATOR, $path));
  35. /**
  36. * @see Zend_Session
  37. */
  38. require_once 'Zend/Session.php';
  39. /**
  40. * @category Zend
  41. * @package Zend_Session
  42. * @subpackage UnitTests
  43. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  44. * @license http://framework.zend.com/license/new-bsd New BSD License
  45. * @group Zend_Session
  46. */
  47. class Zend_Session_TestHelper
  48. {
  49. /**
  50. * Runs the test method specified via command line arguments.
  51. *
  52. * @param array $argv
  53. * @return integer
  54. */
  55. public function run(array $argv)
  56. {
  57. if (!isset($argv[0]) || !isset($argv[1])) {
  58. echo "Usage: {$argv[0]} <test name>\n";
  59. return 1;
  60. }
  61. $testMethod = 'do' . ucfirst($argv[1]);
  62. if (!method_exists($this, $testMethod)) {
  63. echo "Invalid test: '{$argv[1]}'\n";
  64. return 2;
  65. }
  66. array_shift($argv);
  67. array_shift($argv);
  68. return $this->$testMethod($argv);
  69. }
  70. /**
  71. * @param array $args
  72. * @return integer Always returns zero.
  73. */
  74. public function doExpireAll(array $args)
  75. {
  76. Zend_Session::setOptions(array('remember_me_seconds' => 15, 'gc_probability' => 2));
  77. session_id($args[0]);
  78. if (isset($args[1]) && !empty($args[1])) {
  79. $s = new Zend_Session_Namespace($args[1]);
  80. }
  81. else {
  82. $s = new Zend_Session_Namespace();
  83. }
  84. $result = '';
  85. foreach ($s->getIterator() as $key => $val) {
  86. $result .= "$key === $val;";
  87. }
  88. Zend_Session::expireSessionCookie();
  89. Zend_Session::writeClose();
  90. echo $result;
  91. return 0;
  92. }
  93. /**
  94. * @param array $args
  95. * @return integer Always returns zero.
  96. */
  97. public function doSetArray(array $args)
  98. {
  99. $GLOBALS['fpc'] = 'set';
  100. session_id($args[0]);
  101. $s = new Zend_Session_Namespace($args[1]);
  102. array_shift($args);
  103. $s->astring = 'happy';
  104. // works, even for broken versions of PHP
  105. // $s->someArray = array( & $args ) ;
  106. // $args['OOOOOOOOOOOOOOOO'] = 'YYYYYYYYYYYYYYYYYYYYYYYYYYYYY';
  107. $s->someArray = $args;
  108. $s->someArray['bee'] = 'honey'; // Repeating this line twice "solves" the problem for some versions of PHP,
  109. $s->someArray['bee'] = 'honey'; // but PHP 5.2.1 has the real fix for ZF-800.
  110. $s->someArray['ant'] = 'sugar';
  111. $s->someArray['dog'] = 'cat';
  112. // file_put_contents('out.sessiontest.set', (str_replace(array("\n", ' '),array(';',''), print_r($_SESSION, true))) );
  113. $s->serializedArray = serialize($args);
  114. $result = '';
  115. foreach ($s->getIterator() as $key => $val) {
  116. $result .= "$key === ". (print_r($val,true)) .';';
  117. }
  118. Zend_Session::writeClose();
  119. return 0;
  120. }
  121. /**
  122. * @param array $args
  123. * @return integer Always returns zero.
  124. */
  125. public function doGetArray(array $args)
  126. {
  127. $GLOBALS['fpc'] = 'get';
  128. session_id($args[0]);
  129. if (isset($args[1]) && !empty($args[1])) {
  130. $s = new Zend_Session_Namespace($args[1]);
  131. }
  132. else {
  133. $s = new Zend_Session_Namespace();
  134. }
  135. $result = '';
  136. foreach ($s->getIterator() as $key => $val) {
  137. $result .= "$key === ". (str_replace(array("\n", ' '),array(';',''), print_r($val, true))) .';';
  138. }
  139. // file_put_contents('out.sesstiontest.get', print_r($s->someArray, true));
  140. Zend_Session::writeClose();
  141. echo $result;
  142. return 0;
  143. }
  144. }
  145. $testHelper = new Zend_Session_TestHelper();
  146. exit($testHelper->run($argv));