Class yii\codeception\TestCase

Inheritanceyii\codeception\TestCase » Codeception\TestCase\Test
Uses Traitsyii\test\FixtureTrait
Subclassesyii\codeception\DbTestCase
Available since version2.0
Source Code https://github.com/yiisoft/yii2-codeception/blob/master/TestCase.php

TestCase is the base class for all codeception unit tests

Public Properties

Hide inherited properties

Property Type Description Defined By
$appConfig array|string The application configuration that will be used for creating an application instance for each test. yii\codeception\TestCase

Public Methods

Hide inherited methods

Method Description Defined By
__call() Calls the named method which is not a class method. yii\codeception\TestCase
__get() Returns the value of an object property. yii\codeception\TestCase
fixtures() Declares the fixtures that are needed by the current test case. yii\test\FixtureTrait
getFixture() Returns the named fixture. yii\test\FixtureTrait
getFixtures() Returns the fixture objects as specified in globalFixtures() and fixtures(). yii\test\FixtureTrait
globalFixtures() Declares the fixtures shared required by different test cases. yii\test\FixtureTrait
initFixtures() Initialize the fixtures. yii\test\FixtureTrait
loadFixtures() Loads the specified fixtures. yii\test\FixtureTrait
unloadFixtures() Unloads the specified fixtures. yii\test\FixtureTrait

Protected Methods

Hide inherited methods

Method Description Defined By
createFixtures() Creates the specified fixture instances. yii\test\FixtureTrait
destroyApplication() Destroys the application instance created by mockApplication(). yii\codeception\TestCase
mockApplication() Mocks up the application instance. yii\codeception\TestCase
setUp() yii\codeception\TestCase
tearDown() yii\codeception\TestCase

Property Details

Hide inherited properties

$appConfig public property

The application configuration that will be used for creating an application instance for each test.

You can use a string to represent the file path or path alias of a configuration file. The application configuration array may contain an optional class element which specifies the class name of the application instance to be created. By default, a yii\web\Application instance will be created.

public array|string $appConfig = '@tests/codeception/config/unit.php'

Method Details

Hide inherited methods

__call() public method

Calls the named method which is not a class method.

Do not call this method directly as it is a PHP magic method that will be implicitly called when an unknown method is being invoked.

public __call( string $name, array $params ): mixed
$name string

The method name

$params array

Method parameters

return mixed

The method return value

throws yii\base\UnknownMethodException

when calling unknown method

                public function __call($name, $params)
{
    $fixture = $this->getFixture($name);
    if ($fixture instanceof BaseActiveFixture) {
        return $fixture->getModel(reset($params));
    } else {
        throw new UnknownMethodException('Unknown method: ' . get_class($this) . "::$name()");
    }
}

            
__get() public method

Returns the value of an object property.

Do not call this method directly as it is a PHP magic method that will be implicitly called when executing $value = $object->property;.

public __get( string $name ): mixed
$name string

The property name

return mixed

The property value

throws yii\base\UnknownPropertyException

if the property is not defined

                public function __get($name)
{
    $fixture = $this->getFixture($name);
    if ($fixture !== null) {
        return $fixture;
    } else {
        throw new UnknownPropertyException('Getting unknown property: ' . get_class($this) . '::' . $name);
    }
}

            
createFixtures() protected method

Defined in: yii\test\FixtureTrait::createFixtures()

Creates the specified fixture instances.

All dependent fixtures will also be created. Duplicate fixtures and circular dependencies will only be created once.

protected createFixtures( array $fixtures ): yii\test\Fixture[]
$fixtures array

The fixtures to be created. You may provide fixture names or fixture configurations. If this parameter is not provided, the fixtures specified in globalFixtures() and fixtures() will be created.

return yii\test\Fixture[]

The created fixture instances

throws yii\base\InvalidConfigException

if fixtures are not properly configured

                protected function createFixtures(array $fixtures)
{
    // normalize fixture configurations
    $config = [];  // configuration provided in test case
    $aliases = [];  // class name => alias or class name
    foreach ($fixtures as $name => $fixture) {
        if (!is_array($fixture)) {
            $class = ltrim($fixture, '\\');
            $fixtures[$name] = ['class' => $class];
            $aliases[$class] = is_int($name) ? $class : $name;
        } elseif (isset($fixture['class'])) {
            $class = ltrim($fixture['class'], '\\');
            $config[$class] = $fixture;
            $aliases[$class] = $name;
        } else {
            throw new InvalidConfigException("You must specify 'class' for the fixture '$name'.");
        }
    }
    // create fixture instances
    $instances = [];
    $stack = array_reverse($fixtures);
    while (($fixture = array_pop($stack)) !== null) {
        if ($fixture instanceof Fixture) {
            $class = get_class($fixture);
            $name = isset($aliases[$class]) ? $aliases[$class] : $class;
            unset($instances[$name]);  // unset so that the fixture is added to the last in the next line
            $instances[$name] = $fixture;
        } else {
            $class = ltrim($fixture['class'], '\\');
            $name = isset($aliases[$class]) ? $aliases[$class] : $class;
            if (!isset($instances[$name])) {
                $instances[$name] = false;
                $stack[] = $fixture = Yii::createObject($fixture);
                foreach ($fixture->depends as $dep) {
                    // need to use the configuration provided in test case
                    $stack[] = isset($config[$dep]) ? $config[$dep] : ['class' => $dep];
                }
            }
            // if the fixture is already loaded (ie. a circular dependency or if two fixtures depend on the same fixture) just skip it.
        }
    }
    return $instances;
}

            
destroyApplication() protected method

Destroys the application instance created by mockApplication().

protected destroyApplication( ): mixed

                protected function destroyApplication()
{
    if (\Yii::$app) {
        if (\Yii::$app->has('session', true)) {
            \Yii::$app->session->close();
        }
        if (\Yii::$app->has('db', true)) {
            Yii::$app->db->close();
        }
    }
    Yii::$app = null;
    Yii::$container = new Container();
}

            
fixtures() public method

Defined in: yii\test\FixtureTrait::fixtures()

Declares the fixtures that are needed by the current test case.

The return value of this method must be an array of fixture configurations. For example,

[
    // anonymous fixture
    PostFixture::class,
    // "users" fixture
    'users' => UserFixture::class,
    // "cache" fixture with configuration
    'cache' => [
         'class' => CacheFixture::class,
         'host' => 'xxx',
    ],
]

Note that the actual fixtures used for a test case will include both globalFixtures() and fixtures().

public fixtures( ): array
return array

The fixtures needed by the current test case

                public function fixtures()
{
    return [];
}

            
getFixture() public method

Defined in: yii\test\FixtureTrait::getFixture()

Returns the named fixture.

public getFixture( string $name ): yii\test\Fixture|null
$name string

The fixture name. This can be either the fixture alias name, or the class name if the alias is not used.

return yii\test\Fixture|null

The fixture object, or null if the named fixture does not exist.

                public function getFixture($name)
{
    if ($this->_fixtures === null) {
        $this->_fixtures = $this->createFixtures(array_merge($this->globalFixtures(), $this->fixtures()));
    }
    $name = ltrim($name, '\\');
    return isset($this->_fixtures[$name]) ? $this->_fixtures[$name] : null;
}

            
getFixtures() public method

Defined in: yii\test\FixtureTrait::getFixtures()

Returns the fixture objects as specified in globalFixtures() and fixtures().

public getFixtures( ): yii\test\Fixture[]
return yii\test\Fixture[]

The loaded fixtures for the current test case

                public function getFixtures()
{
    if ($this->_fixtures === null) {
        $this->_fixtures = $this->createFixtures(array_merge($this->globalFixtures(), $this->fixtures()));
    }
    return $this->_fixtures;
}

            
globalFixtures() public method

Defined in: yii\test\FixtureTrait::globalFixtures()

Declares the fixtures shared required by different test cases.

The return value should be similar to that of fixtures(). You should usually override this method in a base class.

See also fixtures().

public globalFixtures( ): array
return array

The fixtures shared and required by different test cases.

                public function globalFixtures()
{
    return [];
}

            
initFixtures() public method (available since version 2.0.12)

Defined in: yii\test\FixtureTrait::initFixtures()

Initialize the fixtures.

public initFixtures( ): mixed

                public function initFixtures()
{
    $this->unloadFixtures();
    $this->loadFixtures();
}

            
loadFixtures() public method

Defined in: yii\test\FixtureTrait::loadFixtures()

Loads the specified fixtures.

This method will call yii\test\Fixture::load() for every fixture object.

public loadFixtures( yii\test\Fixture[]|null $fixtures = null ): mixed
$fixtures yii\test\Fixture[]|null

The fixtures to be loaded. If this parameter is not specified, the return value of getFixtures() will be used.

                public function loadFixtures($fixtures = null)
{
    if ($fixtures === null) {
        $fixtures = $this->getFixtures();
    }
    foreach ($fixtures as $fixture) {
        $fixture->beforeLoad();
    }
    foreach ($fixtures as $fixture) {
        $fixture->load();
    }
    foreach (array_reverse($fixtures) as $fixture) {
        $fixture->afterLoad();
    }
}

            
mockApplication() protected method

Mocks up the application instance.

protected mockApplication( array $config = null ): yii\web\Application|yii\console\Application
$config array

The configuration that should be used to generate the application instance. If null, $appConfig will be used.

return yii\web\Application|yii\console\Application

The application instance

throws yii\base\InvalidConfigException

if the application configuration is invalid

                protected function mockApplication($config = null)
{
    if (isset(Yii::$app)) {
        return;
    }
    Yii::$container = new Container();
    $config = $config === null ? $this->appConfig : $config;
    if (is_string($config)) {
        $configFile = Yii::getAlias($config);
        if (!is_file($configFile)) {
            throw new InvalidConfigException("The application configuration file does not exist: $config");
        }
        $config = require($configFile);
    }
    if (is_array($config)) {
        if (!isset($config['class'])) {
            $config['class'] = 'yii\web\Application';
        }
        return Yii::createObject($config);
    } else {
        throw new InvalidConfigException('Please provide a configuration array to mock up an application.');
    }
}

            
setUp() protected method
protected setUp( ):

                protected function setUp()
{
    parent::setUp();
    $this->mockApplication();
    $this->loadFixtures();
}

            
tearDown() protected method
protected tearDown( ):

                protected function tearDown()
{
    $this->unloadFixtures();
    $this->destroyApplication();
    parent::tearDown();
}

            
unloadFixtures() public method

Defined in: yii\test\FixtureTrait::unloadFixtures()

Unloads the specified fixtures.

This method will call yii\test\Fixture::unload() for every fixture object.

public unloadFixtures( yii\test\Fixture[]|null $fixtures = null ): mixed
$fixtures yii\test\Fixture[]|null

The fixtures to be loaded. If this parameter is not specified, the return value of getFixtures() will be used.

                public function unloadFixtures($fixtures = null)
{
    if ($fixtures === null) {
        $fixtures = $this->getFixtures();
    }
    foreach ($fixtures as $fixture) {
        $fixture->beforeUnload();
    }
    $fixtures = array_reverse($fixtures);
    foreach ($fixtures as $fixture) {
        $fixture->unload();
    }
    foreach ($fixtures as $fixture) {
        $fixture->afterUnload();
    }
}