|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * @copyright Copyright (C) Ibexa AS. All rights reserved. |
| 5 | + * @license For full copyright and license information view LICENSE file distributed with this source code. |
| 6 | + */ |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +namespace Ibexa\Tests\HttpCache\ResponseTagger\Value; |
| 10 | + |
| 11 | +use FOS\HttpCache\ResponseTagger; |
| 12 | +use Ibexa\Contracts\Core\Repository\Values\Content\ContentInfo; |
| 13 | +use Ibexa\Contracts\HttpCache\Handler\ContentTagInterface; |
| 14 | +use Ibexa\Core\Repository\Values\Content\Location; |
| 15 | +use Ibexa\HttpCache\ResponseTagger\Value\LocationTagger; |
| 16 | +use PHPUnit\Framework\TestCase; |
| 17 | +use Psr\Log\LoggerInterface; |
| 18 | +use RuntimeException; |
| 19 | + |
| 20 | +final class LocationTaggerTest extends TestCase |
| 21 | +{ |
| 22 | + private ResponseTagger $responseTagger; |
| 23 | + |
| 24 | + protected function setUp(): void |
| 25 | + { |
| 26 | + $this->responseTagger = $this->createMock(ResponseTagger::class); |
| 27 | + } |
| 28 | + |
| 29 | + public function testTagsLocation(): void |
| 30 | + { |
| 31 | + $contentInfo = new ContentInfo(['mainLocationId' => 99]); |
| 32 | + $location = new Location([ |
| 33 | + 'id' => 55, |
| 34 | + 'parentLocationId' => 2, |
| 35 | + 'pathString' => '/1/2/55/', |
| 36 | + 'contentInfo' => $contentInfo, |
| 37 | + ]); |
| 38 | + |
| 39 | + $this->responseTagger |
| 40 | + ->expects(self::exactly(3)) |
| 41 | + ->method('addTags') |
| 42 | + ->withConsecutive( |
| 43 | + [[ContentTagInterface::LOCATION_PREFIX . '55']], |
| 44 | + [[ContentTagInterface::PARENT_LOCATION_PREFIX . '2']], |
| 45 | + [[ |
| 46 | + ContentTagInterface::PATH_PREFIX . '1', |
| 47 | + ContentTagInterface::PATH_PREFIX . '2', |
| 48 | + ContentTagInterface::PATH_PREFIX . '55', |
| 49 | + ]], |
| 50 | + ); |
| 51 | + |
| 52 | + $tagger = new LocationTagger($this->responseTagger); |
| 53 | + $tagger->tag($location); |
| 54 | + } |
| 55 | + |
| 56 | + public function testDoesNotAddLocationTagWhenLocationIsMainLocation(): void |
| 57 | + { |
| 58 | + $contentInfo = new ContentInfo(['mainLocationId' => 55]); |
| 59 | + $location = new Location([ |
| 60 | + 'id' => 55, |
| 61 | + 'parentLocationId' => 2, |
| 62 | + 'pathString' => '/1/2/55/', |
| 63 | + 'contentInfo' => $contentInfo, |
| 64 | + ]); |
| 65 | + |
| 66 | + $this->responseTagger |
| 67 | + ->expects(self::exactly(2)) |
| 68 | + ->method('addTags') |
| 69 | + ->withConsecutive( |
| 70 | + [[ContentTagInterface::PARENT_LOCATION_PREFIX . '2']], |
| 71 | + [[ |
| 72 | + ContentTagInterface::PATH_PREFIX . '1', |
| 73 | + ContentTagInterface::PATH_PREFIX . '2', |
| 74 | + ContentTagInterface::PATH_PREFIX . '55', |
| 75 | + ]], |
| 76 | + ); |
| 77 | + |
| 78 | + $tagger = new LocationTagger($this->responseTagger); |
| 79 | + $tagger->tag($location); |
| 80 | + } |
| 81 | + |
| 82 | + public function testLogsWarningInProdWhenValueIsNotLocation(): void |
| 83 | + { |
| 84 | + $contentInfo = new ContentInfo(['id' => 1]); |
| 85 | + |
| 86 | + $logger = $this->createMock(LoggerInterface::class); |
| 87 | + $logger |
| 88 | + ->expects(self::once()) |
| 89 | + ->method('warning') |
| 90 | + ->with(self::stringContains(get_debug_type($contentInfo))); |
| 91 | + |
| 92 | + $this->responseTagger->expects(self::never())->method('addTags'); |
| 93 | + |
| 94 | + $tagger = new LocationTagger($this->responseTagger, $logger, false); |
| 95 | + $tagger->tag($contentInfo); |
| 96 | + } |
| 97 | + |
| 98 | + public function testThrowsRuntimeExceptionInDebugWhenValueIsNotLocation(): void |
| 99 | + { |
| 100 | + $contentInfo = new ContentInfo(['id' => 1]); |
| 101 | + |
| 102 | + $this->expectException(RuntimeException::class); |
| 103 | + $this->expectExceptionMessageMatches( |
| 104 | + '/' . preg_quote(get_debug_type($contentInfo), '/') . '/' |
| 105 | + ); |
| 106 | + |
| 107 | + $this->responseTagger->expects(self::never())->method('addTags'); |
| 108 | + |
| 109 | + $tagger = new LocationTagger($this->responseTagger, null, true); |
| 110 | + $tagger->tag($contentInfo); |
| 111 | + } |
| 112 | +} |
0 commit comments