我有一个非常简单的类的小单元测试。下面是我要测试的类:
<?php
namespace MyApp'sys;
class Auth
{
// ...
public static function getInstance()
{
if(!isset(self::$_instance))
self::$_instance = new Auth();
return self::$_instance;
}
public function authenticate($sMethod, $sData, $sAuth)
{
// ...
return $this->authSession();
}
public function authSession()
{
// ...
$oHandler = new SessionHandler();
}
}
下面是SessionHandler类:
<?php
namespace MyApp'sys;
class SessionHandler implements 'SessionHandlerInterface
{
public function open($sSavePath, $sSessionId)
{
// ...
}
public function close()
{
// ...
}
public function read($sSessionId)
{
// ...
}
public function write($sSessionId, $sSessionData)
{
// ...
}
public function destroy($sSessionId)
{
// ...
}
public function gc($iMaxLifetime)
{
// ...
}
}
下面是一个简单的单元测试:
<?php
namespace Test'MyApp'sys;
use PHPUnit_Framework_TestCase;
class AuthTest extends PHPUnit_Framework_TestCase
{
protected function setUp()
{
parent::setUp();
}
protected function tearDown()
{
parent::tearDown();
}
public function testGetInstance()
{
$oAuth = 'MyApp'sys'Auth::getInstance();
$this->assertInstanceOf(''MyApp'sys'Auth', $oAuth);
return $oAuth;
}
/**
* @depends testGetInstance
*/
public function testAuthenticate('MyApp'sys'Auth $oAuth)
{
$res = $oAuth->authenticate(null, null, null);
}
}
我有以下phpunit.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
bootstrap="bootstrap.php"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
stopOnError="true"
stopOnFailure="true"
strict="false"
verbose="true">
<testsuites>
<testsuite name="Authentication">
<directory suffix="Test.php">
./MyApp/sys/
</directory>
</testsuite>
</testsuites>
</phpunit>
…以及下面的bootstrap.php文件:
<?php
define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../'));
define('APPLICATION_TEST_PATH', realpath(dirname(__FILE__)));
spl_autoload_register(function($sClas) {
$nClass = str_replace("''", "/", $sClas);
require_once APPLICATION_PATH . '/' . $nClass . ".php";
});
目录结构如下:
- MyProject/
- MyApp/
- sys/
. Auth.php
. SessionHandler.php
- Test/
- MyApp/
- sys/
. AuthTest.php
. bootstrap.php
. phpunit.xml
这是我尝试从命令行运行这个小单元测试的方式(以及我得到的结果):
# phpunit MyApp/sys/AuthTest.php
PHPUnit 3.7.19 by Sebastian Bergmann.
Configuration read from /Users/pmpro/Code/Snevens/MyProject/Tests/phpunit.xml
.PHP Fatal error: {closure}(): Failed opening required '/Users/pmpro/Code/Snevens/MyProject/SessionHandlerInterface.php' (include_path='.:/usr/share/pear') in /Users/pmpro/Code/Snevens/MyProject/Tests/bootstrap.php on line 7
Fatal error: {closure}(): Failed opening required '/Users/pmpro/Code/Snevens/MyProject/SessionHandlerInterface.php' (include_path='.:/usr/share/pear') in /Users/pmpro/Code/Snevens/MyProject/Tests/bootstrap.php on line 7
我认为这将是注册自动加载函数的问题,所以我决定跳过类自动加载,以防它是SessionHandlerInterface,只是为了看看会发生什么:
spl_autoload_register(function($sClas) {
if("SessionHandlerInterface" == $sClas)
return;
$nClass = str_replace("''", "/", $sClas);
require_once APPLICATION_PATH . '/' . $nClass . ".php";
});
,结果如下:
# phpunit MyApp/sys/AuthTest.php
PHPUnit 3.7.19 by Sebastian Bergmann.
Configuration read from /Users/pmpro/Code/Snevens/MyProject/Tests/phpunit.xml
.PHP Fatal error: Interface 'SessionHandlerInterface' not found in /Users/pmpro/Code/Snevens/MyProject/MyApp/sys/SessionHandler.php on line 9
Fatal error: Interface 'SessionHandlerInterface' not found in /Users/pmpro/Code/Snevens/MyProject/MyApp/sys/SessionHandler.php on line 9
如果我直接从命令行调用SessionHandler.php,它不会返回错误:
# php SessionHandler.php
(returns nothing)
我做错了什么或者我错过了什么?
这只发生在SessionHandlerInterface中。我试着只运行testGetInstance,使Auth类实现ArrayAccess,例如,它根本没有抱怨。如果我让Auth类实现SessionHandlerInterface,它会立即报错。
我正在运行PHP 5.4.13和PHP Unit 3.7.19
好吧,所以这是一个愚蠢的问题,与PHP版本被引用在phpunit脚本。在/usr/bin/phpunit的顶部,路径指向PHP 5.3。*显然不支持SessionHandlerInterface。所以我只需要将路径更改为正确的PHP版本,现在就可以工作了。