为什么我会收到此错误?致命错误:类“TestCase”未找到

发布于 2024-12-07 10:45:29 字数 2457 浏览 0 评论 0原文

即使我已经包含了 TestCase 类?

    <?php

require_once("PHPUnit/Autoload.php");
require_once("PHPUnit/Framework/TestCase.php");
require_once("PHPUnit/Framework/TestSuite.php");

class WidgetSession {
    public function __construct($one, $two){}
    public function login() {}
    public function isLoggedIn() {return null;}
    public function getUser(){
        return new WidgetUser();
    }
}

class WidgetUser{
    public $first_name = "";
    public $last_name = "";
    public $email = "";

    public function isSalesPerson() {return null;}
    public function isSalesManager() {return null;}
}

class TestWidgetSession extends TestCase {

    private $_session;
    function setUp(){
        $dsn = array(
            'phptype' => "pgsql",
            'hostspec' => "localhost",
            'database' => "widgetworld",
            'username' => "wuser",
            'password' => "foobar"
        );
        $this->_session = new WidgetSession($dsn, true);
    }

    function testValidLogin(){
        $this->_session->login("ed", "12345");
        $this->assertEqual(true, $this->_session->isLoggedIn());
    }

    function testInvalidLogin(){
        $this->_session->login("ed", "54321"); //fail
        $this->assertEquals(false, $this->_session->isLoggedIn());
    }

    function testUser(){
        $user = $this->_session->getUser();
        $this->assertEquals("Lecky Thompson", $user->last_name);
        $this->assertEquals("Ed", $user->first_name);
        $this->assertEquals("[email protected]", $user->email);
    }

    function testAuthorization(){
        $user = $this->_session->getUser();
        $this->assertEquals("Sales Person", $user->role);
        $this->assertEquals(true, $user->isSalesPerson());
        $this->assertEquals(false, $user->isSalesManager());
        $this->assertEquals(false, $user->isAccountant());
    }
}

$suite = new TestSuite;
$suite->addTest(new TestWidgetSession("testValidLogin"));
$suite->addTest(new TestWidgetSession("testInvalidLogin"));
$suite->addTest(new TestWidgetSession("testUser"));
$suite->addTest(new TestWidgetSession("testAuthorization"));
$testRunner = new TestRunner();
$testRunner->run($suite);
?>

Even though I have got the TestCase class included?

    <?php

require_once("PHPUnit/Autoload.php");
require_once("PHPUnit/Framework/TestCase.php");
require_once("PHPUnit/Framework/TestSuite.php");

class WidgetSession {
    public function __construct($one, $two){}
    public function login() {}
    public function isLoggedIn() {return null;}
    public function getUser(){
        return new WidgetUser();
    }
}

class WidgetUser{
    public $first_name = "";
    public $last_name = "";
    public $email = "";

    public function isSalesPerson() {return null;}
    public function isSalesManager() {return null;}
}

class TestWidgetSession extends TestCase {

    private $_session;
    function setUp(){
        $dsn = array(
            'phptype' => "pgsql",
            'hostspec' => "localhost",
            'database' => "widgetworld",
            'username' => "wuser",
            'password' => "foobar"
        );
        $this->_session = new WidgetSession($dsn, true);
    }

    function testValidLogin(){
        $this->_session->login("ed", "12345");
        $this->assertEqual(true, $this->_session->isLoggedIn());
    }

    function testInvalidLogin(){
        $this->_session->login("ed", "54321"); //fail
        $this->assertEquals(false, $this->_session->isLoggedIn());
    }

    function testUser(){
        $user = $this->_session->getUser();
        $this->assertEquals("Lecky Thompson", $user->last_name);
        $this->assertEquals("Ed", $user->first_name);
        $this->assertEquals("[email protected]", $user->email);
    }

    function testAuthorization(){
        $user = $this->_session->getUser();
        $this->assertEquals("Sales Person", $user->role);
        $this->assertEquals(true, $user->isSalesPerson());
        $this->assertEquals(false, $user->isSalesManager());
        $this->assertEquals(false, $user->isAccountant());
    }
}

$suite = new TestSuite;
$suite->addTest(new TestWidgetSession("testValidLogin"));
$suite->addTest(new TestWidgetSession("testInvalidLogin"));
$suite->addTest(new TestWidgetSession("testUser"));
$suite->addTest(new TestWidgetSession("testAuthorization"));
$testRunner = new TestRunner();
$testRunner->run($suite);
?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云之铃。 2024-12-14 10:45:29
require_once("PHPUnit/Framework/TestCase.php");

该文件肯定不会包含 TestCase 的任何定义,但是 PHPUnit_Framework_TestCase。请小心并使用正确的类名。

我假设这同样适用于:

require_once("PHPUnit/Framework/TestSuite.php");

这是 PHPUnit_Framework_TestSuite 而不是 TestSuite

您只需要要求/包含自动加载程序,这对于 PHPUnit 的类来说就足够了。

require_once("PHPUnit/Framework/TestCase.php");

This file will most certainly not contain any definition of TestCase but PHPUnit_Framework_TestCase. Take care and use the right classname.

I assume the same applies to:

require_once("PHPUnit/Framework/TestSuite.php");

which is PHPUnit_Framework_TestSuite and not TestSuite.

And you only need to require/include the autloader, that's enough for PHPUnit's classes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文