我使用https://github.com/chriskacerguis/codeigniter-restserver/releases(v2.7.7)和https://github.com/kenjis/ci-phpunit-test(v0.12.1)。我尝试修改REST_Controller
{
call_user_func_array([$this, $controller_method], $arguments);
}
catch (CIPHPUnitTestExitException $ex) {
// This block is for ci-phpunit-test
throw $ex;
}
catch (Exception $ex) {
我的测试很简单
class Example_test extends TestCase
{
public function test_When_get_users_Then_returns_all_user_data()
{
try
{
# var_dump('a'); // show on the terminal
$output = $this->request('GET', 'api/example/users');
# var_dump('b'); // not show on the terminal
}
catch (CIPHPUnitTestExitException $e)
{
$output = ob_get_clean();
}
$data = json_decode($output, true);
$this->assertCount(3, $data);
$this->assertResponseCode(200);
}
}
我尝试运行此测试,我在终端上获得了请求输出。
$ php phpunit controller/api/Example_test.php
[{"id":1,"name":"John","email":"john@example.com","fact":"Loves coding:},{...},...]
我发现问题是libraries/Rest_Controller.php
public function response($data = NULL, $http_code = NULL, $continue = FALSE)
{
if ($continue === FALSE)
{
// Display the data and exit execution
$this->output->_display();
exit; # -----> Interrupt test
}
}
在Linux上运行是工作(PHP版本是5.6)。在Windows上不起作用(PHP版本为5.4)。
我尝试创建一个简单的控制器来测试exit
。
Exit_to_exception.php
<?php
class Exit_to_exception extends CI_Controller
{
public function call_exit_in_controller_method()
{
$this->output
->set_status_header(200)
->set_content_type('application/json', 'utf-8')
->set_output(json_encode(['foo' => 'bar']))
->_display();
exit();
}
}
Exit_to_exception_test.php
<?php
/**
* @group patcher
*/
class Exit_to_exception_test extends TestCase
{
public function test_call_exit_in_controller_method()
{
try {
$output = $this->request(
'GET', 'exit_to_exception/call_exit_in_controller_method'
);
} catch (CIPHPUnitTestExitException $e) {
$output = ob_get_clean();
$this->assertEquals('Exit_to_exception', $e->class);
$this->assertEquals('call_exit_in_controller_method', $e->method);
$this->assertNull($e->exit_status);
}
$this->assertContains('{"foo":"bar"}', $output);
}
}
我在我的窗户上跑,它不起作用。
$ php phpunit controller/Exit_to_exception_test.php
{"foo":"bar"}
答案 0 :(得分:1)
这只是在Windows上的ci-phpunit-test中的一个错误。请尝试v0.12.2或更高版本。
请参阅https://github.com/kenjis/ci-phpunit-test/blob/master/docs/ChangeLog.md#v0122-20160724