Class yii\codeception\TestCase
| Inheritance | yii\ |
|---|---|
| Uses Traits | yii\ |
| Subclasses | yii\ |
| Available since version | 2.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
| Property | Type | Description | Defined By |
|---|---|---|---|
| $appConfig | array|string | The application configuration that will be used for creating an application instance for each test. | yii\ |
Public Methods
| Method | Description | Defined By |
|---|---|---|
| __call() | Calls the named method which is not a class method. | yii\ |
| __get() | Returns the value of an object property. | yii\ |
| fixtures() | Declares the fixtures that are needed by the current test case. | yii\ |
| getFixture() | Returns the named fixture. | yii\ |
| getFixtures() | Returns the fixture objects as specified in globalFixtures() and fixtures(). | yii\ |
| globalFixtures() | Declares the fixtures shared required by different test cases. | yii\ |
| initFixtures() | Initialize the fixtures. | yii\ |
| loadFixtures() | Loads the specified fixtures. | yii\ |
| unloadFixtures() | Unloads the specified fixtures. | yii\ |
Protected Methods
| Method | Description | Defined By |
|---|---|---|
| createFixtures() | Creates the specified fixture instances. | yii\ |
| destroyApplication() | Destroys the application instance created by mockApplication(). | yii\ |
| mockApplication() | Mocks up the application instance. | yii\ |
| setUp() | yii\ |
|
| tearDown() | yii\ |
Property Details
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\
Method Details
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\ |
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()");
}
}
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\ |
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);
}
}
Defined in:
yii\
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\ | ||
| $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\ |
The created fixture instances |
|---|---|---|
| throws | yii\ |
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;
}
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();
}
Defined in:
yii\
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 [];
}
Defined in:
yii\
Returns the named fixture.
| public getFixture( string $name ): yii\ | ||
| $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\ |
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;
}
Defined in:
yii\
Returns the fixture objects as specified in globalFixtures() and fixtures().
| public getFixtures( ): yii\ | ||
| return | yii\ |
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;
}
Defined in:
yii\
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 [];
}
Defined in:
yii\
Initialize the fixtures.
| public initFixtures( ): mixed |
public function initFixtures()
{
$this->unloadFixtures();
$this->loadFixtures();
}
Defined in:
yii\
Loads the specified fixtures.
This method will call yii\
| public loadFixtures( yii\ | ||
| $fixtures | yii\ |
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();
}
}
Mocks up the application instance.
| protected mockApplication( array $config = null ): yii\ | ||
| $config | array |
The configuration that should be used to generate the application instance. If null, $appConfig will be used. |
| return | yii\ |
The application instance |
|---|---|---|
| throws | yii\ |
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.');
}
}
| protected setUp( ): |
protected function setUp()
{
parent::setUp();
$this->mockApplication();
$this->loadFixtures();
}
| protected tearDown( ): |
protected function tearDown()
{
$this->unloadFixtures();
$this->destroyApplication();
parent::tearDown();
}
Defined in:
yii\
Unloads the specified fixtures.
This method will call yii\
| public unloadFixtures( yii\ | ||
| $fixtures | yii\ |
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();
}
}