// +---------------------------------------------------------------------- /** * Response测试 * @author 大漠 */ namespace tests\thinkphp\library\think; use think\Config; use think\Response; class responseTest extends \PHPUnit_Framework_TestCase { /** * * @var \think\Response */ protected $object; protected $default_return_type; protected $default_ajax_return; /** * Sets up the fixture, for example, opens a network connection. * This method is called before a test is executed. */ protected function setUp() { // 1. // restore_error_handler(); // Warning: Cannot modify header information - headers already sent by (output started at PHPUnit\Util\Printer.php:173) // more see in https://www.analysisandsolutions.com/blog/html/writing-phpunit-tests-for-wordpress-plugins-wp-redirect-and-continuing-after-php-errors.htm // 2. // the Symfony used the HeaderMock.php // 3. // not run the eclipse will held, and travis-ci.org Searching for coverage reports // **> Python coverage not found // **> No coverage report found. // add the // /** // * @runInSeparateProcess // */ if (!$this->default_return_type) { $this->default_return_type = Config::get('default_return_type'); } if (!$this->default_ajax_return) { $this->default_ajax_return = Config::get('default_ajax_return'); } } /** * Tears down the fixture, for example, closes a network connection. * This method is called after a test is executed. */ protected function tearDown() { Config::set('default_ajax_return', $this->default_ajax_return); Config::set('default_return_type', $this->default_return_type); } /** * @covers think\Response::send * @todo Implement testSend(). */ public function testSend() { $dataArr = array(); $dataArr["key"] = "value"; //$dataArr->key = "val"; $response = new \think\response\Json(); $result = $response->send($dataArr); $this->assertEquals('{"key":"value"}', $result); $_GET['callback'] = 'callback'; $response = new \think\response\Jsonp(); $result = $response->options(['var_jsonp_handler' => 'callback'])->send($dataArr); $this->assertEquals('callback({"key":"value"});', $result); $response = new Response(); $response->transform(function () { return "callbackreturndata"; }); $result = $response->send($dataArr); $this->assertEquals("callbackreturndata", $result); $_GET[Config::get('var_jsonp_handler')] = ""; } /** * @#runInSeparateProcess * @covers think\Response::redirect * @todo Implement testRedirect(). */ public function testRedirect() { // $url = "http://www.testredirect.com"; // $params = array(); // $params[] = 301; // // FIXME 静态方法mock Url::build // // echo "\r\n" . json_encode(xdebug_get_headers()) . "\r\n"; // Response::redirect($url, $params); // $this->assertContains('Location: ' . $url, xdebug_get_headers()); } /** * @#runInSeparateProcess * @covers think\Response::header * @todo Implement testHeader(). */ public function testHeader() { // $name = "Location"; // $url = "http://www.testheader.com/"; // Response::header($name, $url); // $this->assertContains($name . ': ' . $url, xdebug_get_headers()); } }