Skip to content

Commit 8a08766

Browse files
committed
provided test coverage
1 parent c4b06da commit 8a08766

2 files changed

Lines changed: 200 additions & 0 deletions

File tree

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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\ContentInfoTagger;
16+
use PHPUnit\Framework\TestCase;
17+
use Psr\Log\LoggerInterface;
18+
use RuntimeException;
19+
20+
final class ContentInfoTaggerTest 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 testTagsContentInfo(): void
30+
{
31+
$contentInfo = new ContentInfo(['id' => 42, 'contentTypeId' => 7, 'mainLocationId' => 100]);
32+
33+
$this->responseTagger
34+
->expects(self::exactly(2))
35+
->method('addTags')
36+
->withConsecutive(
37+
[[ContentTagInterface::CONTENT_PREFIX . '42', ContentTagInterface::CONTENT_TYPE_PREFIX . '7']],
38+
[[ContentTagInterface::LOCATION_PREFIX . '100']],
39+
);
40+
41+
$tagger = new ContentInfoTagger($this->responseTagger);
42+
$tagger->tag($contentInfo);
43+
}
44+
45+
public function testTagsContentInfoWithoutMainLocation(): void
46+
{
47+
$contentInfo = new ContentInfo(['id' => 42, 'contentTypeId' => 7, 'mainLocationId' => null]);
48+
49+
$this->responseTagger
50+
->expects(self::once())
51+
->method('addTags')
52+
->with([ContentTagInterface::CONTENT_PREFIX . '42', ContentTagInterface::CONTENT_TYPE_PREFIX . '7']);
53+
54+
$tagger = new ContentInfoTagger($this->responseTagger);
55+
$tagger->tag($contentInfo);
56+
}
57+
58+
public function testLogsWarningInProdWhenValueIsNotContentInfo(): void
59+
{
60+
$location = new Location();
61+
62+
$logger = $this->createMock(LoggerInterface::class);
63+
$logger
64+
->expects(self::once())
65+
->method('warning')
66+
->with(self::stringContains(get_debug_type($location)));
67+
68+
$this->responseTagger->expects(self::never())->method('addTags');
69+
70+
$tagger = new ContentInfoTagger($this->responseTagger, $logger, false);
71+
$tagger->tag($location);
72+
}
73+
74+
public function testThrowsRuntimeExceptionInDebugWhenValueIsNotContentInfo(): void
75+
{
76+
$location = new Location();
77+
78+
$this->expectException(RuntimeException::class);
79+
$this->expectExceptionMessageMatches(
80+
'/' . preg_quote(get_debug_type($location), '/') . '/'
81+
);
82+
83+
$this->responseTagger->expects(self::never())->method('addTags');
84+
85+
$tagger = new ContentInfoTagger($this->responseTagger, null, true);
86+
$tagger->tag($location);
87+
}
88+
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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

Comments
 (0)