SessionTestHelper.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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-2015 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. /**
  23. * @see Zend_Session
  24. */
  25. require_once 'Zend/Session.php';
  26. /**
  27. * @category Zend
  28. * @package Zend_Session
  29. * @subpackage UnitTests
  30. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. * @group Zend_Session
  33. */
  34. class Zend_Session_TestHelper
  35. {
  36. /**
  37. * Runs the test method specified via command line arguments.
  38. *
  39. * @param array $argv
  40. * @return integer
  41. */
  42. public function run(array $argv)
  43. {
  44. if (!isset($argv[0]) || !isset($argv[1])) {
  45. echo "Usage: {$argv[0]} <test name>\n";
  46. return 1;
  47. }
  48. $testMethod = 'do' . ucfirst($argv[1]);
  49. if (!method_exists($this, $testMethod)) {
  50. echo "Invalid test: '{$argv[1]}'\n";
  51. return 2;
  52. }
  53. array_shift($argv);
  54. array_shift($argv);
  55. return $this->$testMethod($argv);
  56. }
  57. /**
  58. * @param array $args
  59. * @return integer Always returns zero.
  60. */
  61. public function doExpireAll(array $args)
  62. {
  63. Zend_Session::setOptions(array('remember_me_seconds' => 15, 'gc_probability' => 2));
  64. session_id($args[0]);
  65. if (isset($args[1]) && !empty($args[1])) {
  66. $s = new Zend_Session_Namespace($args[1]);
  67. }
  68. else {
  69. $s = new Zend_Session_Namespace();
  70. }
  71. if (isset($args[2]) && ($args[2] == 'ZF-7196')) {
  72. unset($s->foo);
  73. }
  74. $result = '';
  75. foreach ($s->getIterator() as $key => $val) {
  76. $result .= "$key === $val;";
  77. }
  78. Zend_Session::expireSessionCookie();
  79. Zend_Session::writeClose();
  80. echo $result;
  81. return 0;
  82. }
  83. /**
  84. * @param array $args
  85. * @return integer Always returns zero.
  86. */
  87. public function doSetArray(array $args)
  88. {
  89. $GLOBALS['fpc'] = 'set';
  90. session_id($args[0]);
  91. $s = new Zend_Session_Namespace($args[1]);
  92. array_shift($args);
  93. $s->astring = 'happy';
  94. // works, even for broken versions of PHP
  95. // $s->someArray = array( & $args ) ;
  96. // $args['OOOOOOOOOOOOOOOO'] = 'YYYYYYYYYYYYYYYYYYYYYYYYYYYYY';
  97. $s->someArray = $args;
  98. $s->someArray['bee'] = 'honey'; // Repeating this line twice "solves" the problem for some versions of PHP,
  99. $s->someArray['bee'] = 'honey'; // but PHP 5.2.1 has the real fix for ZF-800.
  100. $s->someArray['ant'] = 'sugar';
  101. $s->someArray['dog'] = 'cat';
  102. // file_put_contents('out.sessiontest.set', (str_replace(array("\n", ' '),array(';',''), print_r($_SESSION, true))) );
  103. $s->serializedArray = serialize($args);
  104. $result = '';
  105. foreach ($s->getIterator() as $key => $val) {
  106. $result .= "$key === ". (print_r($val,true)) .';';
  107. }
  108. Zend_Session::writeClose();
  109. return 0;
  110. }
  111. /**
  112. * @param array $args
  113. * @return integer Always returns zero.
  114. */
  115. public function doGetArray(array $args)
  116. {
  117. $GLOBALS['fpc'] = 'get';
  118. session_id($args[0]);
  119. if (isset($args[1]) && !empty($args[1])) {
  120. $s = new Zend_Session_Namespace($args[1]);
  121. }
  122. else {
  123. $s = new Zend_Session_Namespace();
  124. }
  125. $result = '';
  126. foreach ($s->getIterator() as $key => $val) {
  127. $result .= "$key === ". (str_replace(array("\n", ' '),array(';',''), print_r($val, true))) .';';
  128. }
  129. // file_put_contents('out.sesstiontest.get', print_r($s->someArray, true));
  130. Zend_Session::writeClose();
  131. echo $result;
  132. return 0;
  133. }
  134. }
  135. $testHelper = new Zend_Session_TestHelper();
  136. exit($testHelper->run($argv));