TestCase.php 1.38 KB
Newer Older
w  
Qiang Xue committed
1 2
<?php

Qiang Xue committed
3 4
namespace yiiunit;

Carsten Brandt committed
5 6 7
/**
 * This is the base class for all yii framework unit tests.
 */
Alexander Makarov committed
8
abstract class TestCase extends \yii\test\TestCase
w  
Qiang Xue committed
9
{
Qiang Xue committed
10
	public static $params;
w  
Qiang Xue committed
11

Carsten Brandt committed
12 13 14 15
	/**
	 * Clean up after test.
	 * By default the application created with [[mockApplication]] will be destroyed.
	 */
16 17 18
	protected function tearDown()
	{
		parent::tearDown();
Carsten Brandt committed
19
		$this->destroyApplication();
20
	}
Carsten Brandt committed
21 22 23 24 25 26 27 28

	/**
	 * Returns a test configuration param from /data/config.php
	 * @param string $name params name
	 * @param mixed $default default value to use when param is not set.
	 * @return mixed the value of the configuration param
	 */
	public function getParam($name, $default = null)
w  
Qiang Xue committed
29
	{
Qiang Xue committed
30 31
		if (self::$params === null) {
			self::$params = require(__DIR__ . '/data/config.php');
w  
Qiang Xue committed
32
		}
33
		return isset(self::$params[$name]) ? self::$params[$name] : $default;
w  
Qiang Xue committed
34
	}
Carsten Brandt committed
35 36 37 38 39 40

	/**
	 * Populates Yii::$app with a new application
	 * The application will be destroyed on tearDown() automatically.
	 * @param array $config The application configuration, if needed
	 */
41
	protected function mockApplication($config = array(), $appClass = '\yii\console\Application')
42 43 44 45 46
	{
		static $defaultConfig = array(
			'id' => 'testapp',
			'basePath' => __DIR__,
		);
47

Carsten Brandt committed
48
		new $appClass(array_merge($defaultConfig,$config));
49
	}
50

Carsten Brandt committed
51 52 53 54
	/**
	 * Destroys application in Yii::$app by setting it to null.
	 */
	protected function destroyApplication()
55
	{
56
		\Yii::$app = null;
57
	}
Zander Baldwin committed
58
}