1414use Magento \Csp \Model \SubresourceIntegrity ;
1515use Magento \Csp \Model \SubresourceIntegrityRepository ;
1616use Magento \Csp \Model \SubresourceIntegrityFactory ;
17+ use Magento \Csp \Model \SubresourceIntegrity \StorageInterface ;
1718
1819/**
1920 * Unit Test for Class @see Magento\Csp\Model\SubresourceIntegrityRepository
2021 *
2122 */
2223class SubresourceIntegrityRepositoryTest extends TestCase
2324{
25+ /**
26+ * @var string
27+ */
28+ private string $ context = "test " ;
2429
2530 /**
2631 * @var MockObject
@@ -32,6 +37,11 @@ class SubresourceIntegrityRepositoryTest extends TestCase
3237 */
3338 private MockObject $ serializerMock ;
3439
40+ /**
41+ * @var MockObject
42+ */
43+ private MockObject $ storage ;
44+
3545 /**
3646 * @var MockObject
3747 */
@@ -61,16 +71,21 @@ protected function setUp(): void
6171 $ this ->integrityFactoryMock = $ this ->getMockBuilder (SubresourceIntegrityFactory::class)
6272 ->disableOriginalConstructor ()
6373 ->getMock ();
74+ $ this ->storage = $ this ->getMockBuilder (StorageInterface::class)
75+ ->disableOriginalConstructor ()
76+ ->getMockForAbstractClass ();
6477
6578 $ this ->subresourceIntegrityRepository = new SubresourceIntegrityRepository (
6679 $ this ->cacheMock ,
6780 $ this ->serializerMock ,
68- $ this ->integrityFactoryMock
81+ $ this ->integrityFactoryMock ,
82+ $ this ->context ,
83+ $ this ->storage
6984 );
7085 }
7186
72- /** Test save repository
73- *
87+ /**
88+ * Test save repository
7489 *
7590 * @return void
7691 */
@@ -85,45 +100,80 @@ public function testSave(): void
85100
86101 $ expected [$ data ->getPath ()] = $ data ->getHash ();
87102 $ 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+ );
92125 }
93126
94- /** Test that cache saves in bunch
95- *
127+ /**
128+ * Test that cache saves in bunch
96129 *
97130 * @return void
98131 */
99132 public function testSaveBunch (): void
100133 {
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+ ];
116148
117149 $ expected = [];
118150
119- foreach ($ bunches as $ bunch ) {
120- $ expected [$ bunch ->getPath ()] = $ bunch ->getHash ();
151+ foreach ($ bunch as $ integrity ) {
152+ $ expected [$ integrity ->getPath ()] = $ integrity ->getHash ();
121153 }
154+
122155 $ 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+ );
128178 }
129179}
0 commit comments