我使用https://github.com/chriskacerguis/codeigniter-restserver/releases(v2.7.7)和https://github.com/kenjis/ci-phpunit-test(v0.12.1)。我嘗試修改REST_Controller
ci-phpunit-test退出中斷測試
{
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":"[email protected]","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);
}
}
而且我在我的Windows上運行,這是行不通的。
$ php phpunit controller/Exit_to_exception_test.php
{"foo":"bar"}