CheckHttpCacheComponentTest.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP(tm) Project
  13. * @since 4.4.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Test\TestCase\Controller\Component;
  17. use Cake\Controller\Component\CheckHttpCacheComponent;
  18. use Cake\Controller\Controller;
  19. use Cake\Event\Event;
  20. use Cake\Http\ServerRequest;
  21. use Cake\TestSuite\TestCase;
  22. /**
  23. * CheckHttpCacheComponentTest class
  24. */
  25. class CheckHttpCacheComponentTest extends TestCase
  26. {
  27. /**
  28. * @var \Cake\Controller\Component\CheckHTtpCacheComponent
  29. */
  30. protected $Component;
  31. /**
  32. * @var \Cake\Controller\Controller
  33. */
  34. protected $Controller;
  35. /**
  36. * setUp method
  37. */
  38. public function setUp(): void
  39. {
  40. parent::setUp();
  41. static::setAppNamespace();
  42. $request = (new ServerRequest())
  43. ->withHeader('If-Modified-Since', '2012-01-01 00:00:00')
  44. ->withHeader('If-None-Match', '*');
  45. $this->Controller = new Controller($request);
  46. $this->Component = new CheckHttpCacheComponent($this->Controller->components());
  47. }
  48. public function testBeforeRenderSuccess(): void
  49. {
  50. $response = $this->Controller->getResponse()
  51. ->withEtag('something', true);
  52. $this->Controller->setResponse($response);
  53. $event = new Event('Controller.beforeRender', $this->Controller);
  54. $this->Component->beforeRender($event);
  55. $this->assertTrue($event->isStopped());
  56. $response = $this->Controller->getResponse();
  57. $this->assertSame(304, $response->getStatusCode());
  58. }
  59. public function testBeforeRenderNoOp(): void
  60. {
  61. $event = new Event('Controller.beforeRender', $this->Controller);
  62. $this->Component->beforeRender($event);
  63. $this->assertFalse($event->isStopped());
  64. $response = $this->Controller->getResponse();
  65. $this->assertSame(200, $response->getStatusCode());
  66. }
  67. }