Phpunit cannot find application classes in Yii2

Trying to configure phpunit using Yii2 framework. I use Composer for dependency management

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

I used Pear to install PHPUnit following the instructions in manual . My OS is ubuntu 12.04. I work with the main file structure of the application:

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

I run UserTest from / tests / unit / models, and I get a fatal PHP error:

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

My test looks like this. Note that I tried to enable the class directly to no avail.

<?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);
 }*/
}
?>

I tried updating Pear, reinstalling PHPUnit and checking the paths in php.ini. My way looks like

include_path = ".:/usr/share/php:~/.composer/vendor/bin/:/usr/share/php/PHPunit:/usr/share/php/PEAR"
+3
source share
1 answer
  • Set the receptor code using composer (in the yii2 root run application php composer.phar require "codeception/codeception:*")
  • vendor/bin/codecept build
0

All Articles