加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

phpunit – 使用Fat-Free-Framework进行单元测试

发布时间:2020-12-13 21:57:06 所属栏目:PHP教程 来源:网络整理
导读:有没有办法使用 PHPUnit我有一个indexTest.php的测试文件夹里面测试我的index.php文件中的路由? 无脂指南提供了用于模拟路由请求和POSTS的代码片段.如果我直接在我的测试文件中使用其中的任何功能生成路由,我只能设法得到这样的测试. 我想要的是模拟带有令
有没有办法使用 PHPUnit我有一个indexTest.php的测试文件夹里面测试我的index.php文件中的路由?

无脂指南提供了用于模拟路由请求和POSTS的代码片段.如果我直接在我的测试文件中使用其中的任何功能生成路由,我只能设法得到这样的测试.

我想要的是模拟带有令牌的路由,允许它从index.php中的路由和控制器运行,并测试应该通过运行路由设置的f3变量.

<?php

class indexTest extends PHPUnit_Framework_TestCase
{
    public function test()
    {
        $f3 = Base::instance();
// Don't write to STDOUT
        $f3->set('QUIET',true);
        $f3->route('GET /path',function(){ echo 'TEXT'; });

        $this->assertNull($f3->mock('GET /path'));
        $this->assertSame('TEXT',$f3->get('RESPONSE'));

        $f3->route('GET /verify/@answer/@value',function($f3,$params){
                $errors = array();

                $answer = $params['answer'];
                $value = $params['value'];

                $prefix = substr($answer,3);  //pre,ans,pos
                $id = (int)substr($answer,3);      //question id number (1,2,3,4)
//$value is the input value from user

                $result = check_id($prefix,$id,$value);

                if($result !== true){
                    $errors[] = $result;
                }
                $f3->set('errors',$errors);
                return $errors;
            });

        function check_id($prefix,$value)
        {
            if($prefix == 'pre' || $prefix == 'pos'){

                if($value <= 0 || $value > 180 || $value === NULL){
                    echo 'The input value of ' . $prefix . $id . ' question was out of bounds';
                    return 'The input value of ' . $prefix . $id . ' question was out of bounds';
                }else{
                    return true;
                }

            }else if($prefix == 'ans'){

                if($value < 0 || $value > 10 || $value === NULL){
                    echo 'The value of quiz ans' + $id + ' was out of bounds';
                    return 'The value of quiz ans' + $id + ' was out of bounds';
                }else{
                    return true;
                }

            }else {
                return 'The prefix does not match';
            }
        }

        $this->assertNotNull($f3->mock('GET /verify/ans1/8'));
        $this->assertEmpty($f3->get('RESPONSE')[0]);

        $this->assertNotNull($f3->mock('GET /verify/dsk4/6'));
        $this->assertSame('6',$f3->get('PARAMS.value'));
        $this->assertSame('dsk4',$f3->get('PARAMS.answer'));
        $this->assertEmpty($f3->get('RESPONSE')[0]);

        $this->assertNotNull($f3->mock('GET /verify/pre4/250'));
        $this->assertSame('The input value of pre4 question was out of bounds',$f3->get('errors')[0]);
        $this->assertNotSame('pre4',$f3->get('PARAMS.answer'));

        $f3->set('QUIET',FALSE); // allow test results to be shown later

        $f3->clear('ERROR');  // clear any errors
    }
}

我不想像这样申报整条路线,也许我完全错了,这是不可能的?上面的代码运行vendor / bin / phpunit.相关的例子和教程很难找到.

解决方法

简短的回答

>将控制器代码与引导和路由代码分开
>在您的环境中重用路由配置,例如网站,CLI和测试环境
>在测试中使用Base-> mock()来模拟先前定义的路线
>不要在测试环境中执行Base-> run()

答案很长

我打算花很长时间写一篇关于测试F3路线的文章,但由于时间不够,我只想在这里给出一些观点:

>创建一个定义路由的可重用文件(例如routes.php文件或带路由定义的INI文件)
>在运行测试代码之前加载路由.这可以通过PHPUnit的自定义引导文件(–bootstrap< FILE>或在PHPUnit的配置中使用相应的指令)轻松完成.
>编写PHPUnit测试

以下示例是我的GitHub Gist的改编:

引导-website.php

<?php

$f3 = Base::instance();

require 'bootstrap-shared.php';

// [Custom rules only for the website here]

require 'routes.php';

$f3->run();

引导-test.php的

<?php

$f3 = Base::instance();

require 'bootstrap-shared.php';

// [Custom rules only for testing environment here]

$f3->set('QUIET',true);
$f3->set('APP.TEST',true);

require 'routes.php';

routes.php文件

<?php

/**
 * @var $f3 Base
 */

$f3->route('GET /path',function(){ echo 'TEXT'; });

ExampleTest.php

class ExampleTest extends PHPUnit_Framework_TestCase {
    public function test() {
        // Could also be provided by a custom base TestCase.
        $f3 = Base::instance();

        $this->assertNull($f3->mock('GET /path'));
        $this->assertSame('TEXT',$f3->get('RESPONSE'));
    }
}

一些说明:

> bootstrap-test.php是PHPUnit的自定义引导文件
> bootstrap-website.php是该网站的bootstrapping文件
> bootstrap-shared.php包含所有环境共享的信息.该文件可能包含路由信息.我在示例中分离了路由信息:routes.php
> ExampleTest.php是一个常规的PHPUnit测试
> $f3-> set(‘QUIET’,true);应将代码段添加到自定义引导程序文件中.引入一个显示应用程序在测试模式下运行的变量也是一个好主意,例如$f3-> set(‘APP.TEST’,true)
> F3不会在测试/模拟之间清理变量.您可以在运行测试之前存储原始状态,然后在PHPUnit的setUp()方法中恢复状态
>除了渲染页面之外,还可以仅收集应该可用于渲染的数据.在这种情况下,在视图中使用引入的APP.TEST变量来跳过渲染

以后回答更新的注释

> ini_set(‘error_log’,’./ phpunit / error.log’)> $f3-> set(‘ONERROR’,function(){});

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读