14
14
use Magento \Csp \Model \SubresourceIntegrity ;
15
15
use Magento \Csp \Model \SubresourceIntegrityRepository ;
16
16
use Magento \Csp \Model \SubresourceIntegrityFactory ;
17
+ use Magento \Csp \Model \SubresourceIntegrity \StorageInterface ;
17
18
18
19
/**
19
20
* Unit Test for Class @see Magento\Csp\Model\SubresourceIntegrityRepository
20
21
*
21
22
*/
22
23
class SubresourceIntegrityRepositoryTest extends TestCase
23
24
{
25
+ /**
26
+ * @var string
27
+ */
28
+ private string $ context = "test " ;
24
29
25
30
/**
26
31
* @var MockObject
@@ -32,6 +37,11 @@ class SubresourceIntegrityRepositoryTest extends TestCase
32
37
*/
33
38
private MockObject $ serializerMock ;
34
39
40
+ /**
41
+ * @var MockObject
42
+ */
43
+ private MockObject $ storage ;
44
+
35
45
/**
36
46
* @var MockObject
37
47
*/
@@ -61,16 +71,21 @@ protected function setUp(): void
61
71
$ this ->integrityFactoryMock = $ this ->getMockBuilder (SubresourceIntegrityFactory::class)
62
72
->disableOriginalConstructor ()
63
73
->getMock ();
74
+ $ this ->storage = $ this ->getMockBuilder (StorageInterface::class)
75
+ ->disableOriginalConstructor ()
76
+ ->getMockForAbstractClass ();
64
77
65
78
$ this ->subresourceIntegrityRepository = new SubresourceIntegrityRepository (
66
79
$ this ->cacheMock ,
67
80
$ this ->serializerMock ,
68
- $ this ->integrityFactoryMock
81
+ $ this ->integrityFactoryMock ,
82
+ $ this ->context ,
83
+ $ this ->storage
69
84
);
70
85
}
71
86
72
- /** Test save repository
73
- *
87
+ /**
88
+ * Test save repository
74
89
*
75
90
* @return void
76
91
*/
@@ -85,45 +100,80 @@ public function testSave(): void
85
100
86
101
$ expected [$ data ->getPath ()] = $ data ->getHash ();
87
102
$ serialized = json_encode ($ expected );
88
- $ this ->cacheMock ->expects ($ this ->once ())->method ('load ' )->willReturn (false );
89
- $ this ->serializerMock ->expects ($ this ->once ())->method ('serialize ' )->with ($ expected )->willReturn ($ serialized );
90
- $ this ->cacheMock ->expects ($ this ->once ())->method ('save ' )->willReturn (true );
91
- $ this ->assertTrue ($ this ->subresourceIntegrityRepository ->save ($ data ));
103
+
104
+ $ this ->storage ->expects ($ this ->once ())
105
+ ->method ('load ' )
106
+ ->with ($ this ->context )
107
+ ->willReturn (null );
108
+
109
+ $ this ->serializerMock ->expects ($ this ->never ())
110
+ ->method ('unserialize ' );
111
+
112
+ $ this ->serializerMock ->expects ($ this ->once ())
113
+ ->method ('serialize ' )
114
+ ->with ($ expected )
115
+ ->willReturn ($ serialized );
116
+
117
+ $ this ->storage ->expects ($ this ->once ())
118
+ ->method ('save ' )
119
+ ->with ($ serialized , $ this ->context )
120
+ ->willReturn (true );
121
+
122
+ $ this ->assertTrue (
123
+ $ this ->subresourceIntegrityRepository ->save ($ data )
124
+ );
92
125
}
93
126
94
- /** Test that cache saves in bunch
95
- *
127
+ /**
128
+ * Test that cache saves in bunch
96
129
*
97
130
* @return void
98
131
*/
99
132
public function testSaveBunch (): void
100
133
{
101
- $ bunch1 = new SubresourceIntegrity (
102
- [
103
- 'hash ' => 'testhash ' ,
104
- 'path ' => 'js/jquery.js '
105
- ]
106
- );
107
-
108
- $ bunch2 = new SubresourceIntegrity (
109
- [
110
- 'hash ' => 'testhash2 ' ,
111
- 'path ' => 'js/test.js '
112
- ]
113
- );
114
-
115
- $ bunches = [$ bunch1 , $ bunch2 ];
134
+ $ bunch = [
135
+ new SubresourceIntegrity (
136
+ [
137
+ 'hash ' => 'testhash ' ,
138
+ 'path ' => 'js/jquery.js '
139
+ ]
140
+ ),
141
+ new SubresourceIntegrity (
142
+ [
143
+ 'hash ' => 'testhash2 ' ,
144
+ 'path ' => 'js/test.js '
145
+ ]
146
+ )
147
+ ];
116
148
117
149
$ expected = [];
118
150
119
- foreach ($ bunches as $ bunch ) {
120
- $ expected [$ bunch ->getPath ()] = $ bunch ->getHash ();
151
+ foreach ($ bunch as $ integrity ) {
152
+ $ expected [$ integrity ->getPath ()] = $ integrity ->getHash ();
121
153
}
154
+
122
155
$ serializedBunch = json_encode ($ expected );
123
- $ this ->cacheMock ->expects ($ this ->once ())->method ('load ' )->willReturn (false );
124
- $ this ->serializerMock ->expects ($ this ->once ())->method ('serialize ' )
125
- ->with ($ expected )->willReturn ($ serializedBunch );
126
- $ this ->cacheMock ->expects ($ this ->once ())->method ('save ' )->willReturn (true );
127
- $ this ->assertTrue ($ this ->subresourceIntegrityRepository ->saveBunch ($ bunches ));
156
+
157
+ $ this ->storage ->expects ($ this ->once ())
158
+ ->method ('load ' )
159
+ ->with ($ this ->context )
160
+ ->willReturn (null );
161
+
162
+ $ this ->serializerMock ->expects ($ this ->never ())
163
+ ->method ('unserialize ' );
164
+
165
+ $ this ->serializerMock ->expects ($ this ->once ())
166
+ ->method ('serialize ' )
167
+ ->with ($ expected )
168
+ ->willReturn ($ serializedBunch );
169
+
170
+ $ this ->storage ->expects ($ this ->once ())
171
+ ->method ('save ' )
172
+ ->with ($ serializedBunch , $ this ->context )
173
+ ->willReturn (true );
174
+
175
+ $ this ->assertTrue (
176
+ $ this ->subresourceIntegrityRepository ->saveBunch ($ bunch )
177
+ );
128
178
}
129
179
}
0 commit comments