首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >扩展开发的Yii2测试环境指南

扩展开发的Yii2测试环境指南
EN

Stack Overflow用户
提问于 2015-10-31 18:24:50
回答 2查看 977关注 0票数 1

我正在开发一个YII2public (MIT)扩展,以更改一些yii\web\View行为(minify、组合和许多其他优化)。

我做起来很容易。但我真的想为它写尽可能多的测试(共欺骗)。这就是我非常困惑的地方。

我已经有了一些单元测试(例如:测试一个特定的小型化,或者返回组合的最小化结果)。但是我想测试整个结果,以及我的扩展和使用它的Yii2 web应用程序之间的最终集成。

我只想为这个过程提供一些指导:

  1. 我应该有一个真正(完整)的应用程序在我的扩展,以测试的目的吗?如果是,应该在内部测试中“安装”dir吗?
  2. 你会使用功能测试吗?(我认为是这样的,因为View会在AssetBundles中找到文件,合并并缩小它们,将结果作为一个文件发布,并在视图中用新的url (即优化的资产url)替换资产的url;
  3. 你能提供一些非常基本的例子/指南吗?

我只想强调一下,我不想让你做我的测试工作,我真的想要学习怎么做。这就是为什么我真的会非常感谢任何建议。

非常感谢。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-11-03 20:22:40

我自己的指南

好的,我已经根据易2-自以为是内部的测试找到了我的方法。

因此,以下是使用phpunit测试您自己的Yii2扩展开发的指南:

1) test/bootstrap.php:

代码语言:javascript
复制
// 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基类

代码语言:javascript
复制
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)创建扩展testSomething的TestCase

代码语言:javascript
复制
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/test代码。希望能帮助你(和我有更多的需要)。

票数 4
EN

Stack Overflow用户

发布于 2016-05-25 14:04:35

这种方法很有效,但我不得不做一些小的调整:

  1. 如果您正在开发一个扩展名(in /供应商/ you / bootstrap.php目录),并且bootstrap.php文件位于一个测试目录中,那么autoloader和yii基类的路径很可能是错误的。更好的办法是: require_once(__DIR__ .‘/./autooload.php’);require_once(__DIR__ .‘/./yiisoft/yii2 2/yii.php’);
  2. 我测试了一个验证器类,它需要一个应用程序对象。我只是在引导文件中创建了一个控制台应用程序(附加到文件的末尾): $config = require(__DIR__ .‘/./config/sole.php’);$application = new \Application($config);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33455060

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档