ResponseTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. namespace think\tests;
  3. use PHPUnit\Framework\TestCase;
  4. use Mockery as m;
  5. use think\Cookie;
  6. use think\response\Html;
  7. use think\response\Json;
  8. class ResponseTest extends TestCase
  9. {
  10. protected function tearDown(): void
  11. {
  12. m::close();
  13. }
  14. public function testHtmlResponseCreation()
  15. {
  16. $cookie = m::mock(Cookie::class);
  17. $response = new Html($cookie, 'test content');
  18. $this->assertInstanceOf(Html::class, $response);
  19. $this->assertEquals('test content', $response->getData());
  20. }
  21. public function testJsonResponseCreation()
  22. {
  23. $cookie = m::mock(Cookie::class);
  24. $data = ['key' => 'value'];
  25. $response = new Json($cookie, $data);
  26. $this->assertInstanceOf(Json::class, $response);
  27. $this->assertEquals($data, $response->getData());
  28. }
  29. public function testResponseCode()
  30. {
  31. $cookie = m::mock(Cookie::class);
  32. $response = new Html($cookie, 'test', 200);
  33. $this->assertEquals(200, $response->getCode());
  34. $response->code(404);
  35. $this->assertEquals(404, $response->getCode());
  36. }
  37. public function testResponseHeaders()
  38. {
  39. $cookie = m::mock(Cookie::class);
  40. $response = new Html($cookie, 'test');
  41. $response->header(['Content-Type' => 'text/html']);
  42. $headers = $response->getHeader();
  43. $this->assertEquals('text/html', $headers['Content-Type']);
  44. $response->header(['X-Custom' => 'value']);
  45. $headers = $response->getHeader();
  46. $this->assertEquals('value', $headers['X-Custom']);
  47. }
  48. public function testResponseData()
  49. {
  50. $cookie = m::mock(Cookie::class);
  51. $response = new Html($cookie, 'initial');
  52. $this->assertEquals('initial', $response->getData());
  53. $response->data('updated');
  54. $this->assertEquals('updated', $response->getData());
  55. }
  56. public function testResponseStatusMethods()
  57. {
  58. $cookie = m::mock(Cookie::class);
  59. $response = new Html($cookie, '', 200);
  60. $this->assertEquals(200, $response->getCode());
  61. $response->code(404);
  62. $this->assertEquals(404, $response->getCode());
  63. $response->code(500);
  64. $this->assertEquals(500, $response->getCode());
  65. }
  66. public function testContentTypeMethod()
  67. {
  68. $cookie = m::mock(Cookie::class);
  69. $response = new Html($cookie, 'test');
  70. $response->contentType('application/json', 'utf-8');
  71. $headers = $response->getHeader();
  72. $this->assertEquals('application/json; charset=utf-8', $headers['Content-Type']);
  73. }
  74. public function testLastModified()
  75. {
  76. $cookie = m::mock(Cookie::class);
  77. $response = new Html($cookie, 'test');
  78. $time = '2025-01-01 10:00:00';
  79. $response->lastModified($time);
  80. $headers = $response->getHeader();
  81. $this->assertArrayHasKey('Last-Modified', $headers);
  82. }
  83. public function testETag()
  84. {
  85. $cookie = m::mock(Cookie::class);
  86. $response = new Html($cookie, 'test');
  87. $etag = 'test-etag';
  88. $response->eTag($etag);
  89. $headers = $response->getHeader();
  90. $this->assertEquals('test-etag', $headers['ETag']);
  91. }
  92. }