phpunit – 使用Fat-Free-Framework进行单元测试
| 
                         
 有没有办法使用 
 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.相关的例子和教程很难找到. 解决方法
 简短的回答 
  
  
        >将控制器代码与引导和路由代码分开 答案很长 我打算花很长时间写一篇关于测试F3路线的文章,但由于时间不够,我只想在这里给出一些观点: >创建一个定义路由的可重用文件(例如routes.php文件或带路由定义的INI文件) 例 以下示例是我的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的自定义引导文件 以后回答更新的注释 > ini_set(‘error_log’,’./ phpunit / error.log’)> $f3-> set(‘ONERROR’,function(){}); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!  | 
                  
