【问题标题】:phpunit cannot find app classes in Yii2phpunit 在 Yii2 中找不到应用程序类
【发布时间】:2014-03-21 20:28:33
【问题描述】:

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

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

我使用 Pear 安装 PHPUnit,按照manual 中的说明进行操作。我的操作系统是 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"

【问题讨论】:

  • 您是否尝试使用 Codeception 运行测试?
  • 我也有密码接收问题。我的 codecept.phar 文件位于我的网络根目录中。当我进入我的应用程序目录(位于 web 根目录下)并运行 php /../codecept.phar build 时,我收到“找不到输入文件”的错误消息。我的 composer.phar 文件位于我的网络根目录中。

标签: php yii phpunit pear yii2


【解决方案1】:
  1. 使用 composer 安装 codeceptor(在 yii2 app root run php composer.phar require "codeception/codeception:*"
  2. 在应用根目录下运行vendor/bin/codecept build

【讨论】:

  • 谢谢阿列克谢!事实证明,在尝试安装代码接收时,我没有对错误消息给予足够的关注。我最近更新了 php,我猜我的 CURL 扩展丢失了,所以很多依赖项都没有安装。您的回答帮助我立即解决了问题。只是一个方面 - 我专门安装了 codeception: 1.8.3,它立即开始对我使用当前的 Yii2 更顺畅
猜你喜欢
  • 1970-01-01
  • 2015-08-31
  • 1970-01-01
  • 1970-01-01
  • 2019-07-22
  • 2017-06-29
  • 1970-01-01
  • 2012-04-28
  • 2017-02-24
相关资源
最近更新 更多