3

尝试使用 Yii2 框架设置 phpunit。我正在使用 Composer 来管理依赖项

"require-dev": {
    "yiisoft/yii2-codeception": "*",
    "yiisoft/yii2-debug": "*",
    "yiisoft/yii2-gii": "*",
    "phpunit/phpunit": "3.7.*"
},

我按照手册中的说明使用 Pear 安装 PHPUnit 。我的操作系统是 ubuntu 12.04。我正在使用基本的应用程序文件结构:

-app 
   -tests
      -unit
         -models
            -UserTest.php
   -models
      -User.php

我从 /tests/unit/models 中运行 UserTest,我得到一个 PHP 致命错误:

PHP Fatal error:  Class 'User' not found
PHP Stack trace:
PHP   1. {main}() /usr/local/bin/phpunit:0
PHP   2. PHPUnit_TextUI_Command::main() /usr/local/bin/phpunit:615
PHP   3. PHPUnit_TextUI_Command->run()     
phar:///usr/local/bin/phpunit/phpunit/TextUI/Command.php:129
PHP   4. PHPUnit_Runner_BaseTestRunner->getTest()    
phar:///usr/local/bin/phpunit/phpunit/TextUI/Command.php:150
PHP   5. PHPUnit_Runner_BaseTestRunner->loadSuiteClass() 
phar:///usr/local/bin/phpunit/phpunit/Runner/BaseTestRunner.php:104
PHP   6. PHPUnit_Runner_StandardTestSuiteLoader->load() 
phar:///usr/local/bin/phpunit/phpunit/Runner/BaseTestRunner.php:168
PHP   7. PHPUnit_Util_Fileloader::checkAndLoad() 
phar:///usr/local/bin/phpunit/phpunit/Runner/StandardTestSuiteLoader.php:77
PHP   8. PHPUnit_Util_Fileloader::load() 
phar:///usr/local/bin/phpunit/phpunit/Util/Fileloader.php:76
PHP   9. include_once() phar:///usr/local/bin/phpunit/phpunit/Util/Fileloader.php:92
PHP  10. require_once() /home/thomas/Documents/records/tests/unit/models/UserTest.php:4

我的测试看起来像这样。请注意,我尝试直接包含该课程无济于事。

<?php

//set_include_path(get_include_path() . PATH_SEPARATOR . getcwd/../../../);
//require_once('models/User.php');
//require_once('db/ActiveRecord.php');

class UserTest extends PHPUnit_Framework_TestCase
{

public function testForAttributeId() {
    $this-> assertClassHasAttribute('id', 'User');
}
public function testFindIdentity() {
    $userobj = new \app\models\User();
    $user = $userobj->findIdentity(hash('sha256', 871821997));
    $this-> assertContainsOnlyInstancesOf('User', $user);
    $this-> assertEquals($user->id, 871821997);
    $this-> assertEquals($user->name, 'Thomas Dressler');
}

/*  
 public function testEmpty()
 {
     $stack = array();
     $this->assertEmpty($stack);
     return $stack;
 }
 /**
 * @depends testEmpty
 *
 public function testPush(array $stack)
 {
     array_push($stack, 'foo');
     $this->assertEquals('foo', $stack[count($stack)-1]);
     $this->assertNotEmpty($stack);
     return $stack;
 }
 /**
 * @depends testPush
 *
 public function testPop(array $stack)
 {
     $this->assertEquals('foo', array_pop($stack));
     $this->assertEmpty($stack);
 }*/
}
?>

我尝试升级 Pear,重新安装 PHPUnit,并检查 php.ini 中的路径。我的路径看起来像

include_path = ".:/usr/share/php:~/.composer/vendor/bin/:/usr/share/php/PHPunit:/usr/share/php/PEAR"
4

1 回答 1

0
  1. 使用 composer 安装 codeceptor (在 yii2 app root run php composer.phar require "codeception/codeception:*")
  2. 在应用程序根运行vendor/bin/codecept build
于 2014-02-28T05:26:41.367 回答