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 8 9 10 11 12 13

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

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

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

44
	/**
Qiang Xue committed
45 46 47 48
	 * 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
49
	 */
Qiang Xue committed
50
	protected function mockApplication($config = null)
51
	{
Qiang Xue committed
52 53 54 55 56 57 58 59
		$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);
60 61
	}

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