Yii2 framework backup
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 lines
2.8 KiB

13 years ago
<?php
namespace yiiunit;
11 years ago
use yii\helpers\ArrayHelper;
12 years ago
/**
* This is the base class for all yii framework unit tests.
*/
abstract class TestCase extends \PHPUnit_Framework_TestCase
13 years ago
{
public static $params;
/**
* Clean up after test.
* By default the application created with [[mockApplication]] will be destroyed.
*/
protected function tearDown()
{
parent::tearDown();
$this->destroyApplication();
}
/**
* 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 static function getParam($name, $default = null)
{
if (static::$params === null) {
static::$params = require(__DIR__ . '/data/config.php');
}
return isset(static::$params[$name]) ? static::$params[$name] : $default;
}
/**
* Populates Yii::$app with a new application
* The application will be destroyed on tearDown() automatically.
* @param array $config The application configuration, if needed
* @param string $appClass name of the application class to create
*/
protected function mockApplication($config = [], $appClass = '\yii\console\Application')
{
new $appClass(ArrayHelper::merge([
'id' => 'testapp',
'basePath' => __DIR__,
'vendorPath' => $this->getVendorPath(),
], $config));
}
10 years ago
protected function mockWebApplication($config = [], $appClass = '\yii\web\Application')
{
new $appClass(ArrayHelper::merge([
10 years ago
'id' => 'testapp',
'basePath' => __DIR__,
'vendorPath' => $this->getVendorPath(),
10 years ago
'components' => [
'request' => [
'cookieValidationKey' => 'wefJDF8sfdsfSDefwqdxj9oq',
'scriptFile' => __DIR__ .'/index.php',
'scriptUrl' => '/index.php',
10 years ago
],
]
], $config));
}
10 years ago
protected function getVendorPath()
{
$vendor = dirname(dirname(__DIR__)) . '/vendor';
if (!is_dir($vendor)) {
$vendor = dirname(dirname(dirname(dirname(__DIR__))));
}
return $vendor;
10 years ago
}
/**
* Destroys application in Yii::$app by setting it to null.
*/
protected function destroyApplication()
{
\Yii::$app = null;
}
/**
* Asserting two strings equality ignoring line endings
*
* @param string $expected
* @param string $actual
*/
public function assertEqualsWithoutLE($expected, $actual)
{
$expected = str_replace("\r\n", "\n", $expected);
$actual = str_replace("\r\n", "\n", $actual);
$this->assertEquals($expected, $actual);
}
}