responseTest.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. /**
  12. * Response测试
  13. * @author 大漠 <zhylninc@gmail.com>
  14. */
  15. namespace tests\thinkphp\library\think;
  16. use think\Config;
  17. use think\Request;
  18. use think\Response;
  19. class responseTest extends \PHPUnit_Framework_TestCase
  20. {
  21. /**
  22. *
  23. * @var \think\Response
  24. */
  25. protected $object;
  26. protected $default_return_type;
  27. protected $default_ajax_return;
  28. /**
  29. * Sets up the fixture, for example, opens a network connection.
  30. * This method is called before a test is executed.
  31. */
  32. protected function setUp()
  33. {
  34. // 1.
  35. // restore_error_handler();
  36. // Warning: Cannot modify header information - headers already sent by (output started at PHPUnit\Util\Printer.php:173)
  37. // more see in https://www.analysisandsolutions.com/blog/html/writing-phpunit-tests-for-wordpress-plugins-wp-redirect-and-continuing-after-php-errors.htm
  38. // 2.
  39. // the Symfony used the HeaderMock.php
  40. // 3.
  41. // not run the eclipse will held, and travis-ci.org Searching for coverage reports
  42. // **> Python coverage not found
  43. // **> No coverage report found.
  44. // add the
  45. // /**
  46. // * @runInSeparateProcess
  47. // */
  48. if (!$this->default_return_type) {
  49. $this->default_return_type = Config::get('default_return_type');
  50. }
  51. if (!$this->default_ajax_return) {
  52. $this->default_ajax_return = Config::get('default_ajax_return');
  53. }
  54. }
  55. /**
  56. * Tears down the fixture, for example, closes a network connection.
  57. * This method is called after a test is executed.
  58. */
  59. protected function tearDown()
  60. {
  61. Config::set('default_ajax_return', $this->default_ajax_return);
  62. Config::set('default_return_type', $this->default_return_type);
  63. }
  64. /**
  65. * @covers think\Response::send
  66. * @todo Implement testSend().
  67. */
  68. public function testSend()
  69. {
  70. $dataArr = [];
  71. $dataArr["key"] = "value";
  72. $response = Response::create($dataArr, 'json');
  73. $result = $response->getContent();
  74. $this->assertEquals('{"key":"value"}', $result);
  75. $request = Request::instance();
  76. $request->get(['callback' => 'callback']);
  77. $response = Response::create($dataArr, 'jsonp');
  78. $result = $response->getContent();
  79. $this->assertEquals('callback({"key":"value"});', $result);
  80. }
  81. }