使用无脂肪框架进行单元测试

its*_*ude 3 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, 0, 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, $id, $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
    }
}
Run Code Online (Sandbox Code Playgroud)

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

Ray*_*yne 7

简短的回答

  • 将控制器代码与引导和路由代码分开

  • 在您的环境中重用路由配置,例如网站,CLI和测试环境

  • 使用Base->mock()在测试中嘲笑先前定义的路由

  • 不要Base->run()在测试环境中执行


答案很长

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

  1. 创建一个可重用的文件,用于定义路由(例如routes.php文件或INI具有路由定义的文件)

  2. 在运行测试代码之前加载路由.这可以通过PHPUnit的自定义引导程序文件轻松完成(--bootstrap <FILE>或者在PHPUnit的配置中使用相应的指令).

  3. 编写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();
Run Code Online (Sandbox Code Playgroud)

引导-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';
Run Code Online (Sandbox Code Playgroud)

routes.php文件

<?php

/**
 * @var $f3 Base
 */

$f3->route('GET /path', function(){ echo 'TEXT'; });
Run Code Online (Sandbox Code Playgroud)

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'));
    }
}
Run Code Online (Sandbox Code Playgroud)

一些说明:

  • 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(){});