|
|
|
@ -2,21 +2,30 @@
|
|
|
|
|
|
|
|
|
|
namespace yiiunit; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* This is the base class for all yii framework unit tests. |
|
|
|
|
*/ |
|
|
|
|
class TestCase extends \yii\test\TestCase |
|
|
|
|
{ |
|
|
|
|
public static $params; |
|
|
|
|
|
|
|
|
|
protected function setUp() { |
|
|
|
|
parent::setUp(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Clean up after test. |
|
|
|
|
* By default the application created with [[mockApplication]] will be destroyed. |
|
|
|
|
*/ |
|
|
|
|
protected function tearDown() |
|
|
|
|
{ |
|
|
|
|
parent::tearDown(); |
|
|
|
|
$this->destroyApp(); |
|
|
|
|
$this->destroyApplication(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public function getParam($name,$default=null) |
|
|
|
|
/** |
|
|
|
|
* 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) |
|
|
|
|
{ |
|
|
|
|
if (self::$params === null) { |
|
|
|
|
self::$params = require(__DIR__ . '/data/config.php'); |
|
|
|
@ -24,7 +33,12 @@ class TestCase extends \yii\test\TestCase
|
|
|
|
|
return isset(self::$params[$name]) ? self::$params[$name] : $default; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
protected function mockApplication($requiredConfig=array()) |
|
|
|
|
/** |
|
|
|
|
* Populates Yii::$app with a new application |
|
|
|
|
* The application will be destroyed on tearDown() automatically. |
|
|
|
|
* @param array $config The application configuration, if needed |
|
|
|
|
*/ |
|
|
|
|
protected function mockApplication($config=array()) |
|
|
|
|
{ |
|
|
|
|
static $defaultConfig = array( |
|
|
|
|
'id' => 'testapp', |
|
|
|
@ -32,10 +46,13 @@ class TestCase extends \yii\test\TestCase
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
$appClass = $this->getParam( 'appClass', '\yii\web\Application' ); |
|
|
|
|
new $appClass(array_merge($defaultConfig,$requiredConfig)); |
|
|
|
|
new $appClass(array_merge($defaultConfig,$config)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
protected function destroyApp() |
|
|
|
|
/** |
|
|
|
|
* Destroys application in Yii::$app by setting it to null. |
|
|
|
|
*/ |
|
|
|
|
protected function destroyApplication() |
|
|
|
|
{ |
|
|
|
|
\Yii::$app = null; |
|
|
|
|
} |
|
|
|
|