-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReplaceWithNotModifiedResponseTest.php
More file actions
117 lines (99 loc) · 3.92 KB
/
ReplaceWithNotModifiedResponseTest.php
File metadata and controls
117 lines (99 loc) · 3.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
/*
* (c) webfactory GmbH <info@webfactory.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Webfactory\HttpCacheBundle\Tests\NotModified\Attribute;
use DateTime;
use PHPUnit\Framework\Attributes\DoesNotPerformAssertions;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use RuntimeException;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Webfactory\HttpCacheBundle\NotModified\Attribute\ReplaceWithNotModifiedResponse;
use Webfactory\HttpCacheBundle\NotModified\LastModifiedDeterminator;
/**
* Tests for the ReplaceWithNotModifiedResponse attribute.
*/
final class ReplaceWithNotModifiedResponseTest extends TestCase
{
#[Test]
public function lastModifiedDescriptionsCannotBeEmpty()
{
$this->expectException(RuntimeException::class);
$attribute = new ReplaceWithNotModifiedResponse([]);
$attribute->determineLastModified(new Request());
}
#[Test]
#[DoesNotPerformAssertions]
public function stringAsSimpleLastModifiedDescription()
{
$attribute = new ReplaceWithNotModifiedResponse([MyLastModifedDeterminator::class]);
$attribute->determineLastModified(new Request());
}
#[Test]
public function serviceNameAsLastModifiedDescription()
{
/** @var ContainerInterface|MockObject $container */
$container = $this->createMock(ContainerInterface::class);
$container->expects($this->once())
->method('get')
->with('my.service')
->willReturn(new MyLastModifedDeterminator());
$attribute = new ReplaceWithNotModifiedResponse(['@my.service']);
$attribute->setContainer($container);
$attribute->determineLastModified(new Request());
}
#[Test]
#[DoesNotPerformAssertions]
public function arrayAslastModifiedDeterminatorDescriptionWithConstructorArguments()
{
$attribute = new ReplaceWithNotModifiedResponse([[MyLastModifedDeterminator::class => new DateTime('2000-01-01')]]);
$attribute->determineLastModified(new Request());
}
#[Test]
public function lastModifiedDeterminatorsHaveToImplementInterface()
{
$this->expectException(RuntimeException::class);
$attribute = new ReplaceWithNotModifiedResponse([FakeLastModifiedDeterminatorWithoutInterface::class]);
$attribute->determineLastModified(new Request());
}
#[Group('time-sensitive')]
#[Test]
public function determineLastModifiedDeterminesLastModifiedOfOneDeterminator()
{
$attribute = new ReplaceWithNotModifiedResponse([MyLastModifedDeterminator::class]);
$lastModified = $attribute->determineLastModified(new Request());
self::assertEquals(DateTime::createFromFormat('U', time()), $lastModified);
}
#[Test]
public function determineLastModifiedDeterminesLastModifiedOfMultipleDeterminators()
{
$attribute = new ReplaceWithNotModifiedResponse([
[MyLastModifedDeterminator::class => new DateTime('2001-01-01')],
[MyLastModifedDeterminator::class => new DateTime('2003-01-01')],
[MyLastModifedDeterminator::class => new DateTime('2002-01-01')],
]);
$this->assertEquals(new DateTime('2003-01-01'), $attribute->determineLastModified(new Request()));
}
}
final class FakeLastModifiedDeterminatorWithoutInterface
{
}
final class MyLastModifedDeterminator implements LastModifiedDeterminator
{
private DateTime $lastModified;
public function __construct(?DateTime $lastModified = null)
{
$this->lastModified = $lastModified ?: DateTime::createFromFormat('U', time());
}
public function getLastModified(Request $request): DateTime
{
return $this->lastModified;
}
}