TestCase.php 1.63 KB
Newer Older
1 2 3 4 5
<?php

namespace yii\codeception;

use Yii;
Qiang Xue committed
6
use yii\base\InvalidConfigException;
7
use Codeception\TestCase\Test;
8 9 10 11 12 13 14

/**
 * TestCase is the base class for all codeception unit tests
 *
 * @author Mark Jebri <mark.github@yandex.ru>
 * @since 2.0
 */
15
class TestCase extends Test
16 17
{
	/**
Qiang Xue committed
18 19
	 * @var array|string 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.
20
	 */
Qiang Xue committed
21
	public static $appConfig = [];
22
	/**
Qiang Xue committed
23
	 * @var string the application class that [[mockApplication()]] should use
24
	 */
Qiang Xue committed
25 26
	public static $appClass = 'yii\web\Application';

Qiang Xue committed
27 28 29 30 31 32 33 34
	/**
	 * @inheritdoc
	 */
	protected function setUp()
	{
		parent::setUp();
		$this->mockApplication();
	}
35 36

	/**
Qiang Xue committed
37
	 * @inheritdoc
38 39 40 41 42 43 44
	 */
	protected function tearDown()
	{
		$this->destroyApplication();
		parent::tearDown();
	}

45
	/**
Qiang Xue committed
46 47 48 49
	 * Mocks up the application instance.
	 * @param array $config 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
50
	 */
Qiang Xue committed
51
	protected function mockApplication($config = null)
52
	{
Qiang Xue committed
53 54 55 56 57 58 59 60
		$config = $config === null ? static::$appConfig : $config;
		if (is_string($config)) {
			$config = Yii::getAlias($config);
		}
		if (!is_array($config)) {
			throw new InvalidConfigException('Please provide a configuration for creating application.');
		}
		return new static::$appClass($config);
61 62
	}

63
	/**
Qiang Xue committed
64
	 * Destroys the application instance created by [[mockApplication]].
65
	 */
66 67
	protected function destroyApplication()
	{
Qiang Xue committed
68
		Yii::$app = null;
69 70
	}
}