SessionTestHelper.php 4.9 KB

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