Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't find Yii2 classes in Codeception test

I am beginning to use Codeception for Unit testing within Yii2. Great that Yii2 embraces it completely now, a major reason for me to upgrade!

I am having difficulties letting Codeception find the classes. Controller classes that I have written e.g. These classes load fine by autoloading in the Yii2 application.

E.g. this controller:

<?php

class RecipeControllerTest extends \Codeception\TestCase\Test
{
   /**
    * @var \UnitTester
    */
    protected $tester;

    protected function _before()
    {
    }

    protected function _after()
    {
    }

    // tests
    public function testMe()
    {
      echo \app\controllers\RecipesController::getallrecipes();
    }

}

Results when I do codeception run unit in the console in his output:

FATAL ERROR. TESTS NOT FINISHED.

Class 'app\controllers\RecipesController' not found

My _bootstrap.php file includes this:

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

So I am sure I'm doing something wrong but am clueless.

like image 657
Alex Greve Avatar asked Jan 08 '23 12:01

Alex Greve


1 Answers

I solved this problem by explicitly creating the Yii application in the global Codeception bootstrap file.

That is, inside tests/_bootstrap.php, appended the following line of code:

new yii\web\Application(require(__DIR__ . '/../config/test.php'));

Most possibly it's because only in Application constructor the autoloaders are being actually attached to the runtime.

like image 120
hijarian Avatar answered Jan 18 '23 05:01

hijarian