Skip to content

Commit 6eb6655

Browse files
committed
added test coverage
1 parent 0314543 commit 6eb6655

1 file changed

Lines changed: 189 additions & 0 deletions

File tree

Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
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\EventSubscriber\CachePurge;
10+
11+
use FOS\HttpCache\ProxyClient\ProxyClient;
12+
use FOS\HttpCacheBundle\CacheManager;
13+
use Ibexa\Contracts\Core\Repository\Events\Content\PublishVersionEvent;
14+
use Ibexa\Contracts\Core\Repository\Values\Content\Content;
15+
use Ibexa\Contracts\Core\Repository\Values\Content\Field;
16+
use Ibexa\Contracts\Core\Repository\Values\Content\VersionInfo;
17+
use Ibexa\Core\FieldType\BinaryFile\Value as BinaryFileValue;
18+
use Ibexa\Core\FieldType\Image\Value as ImageValue;
19+
use Ibexa\HttpCache\EventSubscriber\CachePurge\BinaryFileHttpCachePurgeSubscriber;
20+
use PHPUnit\Framework\TestCase;
21+
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
22+
23+
final class BinaryFileHttpCachePurgeSubscriberTest extends TestCase
24+
{
25+
private CacheManager $cacheManager;
26+
27+
private BinaryFileHttpCachePurgeSubscriber $subscriber;
28+
29+
protected function setUp(): void
30+
{
31+
$this->cacheManager = $this->getMockBuilder(CacheManager::class)
32+
->setConstructorArgs([
33+
$this->createMock(ProxyClient::class),
34+
$this->createMock(UrlGeneratorInterface::class),
35+
])
36+
->getMock();
37+
38+
$this->subscriber = new BinaryFileHttpCachePurgeSubscriber($this->cacheManager);
39+
}
40+
41+
public function testGetSubscribedEvents(): void
42+
{
43+
self::assertArrayHasKey(PublishVersionEvent::class, BinaryFileHttpCachePurgeSubscriber::getSubscribedEvents());
44+
}
45+
46+
/**
47+
* @param \Ibexa\Contracts\Core\Repository\Values\Content\Field[] $fields
48+
*/
49+
private function buildEvent(array $fields): PublishVersionEvent
50+
{
51+
$content = $this->createMock(Content::class);
52+
$content->method('getFields')->willReturn($fields);
53+
54+
return new PublishVersionEvent(
55+
$content,
56+
$this->createMock(VersionInfo::class),
57+
[],
58+
);
59+
}
60+
61+
public function testNoFieldsDoesNotCallInvalidatePath(): void
62+
{
63+
$this->cacheManager->expects(self::never())->method('invalidatePath');
64+
65+
$this->subscriber->onPublishVersion($this->buildEvent([]));
66+
}
67+
68+
public function testNonBinaryFieldIsSkipped(): void
69+
{
70+
$this->cacheManager->expects(self::never())->method('invalidatePath');
71+
72+
$field = new Field(['value' => new \stdClass()]);
73+
$this->subscriber->onPublishVersion($this->buildEvent([$field]));
74+
}
75+
76+
public function testImageValueWithUriIsInvalidated(): void
77+
{
78+
$imageValue = new ImageValue();
79+
$imageValue->uri = '/var/site/storage/images/foo.jpg';
80+
81+
$this->cacheManager
82+
->expects(self::once())
83+
->method('invalidatePath')
84+
->with('/var/site/storage/images/foo.jpg');
85+
86+
$this->subscriber->onPublishVersion($this->buildEvent([
87+
new Field(['value' => $imageValue]),
88+
]));
89+
}
90+
91+
public function testBinaryFileValueWithUriIsInvalidated(): void
92+
{
93+
$binaryValue = new BinaryFileValue();
94+
$binaryValue->uri = '/var/site/storage/original/application/foo.pdf';
95+
96+
$this->cacheManager
97+
->expects(self::once())
98+
->method('invalidatePath')
99+
->with('/var/site/storage/original/application/foo.pdf');
100+
101+
$this->subscriber->onPublishVersion($this->buildEvent([
102+
new Field(['value' => $binaryValue]),
103+
]));
104+
}
105+
106+
public function testImageValueWithNullUriIsSkipped(): void
107+
{
108+
$imageValue = new ImageValue();
109+
$imageValue->uri = null;
110+
111+
$this->cacheManager->expects(self::never())->method('invalidatePath');
112+
113+
$this->subscriber->onPublishVersion($this->buildEvent([
114+
new Field(['value' => $imageValue]),
115+
]));
116+
}
117+
118+
public function testImageValueWithEmptyUriIsSkipped(): void
119+
{
120+
$imageValue = new ImageValue();
121+
$imageValue->uri = '';
122+
123+
$this->cacheManager->expects(self::never())->method('invalidatePath');
124+
125+
$this->subscriber->onPublishVersion($this->buildEvent([
126+
new Field(['value' => $imageValue]),
127+
]));
128+
}
129+
130+
public function testDuplicateUriIsInvalidatedOnlyOnce(): void
131+
{
132+
$uri = '/var/site/storage/images/same.jpg';
133+
134+
$imageValue1 = new ImageValue();
135+
$imageValue1->uri = $uri;
136+
137+
$imageValue2 = new ImageValue();
138+
$imageValue2->uri = $uri;
139+
140+
$this->cacheManager
141+
->expects(self::once())
142+
->method('invalidatePath')
143+
->with($uri);
144+
145+
$this->subscriber->onPublishVersion($this->buildEvent([
146+
new Field(['value' => $imageValue1]),
147+
new Field(['value' => $imageValue2]),
148+
]));
149+
}
150+
151+
public function testMultipleDistinctUrisAreEachInvalidated(): void
152+
{
153+
$imageValue = new ImageValue();
154+
$imageValue->uri = '/var/site/storage/images/a.jpg';
155+
156+
$binaryValue = new BinaryFileValue();
157+
$binaryValue->uri = '/var/site/storage/original/application/b.pdf';
158+
159+
$this->cacheManager
160+
->expects(self::exactly(2))
161+
->method('invalidatePath')
162+
->withConsecutive(
163+
['/var/site/storage/images/a.jpg'],
164+
['/var/site/storage/original/application/b.pdf'],
165+
);
166+
167+
$this->subscriber->onPublishVersion($this->buildEvent([
168+
new Field(['value' => $imageValue]),
169+
new Field(['value' => $binaryValue]),
170+
]));
171+
}
172+
173+
public function testMixedFieldsOnlyInvalidatesBinaryAndImageUris(): void
174+
{
175+
$imageValue = new ImageValue();
176+
$imageValue->uri = '/var/site/storage/images/photo.jpg';
177+
178+
$this->cacheManager
179+
->expects(self::once())
180+
->method('invalidatePath')
181+
->with('/var/site/storage/images/photo.jpg');
182+
183+
$this->subscriber->onPublishVersion($this->buildEvent([
184+
new Field(['value' => 'plain text value']),
185+
new Field(['value' => $imageValue]),
186+
new Field(['value' => 42]),
187+
]));
188+
}
189+
}

0 commit comments

Comments
 (0)