Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Namespaces not working with Codeception (Yii2)

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';
    }
...
like image 321
VictorArcas Avatar asked Apr 16 '15 16:04

VictorArcas


1 Answers

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));
like image 66
Attila Kiss Avatar answered Nov 13 '22 10:11

Attila Kiss