0

我有一个非常简单的类的小单元测试。这是我要测试的课程:

<?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

4

1 回答 1

0

好的,所以这是一个与 phpunit 脚本中引用的 PHP 版本相关的愚蠢问题。在 /usr/bin/phpunit 的顶部,路径是指 PHP 5.3.*,它显然不支持 SessionHandlerInterface。所以我只需要将该路径更改为正确的 PHP 版本,现在就可以使用了。

于 2013-04-05T09:47:23.580 回答