|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the FOSHttpCache package. |
| 5 | + * |
| 6 | + * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace FOS\HttpCache\Tests\Unit\SymfonyCache; |
| 13 | + |
| 14 | +use FOS\HttpCache\SymfonyCache\CacheEvent; |
| 15 | +use FOS\HttpCache\SymfonyCache\CacheInvalidation; |
| 16 | +use FOS\HttpCache\SymfonyCache\CleanupCacheTagsListener; |
| 17 | +use FOS\HttpCache\SymfonyCache\Events; |
| 18 | +use PHPUnit\Framework\TestCase; |
| 19 | +use Symfony\Component\HttpFoundation\Request; |
| 20 | +use Symfony\Component\HttpFoundation\Response; |
| 21 | + |
| 22 | +/** |
| 23 | + * @author Yanick Witschi <[email protected]> |
| 24 | + */ |
| 25 | +class CleanupCacheTagsListenerTest extends TestCase |
| 26 | +{ |
| 27 | + public function testSubscribedEvents() |
| 28 | + { |
| 29 | + $this->assertEquals([ |
| 30 | + Events::POST_HANDLE => 'removeTagsHeader', |
| 31 | + ], CleanupCacheTagsListener::getSubscribedEvents()); |
| 32 | + } |
| 33 | + |
| 34 | + public function testNoResponse() |
| 35 | + { |
| 36 | + $listener = new CleanupCacheTagsListener(); |
| 37 | + $listener->removeTagsHeader($this->createEvent()); |
| 38 | + $this->addToAssertionCount(1); // Nothing should happen, just asserting the "response is null" case |
| 39 | + } |
| 40 | + |
| 41 | + public function testResponseHeaderIsCleanedUp() |
| 42 | + { |
| 43 | + // Default cache tags header |
| 44 | + $response = new Response(); |
| 45 | + $response->headers->set('X-Cache-Tags', 'foo, bar'); |
| 46 | + |
| 47 | + $listener = new CleanupCacheTagsListener(); |
| 48 | + $listener->removeTagsHeader($this->createEvent($response)); |
| 49 | + |
| 50 | + $this->assertFalse($response->headers->has('X-Cache-Tags')); |
| 51 | + |
| 52 | + // Custom cache tags header |
| 53 | + $response = new Response(); |
| 54 | + $response->headers->set('Foobar', 'foo, bar'); |
| 55 | + |
| 56 | + $listener = new CleanupCacheTagsListener('Foobar'); |
| 57 | + $listener->removeTagsHeader($this->createEvent($response)); |
| 58 | + |
| 59 | + $this->assertFalse($response->headers->has('Foobar')); |
| 60 | + } |
| 61 | + |
| 62 | + private function createEvent(Response $response = null) |
| 63 | + { |
| 64 | + return new CacheEvent( |
| 65 | + $this->createMock(CacheInvalidation::class), |
| 66 | + new Request(), |
| 67 | + $response |
| 68 | + ); |
| 69 | + } |
| 70 | +} |
0 commit comments