我自己的指导方针
好的,我根据yii2-smarty 内部的测试找到了我的方法。
因此,这些是使用 phpunit 测试您自己的 Yii2 扩展开发的指南:
1) 测试/bootstrap.php:
// ensure we get report on all possible php errors
error_reporting(-1);
define('YII_ENABLE_ERROR_HANDLER', false);
define('YII_DEBUG', true);
$_SERVER['SCRIPT_NAME'] = '/' . __DIR__;
$_SERVER['SCRIPT_FILENAME'] = __FILE__;
require_once(__DIR__ . '/../vendor/autoload.php');
require_once(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php');
//optionals
Yii::setAlias('@testsBasePathOrWhateverYouWant', __DIR__);
Yii::setAlias('@slinstj/MyExtensionAlias', dirname(__DIR__));
2) 创建一个扩展 \PHPUnit_Framework_TestCase 的测试/TestCase 基类:
namespace slinstj\MyExtension\tests;
use yii\di\Container;
/**
* This is the base class for all yii framework unit tests.
*/
abstract class TestCase extends \PHPUnit_Framework_TestCase
{
/**
* Clean up after test.
* By default the application created with [[mockApplication]] will be destroyed.
*/
protected function tearDown()
{
parent::tearDown();
$this->destroyApplication();
}
/**
* 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' => dirname(__DIR__) . '/vendor',
], $config));
}
protected function mockWebApplication($config = [], $appClass = '\yii\web\Application')
{
new $appClass(ArrayHelper::merge([
'id' => 'testapp',
'basePath' => __DIR__,
'vendorPath' => dirname(__DIR__) . '/vendor',
'components' => [
'request' => [
'cookieValidationKey' => 'wefJDF8sfdsfSDefwqdxj9oq',
'scriptFile' => __DIR__ .'/index.php',
'scriptUrl' => '/index.php',
],
]
], $config));
}
/**
* Destroys application in Yii::$app by setting it to null.
*/
protected function destroyApplication()
{
Yii::$app = null;
Yii::$container = new Container();
}
protected function debug($data)
{
return fwrite(STDERR, print_r($data, TRUE));
}
}
3) 创建扩展 TestCase 的 testSomething 类:
namespace slinstj\MyExtension\tests;
use yii\web\AssetManager;
use slinstj\MyExtension\View;
use Yii;
/**
* Generated by PHPUnit_SkeletonGenerator on 2015-10-30 at 17:45:03.
*/
class ViewTest extends TestCase
{
/**
* Sets up the fixture, for example, opens a network connection.
* This method is called before a test is executed.
*/
protected function setUp()
{
parent::setUp();
$this->mockWebApplication();
}
public function testSomething()
{
$view = $this->mockView();
$content = $view->renderFile('@someAlias/views/index.php', ['data' => 'Hello World!']);
$this->assertEquals(1, preg_match('#something#', $content), 'Html view does not contain "something": ' . $content);
}
//other tests...
/**
* @return View
*/
protected function mockView()
{
return new View([
'someConfig' => 'someValue',
'assetManager' => $this->mockAssetManager(),
]);
}
protected function mockAssetManager()
{
$assetDir = Yii::getAlias('@the/path/to/assets');
if (!is_dir($assetDir)) {
mkdir($assetDir, 0777, true);
}
return new AssetManager([
'basePath' => $assetDir,
'baseUrl' => '/assets',
]);
}
protected function findByRegex($regex, $content, $match = 1)
{
$matches = [];
preg_match($regex, $content, $matches);
return $matches[$match];
}
}
就是这样!这个代码框架高度基于 yii2-smaty/tests 代码。希望能帮助你(还有我的进一步需要)。