7

Im using Codeception in Yii2 to make acceptance tests and there's no way to access my models because namespaces are not working into these tests.

I have this in my tests/_bootstrap.php

 require(__DIR__ . '/../vendor/autoload.php');
 require(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php');

 $config = require(__DIR__ . '/../console/config/main.php');
 //
 $application = new yii\console\Application( $config );

 ## Added (@vitalik_74)
 Yii::setAlias('@tests', dirname(__DIR__));

This in my console/config/main

 <?php
 $params = array_merge(
     require(__DIR__ . '/params.php'),
     require(__DIR__ . '/params-local.php')
 );

 return [
     'id' => 'app-console',
     'basePath' => dirname(__DIR__),
     'bootstrap' => ['log'],
     'controllerNamespace' => 'console\controllers',
     'components' => [
         'log' => [
             'targets' => [
                 [
                     'class' => 'yii\log\FileTarget',
                     'levels' => ['error', 'warning'],
                 ],
             ],
         ],
     ],
     'params' => $params,
 ];

 <?php
 return [
     'adminEmail' => '[email protected]',
     'supportEmail' => '[email protected]',
     'user.passwordResetTokenExpire' => 3600,
 ];

And this is one of the wannabe-tests:

 <?php namespace tests\acceptance;

 use \AcceptanceTester;
 use backend\models\User; ## I have tried writing it with a / at the beggining

class ListUserCest
{
public function _before(AcceptanceTester $I)
{

}

public function _after(AcceptanceTester $I)
{
}

public function init(AcceptanceTester $I)
{
    $this->login($I);

    if( User::find()->exists() )
        $I->amGoingTo('List Users having at least one');
    else
        $I->amGoingTo('List Users having any');
}

...

I get this error when running the tests:

PHP Fatal error: Class 'backend\models\User' not found in /var/www/project/tests/acceptance/ListUserCest.php on line 21 Error: Class 'backend\models\User' not found

Please, help me, I have tried everything I know

EDIT

Now (after adding the line recommended by vitalik_74) I can use for example, \Yii methods into the tests but witout the web application configuration, just the console configuration. I mean, I still can't use \backend\models\User and I can't neither access the Yii::$app->user status (to check if user is logged, for example).

The User model is just a common ActiveRecord model with his tableName, rules, attributeLabels, and some relational methods like getProfile(). It works out of the tests

<?php
namespace backend\models;
use common\helpers\MathHelper;
use backend\models\AntropometricData;
use Yii;

class User extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'user';
    }
...
3
  • 1
    Please post the backend\models\User class Commented Apr 16, 2015 at 17:00
  • 1
    Try this. Add in _bootstrap.php in end of file this - Yii::setAlias('@tests', dirname(__DIR__)); Commented Apr 16, 2015 at 18:21
  • Please post the backend\models\User class not the ListUserCest Class Commented Apr 17, 2015 at 6:55

1 Answer 1

2

Put the next code to tests/_bootstrap.php:

require('vendor/autoload.php');
require('vendor/yiisoft/yii2/Yii.php');
$config = require('config/web.php');
(new yii\web\Application($config));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.