phpunit在Yii2中找不到应用类

时间:2014-02-20 17:43:37

标签: php yii phpunit pear yii2

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

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

我按照manual中的说明使用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"

1 个答案:

答案 0 :(得分:0)

  1. 使用composer安装codeceptor(在yii2 app root run php composer.phar require "codeception/codeception:*"
  2. 在app root run vendor/bin/codecept build