Yii2: tests not finished Class 'Yii' not found

Created on 1 Nov 2016  ยท  11Comments  ยท  Source: yiisoft/yii2

  1. The wrong place is not in the test class I wrote myself, but in the Codecept.
    in phar:///home/jenkins/workspace/Xxxtrunk/build/tools/codecept.phar/src/Codeception/Lib/Connector/Yii2.php:71
  2. Environmental Science
Yii2 2.0.10
Codeception PHP Testing Framework v2.2.5
Powered by PHPUnit 5.5.5 by Sebastian Bergmann and contributors.
  1. UserTest
<?php
namespace tests\unit\models;

use app\models\User;
use Codeception\Test\Unit;

class UserTest extends Unit
{
    public function testFindUserById()
    {
        $this->assertNotEmpty($user = User::findIdentity(100));
        $this->assertEquals('admin', $user->username);

        $this->assertEmpty(User::findIdentity(999));
    }

    public function testFindUserByAccessToken()
    {
        $this->assertNotEmpty($user = User::findIdentityByAccessToken('100-token'));
        $this->assertEquals('admin', $user->username);

        $this->assertEmpty(User::findIdentityByAccessToken('non-existing'));
    }

    public function testFindUserByUsername()
    {
        $this->assertNotEmpty($user = User::findByUsername('admin'));
        $this->assertEmpty(User::findByUsername('not-admin'));
    }

    /**
     * @depends testFindUserByUsername
     */
    public function testValidateUser($user)
    {
        $user = User::findByUsername('admin');
        $this->assertNotEmpty($user->validateAuthKey('test100key'));
        $this->assertEmpty($user->validateAuthKey('test102key'));

        $this->assertNotEmpty($user->validatePassword('admin'));
        $this->assertEmpty($user->validatePassword('123456'));
    }

}

  1. directory structure
โ”œโ”€โ”€ config
โ”œโ”€โ”€ tests
    โ”œโ”€โ”€ acceptance
    โ”‚ย ย  โ””โ”€โ”€ _bootstrap.php
    โ”œโ”€โ”€ acceptance.suite.yml.example
    โ”œโ”€โ”€ bin
    โ”‚ย ย  โ”œโ”€โ”€ yii
    โ”‚ย ย  โ””โ”€โ”€ yii.bat
    โ”œโ”€โ”€ _bootstrap.php
    โ”œโ”€โ”€ _data
    โ”‚ย ย  โ””โ”€โ”€ dump.sql
    โ”œโ”€โ”€ _envs
    โ”œโ”€โ”€ functional
    โ”‚ย ย  โ””โ”€โ”€ _bootstrap.php
    โ”œโ”€โ”€ functional.suite.yml
    โ”œโ”€โ”€ _output
    โ”‚ย ย  โ””โ”€โ”€ report.xml
    โ”œโ”€โ”€ _support
    โ”‚ย ย  โ”œโ”€โ”€ AcceptanceTester.php
    โ”‚ย ย  โ”œโ”€โ”€ FunctionalTester.php
    โ”‚ย ย  โ”œโ”€โ”€ _generated
    โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ AcceptanceTesterActions.php
    โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ FunctionalTesterActions.php
    โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ UnitTesterActions.php
    โ”‚ย ย  โ”œโ”€โ”€ Helper
    โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ Acceptance.php
    โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ Functional.php
    โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ Unit.php
    โ”‚ย ย  โ””โ”€โ”€ UnitTester.php
    โ”œโ”€โ”€ unit
    โ”‚ย ย  โ”œโ”€โ”€ _bootstrap.php
    โ”‚ย ย  โ””โ”€โ”€ models
    โ”‚ย ย      โ””โ”€โ”€ UserTest.php
    โ””โ”€โ”€ unit.suite.yml

Most helpful comment

You need include Yii manually - it is not autoloaded.

https://github.com/yiisoft/yii2-app-basic/blob/master/tests/_bootstrap.php

All 11 comments

seems that your composer is not autoloading Yii. Can you verify?

How do I define the problem? I make sure that it has been added to the environment variable.

# composer -v
   ______
  / ____/___  ____ ___  ____  ____  ________  _____
 / /   / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
                    /_/
Composer version 1.2.0 2016-07-19 01:28:52

are you sure Yii is in your Vendor folder?

Yes, I'm sure!

qq 20161101161617

  • is yii2 in yiisoft?
  • can you check if autoload.php is loaded?
  • can you somehow verify if Yii is loaded?

Do you need a global install yii2? I'm studying it myself.

global? no just locally in your vendor

You need include Yii manually - it is not autoloaded.

https://github.com/yiisoft/yii2-app-basic/blob/master/tests/_bootstrap.php

I tried for a long time, but haven't found out where the problem is!

# codecept run unit
Codeception PHP Testing Framework v2.2.6
Powered by PHPUnit 5.6.1 by Sebastian Bergmann and contributors.

Unit Tests (4) -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- UserTest: Find user by id


FATAL ERROR. TESTS NOT FINISHED.
Class 'Yii' not found
in /root/.composer/vendor/codeception/codeception/src/Codeception/Lib/Connector/Yii2.php:71

@rob006 You helped me find the problem, Thank you very much! At the same time thank you @dynasource

Im sorry but how you include yii manually? can you show me how to do it? thankyou

Was this page helpful?
0 / 5 - 0 ratings