1010use Magento \Framework \Cache \Backend \Database ;
1111use Magento \Framework \Cache \Backend \RemoteSynchronizedCache ;
1212use Magento \Framework \DB \Adapter \Pdo \Mysql ;
13- use Magento \Framework \TestFramework \Unit \Helper \ObjectManager ;
1413use PHPUnit \Framework \MockObject \MockObject ;
1514use PHPUnit \Framework \TestCase ;
1615
1716class RemoteSynchronizedCacheTest extends TestCase
1817{
19- /**
20- * @var ObjectManager
21- */
22- protected $ objectManager ;
23-
2418 /**
2519 * @var \Cm_Cache_Backend_File|MockObject
2620 */
@@ -41,24 +35,12 @@ class RemoteSynchronizedCacheTest extends TestCase
4135 */
4236 protected function setUp (): void
4337 {
44- $ this ->objectManager = new ObjectManager ($ this );
45-
46- $ this ->localCacheMockExample = $ this ->getMockBuilder (\Cm_Cache_Backend_File::class)
47- ->disableOriginalConstructor ()
48- ->getMock ();
49-
50- $ this ->remoteCacheMockExample = $ this ->getMockBuilder (Database::class)
51- ->disableOriginalConstructor ()
52- ->getMock ();
53- /** @var \Magento\Framework\Cache\Backend\Database $databaseCacheInstance */
54-
55- $ this ->remoteSyncCacheInstance = $ this ->objectManager ->getObject (
56- RemoteSynchronizedCache::class,
38+ $ this ->localCacheMockExample = $ this ->createMock (\Cm_Cache_Backend_File::class);
39+ $ this ->remoteCacheMockExample = $ this ->createMock (Database::class);
40+ $ this ->remoteSyncCacheInstance = new RemoteSynchronizedCache (
5741 [
58- 'options ' => [
59- 'remote_backend ' => $ this ->remoteCacheMockExample ,
60- 'local_backend ' => $ this ->localCacheMockExample
61- ]
42+ 'remote_backend ' => $ this ->remoteCacheMockExample ,
43+ 'local_backend ' => $ this ->localCacheMockExample
6244 ]
6345 );
6446 }
@@ -67,19 +49,13 @@ protected function setUp(): void
6749 * Test that exception is thrown if cache is not configured.
6850 *
6951 * @param array $options
70- *
7152 * @return void
7253 * @dataProvider initializeWithExceptionDataProvider
7354 */
7455 public function testInitializeWithException ($ options ): void
7556 {
7657 $ this ->expectException ('Zend_Cache_Exception ' );
77- $ this ->objectManager ->getObject (
78- RemoteSynchronizedCache::class,
79- [
80- 'options ' => $ options
81- ]
82- );
58+ new RemoteSynchronizedCache ($ options );
8359 }
8460
8561 /**
@@ -119,12 +95,7 @@ public function initializeWithExceptionDataProvider(): array
11995 */
12096 public function testInitializeWithOutException ($ options ): void
12197 {
122- $ result = $ this ->objectManager ->getObject (
123- RemoteSynchronizedCache::class,
124- [
125- 'options ' => $ options
126- ]
127- );
98+ $ result = new RemoteSynchronizedCache ($ options );
12899 $ this ->assertInstanceOf (RemoteSynchronizedCache::class, $ result );
129100 }
130101
@@ -377,6 +348,38 @@ public function testSaveWithEqualRemoteData(): void
377348 $ this ->remoteSyncCacheInstance ->save ($ remoteData , 1 , $ tags );
378349 }
379350
351+ /**
352+ * Test data save when remote data are missed but hash exists.
353+ *
354+ * @return void
355+ */
356+ public function testSaveWithEqualHashesAndMissedRemoteData (): void
357+ {
358+ $ cacheKey = 'key ' ;
359+ $ dataToSave = '2 ' ;
360+ $ remoteData = '1 ' ;
361+ $ tags = ['MAGE ' ];
362+
363+ $ this ->remoteCacheMockExample
364+ ->method ('load ' )
365+ ->willReturnOnConsecutiveCalls (\hash ('sha256 ' , $ dataToSave ), $ remoteData );
366+
367+ $ this ->remoteCacheMockExample
368+ ->expects ($ this ->exactly (2 ))
369+ ->method ('save ' )
370+ ->withConsecutive (
371+ [$ dataToSave , $ cacheKey , $ tags ],
372+ [\hash ('sha256 ' , $ dataToSave ), $ cacheKey . ':hash ' , $ tags ]
373+ )->willReturn (true );
374+ $ this ->localCacheMockExample
375+ ->expects ($ this ->once ())
376+ ->method ('save ' )
377+ ->with ($ dataToSave , $ cacheKey , [])
378+ ->willReturn (true );
379+
380+ $ this ->remoteSyncCacheInstance ->save ($ dataToSave , $ cacheKey , $ tags );
381+ }
382+
380383 /**
381384 * @return void
382385 */
0 commit comments