在我有一个带有indexTest的测试文件夹的地方,有没有办法使用PHPUnit。里面的php测试我索引中的路由。php文件?
《无脂肪指南》给出了模拟路由请求和帖子的代码片段。只有当我在测试文件中直接生成包含任何功能的路由时,我才能使这样的测试正常工作。
我想要的是用令牌模拟路由,允许它从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
}
}
我宁愿不这样声明整个路线,也许我完全错了,这是不可能的?上面的代码运行供应商/bin/phpUnit。在这方面很难找到相关的例子和教程。
>
在您的环境中重新使用路由配置,例如网站、CLI和测试环境
使用Base-
不要执行Base-
我计划了很长一段时间写一篇关于测试F3路线的文章,但由于时间不够,我将在这里给出一些要点:
>
在运行测试代码之前加载路由。这可以通过PHPUnit的自定义引导文件(--bootstrap)轻松完成
编写PHPUnit测试
以下示例是对我的GitHub Gist的改编:
引导网站。php
<?php
$f3 = Base::instance();
require 'bootstrap-shared.php';
// [Custom rules only for the website here]
require 'routes.php';
$f3->run();
引导测试。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';
路线。php
<?php
/**
* @var $f3 Base
*/
$f3->route('GET /path', function(){ echo 'TEXT'; });
示例测试。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网站。php
是网站的引导文件
引导共享。php包含所有环境共享的信息。该文件可能包含路由信息。我在示例中分离了路由信息:
routes。php
ExampleTest。php
是一种常规的PHPUnit测试
$f3-
F3不会在测试/模拟之间清理变量。您可以在运行测试之前存储原始状态,然后在PHPUnit的setUp()
方法中恢复状态
除了渲染页面,只收集应该可以用于渲染的数据也是足够的。在这种情况下,在视图中使用引入的APP. TEST
变量来跳过渲染
后续答案更新的注意事项
ini\u集('error\u log','./phpunit/error.log')
$f3-