12
12
namespace Symfony \Component \Cache \Tests \Adapter ;
13
13
14
14
use Psr \Cache \CacheItemPoolInterface ;
15
+ use Psr \Log \AbstractLogger ;
15
16
use Symfony \Component \Cache \Adapter \ArrayAdapter ;
16
17
use Symfony \Component \Cache \Adapter \FilesystemAdapter ;
17
18
use Symfony \Component \Cache \Adapter \Psr16Adapter ;
19
+ use Symfony \Component \Cache \Exception \InvalidArgumentException ;
18
20
use Symfony \Component \Cache \Psr16Cache ;
19
21
20
22
/**
@@ -29,9 +31,105 @@ class Psr16AdapterTest extends AdapterTestCase
29
31
'testClearPrefix ' => 'SimpleCache cannot clear by prefix ' ,
30
32
];
31
33
34
+ private TestLogger $ testLogger ;
35
+
32
36
public function createCachePool (int $ defaultLifetime = 0 ): CacheItemPoolInterface
33
37
{
34
- return new Psr16Adapter (new Psr16Cache (new FilesystemAdapter ()), '' , $ defaultLifetime );
38
+ $ this ->testLogger = new TestLogger ();
39
+ $ psr16Adapter = new Psr16Adapter (new Psr16Cache (new FilesystemAdapter ()), '' , $ defaultLifetime );
40
+ $ psr16Adapter ->setLogger ($ this ->testLogger );
41
+
42
+ return $ psr16Adapter ;
43
+ }
44
+
45
+ /**
46
+ * @dataProvider invalidKeys
47
+ */
48
+ public function testGetItemInvalidKeys ($ key )
49
+ {
50
+ if (isset ($ this ->skippedTests [__FUNCTION__ ])) {
51
+ $ this ->markTestSkipped ($ this ->skippedTests [__FUNCTION__ ]);
52
+ }
53
+
54
+ try {
55
+ $ this ->cache ->getItem ($ key );
56
+ } catch (\Exception $ exception ) {
57
+ $ this ->assertInstanceOf (InvalidArgumentException::class, $ exception );
58
+
59
+ return ;
60
+ }
61
+
62
+ $ this ->assertNotEmpty ($ this ->testLogger ->records );
63
+ $ record = $ this ->testLogger ->records [0 ];
64
+
65
+ $ this ->assertSame ('warning ' , $ record ['level ' ]);
66
+ $ this ->assertSame (sprintf ('Failed to fetch key "{key}": Cache key "%s" contains reserved characters "{}()/ \\@:". ' , $ key ), $ record ['message ' ]);
67
+ $ this ->assertSame ('Symfony \\Component \\Cache \\Adapter \\Psr16Adapter ' , $ record ['context ' ]['cache-adapter ' ]);
68
+ $ this ->assertSame ($ key , $ record ['context ' ]['key ' ]);
69
+
70
+ $ exception = $ record ['context ' ]['exception ' ];
71
+ $ this ->assertInstanceOf (InvalidArgumentException::class, $ exception );
72
+ $ this ->assertSame (sprintf ('Cache key "%s" contains reserved characters "{}()/ \\@:". ' , $ key ), $ exception ->getMessage ());
73
+ }
74
+
75
+ /**
76
+ * @dataProvider invalidKeys
77
+ */
78
+ public function testHasItemInvalidKeys ($ key )
79
+ {
80
+ if (isset ($ this ->skippedTests [__FUNCTION__ ])) {
81
+ $ this ->markTestSkipped ($ this ->skippedTests [__FUNCTION__ ]);
82
+ }
83
+
84
+ try {
85
+ $ this ->cache ->hasItem ($ key );
86
+ } catch (\Exception $ exception ) {
87
+ $ this ->assertInstanceOf (InvalidArgumentException::class, $ exception );
88
+
89
+ return ;
90
+ }
91
+
92
+ $ this ->assertNotEmpty ($ this ->testLogger ->records );
93
+ $ record = $ this ->testLogger ->records [0 ];
94
+
95
+ $ this ->assertSame ('warning ' , $ record ['level ' ]);
96
+ $ this ->assertSame (sprintf ('Failed to check if key "{key}" is cached: Cache key "%s" contains reserved characters "{}()/\@:". ' , $ key ), $ record ['message ' ]);
97
+ $ this ->assertSame ('Symfony \\Component \\Cache \\Adapter \\Psr16Adapter ' , $ record ['context ' ]['cache-adapter ' ]);
98
+ $ this ->assertSame ($ key , $ record ['context ' ]['key ' ]);
99
+
100
+ $ exception = $ record ['context ' ]['exception ' ];
101
+ $ this ->assertInstanceOf (InvalidArgumentException::class, $ exception );
102
+ $ this ->assertSame (sprintf ('Cache key "%s" contains reserved characters "{}()/ \\@:". ' , $ key ), $ exception ->getMessage ());
103
+ }
104
+
105
+ /**
106
+ * @dataProvider invalidKeys
107
+ */
108
+ public function testDeleteItemInvalidKeys ($ key )
109
+ {
110
+ if (isset ($ this ->skippedTests [__FUNCTION__ ])) {
111
+ $ this ->markTestSkipped ($ this ->skippedTests [__FUNCTION__ ]);
112
+ }
113
+
114
+ try {
115
+ $ this ->cache ->deleteItem ($ key );
116
+ } catch (\Exception $ exception ) {
117
+ $ this ->assertInstanceOf (InvalidArgumentException::class, $ exception );
118
+
119
+ return ;
120
+ }
121
+
122
+ $ this ->assertNotEmpty ($ this ->testLogger ->records );
123
+ $ record = $ this ->testLogger ->records [0 ];
124
+
125
+ $ this ->assertSame ('warning ' , $ record ['level ' ]);
126
+ $ this ->assertSame (sprintf ('Failed to delete key "{key}": Cache key "%s" contains reserved characters "{}()/\@:". ' , $ key ), $ record ['message ' ]);
127
+ $ this ->assertSame ('Symfony \\Component \\Cache \\Adapter \\Psr16Adapter ' , $ record ['context ' ]['cache-adapter ' ]);
128
+ $ this ->assertSame ($ key , $ record ['context ' ]['key ' ]);
129
+
130
+ $ exception = $ record ['context ' ]['exception ' ];
131
+ $ this ->assertInstanceOf (InvalidArgumentException::class, $ exception );
132
+ $ this ->assertSame (
A8ED
sprintf ('Cache key "%s" contains reserved characters "{}()/ \\@:". ' , $ key ), $ exception ->getMessage ());
35
133
}
36
134
37
135
public function testValidCacheKeyWithNamespace ()
@@ -44,3 +142,17 @@ public function testValidCacheKeyWithNamespace()
44
142
$ this ->assertTrue ($ cache ->getItem ('my_key ' )->isHit (), 'Stored item is successfully retrieved. ' );
45
143
}
46
144
}
145
+
146
+ final class TestLogger extends AbstractLogger
147
+ {
148
+ public array $ records = [];
149
+
150
+ public function log ($ level , $ message , array $ context = []): void
151
+ {
152
+ $ this ->records [] = [
153
+ 'level ' => $ level ,
154
+ 'message ' => $ message ,
155
+ 'context ' => $ context ,
156
+ ];
157
+ }
158
+ }
0 commit comments