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

Qiang Xue committed
3
namespace yiiunit;
Qiang Xue committed
4
use yii\helpers\ArrayHelper;
Qiang Xue committed
5

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

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

	/**
	 * 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
30
	{
31 32
		if (static::$params === null) {
			static::$params = require(__DIR__ . '/data/config.php');
w  
Qiang Xue committed
33
		}
34
		return isset(static::$params[$name]) ? static::$params[$name] : $default;
w  
Qiang Xue committed
35
	}
Carsten Brandt committed
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
Alexander Makarov committed
41
	 * @param string $appClass name of the application class to create
Carsten Brandt committed
42
	 */
Alexander Makarov committed
43
	protected function mockApplication($config = [], $appClass = '\yii\console\Application')
44
	{
Alexander Makarov committed
45
		static $defaultConfig = [
46 47
			'id' => 'testapp',
			'basePath' => __DIR__,
Alexander Makarov committed
48
		];
49
		$defaultConfig['vendorPath'] = dirname(dirname(__DIR__)) . '/vendor';
50

Qiang Xue committed
51
		new $appClass(ArrayHelper::merge($defaultConfig, $config));
52
	}
53

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