Skip to content

Commit

Permalink
Fixes for Session tests
Browse files Browse the repository at this point in the history
- Moved test autoload setup to tests/_autoload.php
- Modified Session\ContainerTest and Session\SessionManagerTest to include
  _autoload.php if no autoloader is detected (happens when global state is
  destroyed).
  • Loading branch information
weierophinney committed Aug 20, 2010
1 parent 458136a commit ea69331
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 53 deletions.
54 changes: 1 addition & 53 deletions tests/Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,59 +49,7 @@
/**
* Setup autoloading
*/
function ZendTest_Autoloader($class)
{
$class = ltrim($class, '\\');

if (!preg_match('#^(Zend(Test)?|PHPUnit)(\\\\|_)#', $class)) {
return false;
}

// $segments = explode('\\', $class); // preg_split('#\\\\|_#', $class);//
$segments = preg_split('#[\\\\_]#', $class); // preg_split('#\\\\|_#', $class);//
$ns = array_shift($segments);

switch ($ns) {
case 'Zend':
$file = dirname(__DIR__) . '/library/Zend/';
break;
case 'ZendTest':
// temporary fix for ZendTest namespace until we can migrate files
// into ZendTest dir
$file = __DIR__ . '/Zend/';
break;
default:
$file = false;
break;
}

if ($file) {
$file .= implode('/', $segments) . '.php';
if (file_exists($file)) {
return include_once $file;
}
}

$segments = explode('_', $class);
$ns = array_shift($segments);

switch ($ns) {
case 'PHPUnit':
return include_once str_replace('_', DIRECTORY_SEPARATOR, $class) . '.php';
case 'Zend':
$file = dirname(__DIR__) . '/library/Zend/';
break;
default:
return false;
}
$file .= implode('/', $segments) . '.php';
if (file_exists($file)) {
return include_once $file;
}

return false;
}
spl_autoload_register('ZendTest_Autoloader', true, true);
include __DIR__ . '/_autoload.php';

/*
* Load the user-defined test configuration file, if it exists; otherwise, load
Expand Down
9 changes: 9 additions & 0 deletions tests/Zend/Session/ContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class ContainerTest extends \PHPUnit_Framework_TestCase
{
public function setUp()
{
$this->forceAutoloader();
$_SESSION = array();
Container::setDefaultManager(null);
$this->manager = $manager = new TestAsset\TestManager(array(
Expand All @@ -25,6 +26,14 @@ public function tearDown()
Container::setDefaultManager(null);
}

protected function forceAutoloader()
{
$splAutoloadFunctions = spl_autoload_functions();
if (!$splAutoloadFunctions || !in_array('ZendTest_Autoloader', $splAutoloadFunctions)) {
include __DIR__ . '/../../_autoload.php';
}
}

/**
* Hack to allow running tests in separate processes
*
Expand Down
9 changes: 9 additions & 0 deletions tests/Zend/Session/SessionManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,20 @@ class SessionManagerTest extends \PHPUnit_Framework_TestCase

public function setUp()
{
$this->forceAutoloader();
$this->error = false;
$this->manager = new SessionManager();
Registry::_unsetInstance();
}

protected function forceAutoloader()
{
$splAutoloadFunctions = spl_autoload_functions();
if (!$splAutoloadFunctions || !in_array('ZendTest_Autoloader', $splAutoloadFunctions)) {
include __DIR__ . '/../../_autoload.php';
}
}

/**
* Hack to allow running tests in separate processes
*
Expand Down
58 changes: 58 additions & 0 deletions tests/_autoload.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php
/**
* Setup autoloading
*/
function ZendTest_Autoloader($class)
{
$class = ltrim($class, '\\');

if (!preg_match('#^(Zend(Test)?|PHPUnit)(\\\\|_)#', $class)) {
return false;
}

// $segments = explode('\\', $class); // preg_split('#\\\\|_#', $class);//
$segments = preg_split('#[\\\\_]#', $class); // preg_split('#\\\\|_#', $class);//
$ns = array_shift($segments);

switch ($ns) {
case 'Zend':
$file = dirname(__DIR__) . '/library/Zend/';
break;
case 'ZendTest':
// temporary fix for ZendTest namespace until we can migrate files
// into ZendTest dir
$file = __DIR__ . '/Zend/';
break;
default:
$file = false;
break;
}

if ($file) {
$file .= implode('/', $segments) . '.php';
if (file_exists($file)) {
return include_once $file;
}
}

$segments = explode('_', $class);
$ns = array_shift($segments);

switch ($ns) {
case 'PHPUnit':
return include_once str_replace('_', DIRECTORY_SEPARATOR, $class) . '.php';
case 'Zend':
$file = dirname(__DIR__) . '/library/Zend/';
break;
default:
return false;
}
$file .= implode('/', $segments) . '.php';
if (file_exists($file)) {
return include_once $file;
}

return false;
}
spl_autoload_register('ZendTest_Autoloader', true, true);

0 comments on commit ea69331

Please sign in to comment.