|
6 | 6 |
|
7 | 7 | namespace Magento\RemoteStorage\Test\Unit\Driver\Adpater\Cache; |
8 | 8 |
|
| 9 | +use Magento\RemoteStorage\Driver\Adapter\Cache\CacheInterface as AdapterCacheInterface; |
9 | 10 | use Magento\Framework\App\CacheInterface; |
10 | 11 | use Magento\Framework\Serialize\SerializerInterface; |
11 | 12 | use Magento\RemoteStorage\Driver\Adapter\Cache\Generic; |
@@ -38,19 +39,176 @@ class GenericTest extends TestCase |
38 | 39 | */ |
39 | 40 | private PathUtil $pathUtilMock; |
40 | 41 |
|
| 42 | + /** |
| 43 | + * @var int |
| 44 | + */ |
| 45 | + private int $ttl; |
| 46 | + |
41 | 47 | protected function setUp(): void |
42 | 48 | { |
43 | 49 | $this->cacheAdapterMock = $this->createMock(CacheInterface::class); |
44 | 50 | $this->serializerMock = $this->createMock(SerializerInterface::class); |
45 | 51 | $this->pathUtilMock = $this->createMock(PathUtil::class); |
| 52 | + $this->ttl = 300; |
46 | 53 |
|
47 | 54 | $this->generic = new Generic( |
48 | 55 | $this->cacheAdapterMock, |
49 | 56 | $this->serializerMock, |
50 | | - $this->pathUtilMock |
| 57 | + $this->pathUtilMock, |
| 58 | + 'flysystem:', |
| 59 | + $this->ttl |
51 | 60 | ); |
52 | 61 | } |
53 | 62 |
|
| 63 | + /** |
| 64 | + * @return void |
| 65 | + */ |
| 66 | + public function testUpdateMetadataPersists(): void |
| 67 | + { |
| 68 | + $path = 'dir/file.txt'; |
| 69 | + $objectMetadata = ['type' => 'file', 'size' => 123]; |
| 70 | + |
| 71 | + $this->pathUtilMock |
| 72 | + ->expects($this->exactly(2)) |
| 73 | + ->method('pathInfo') |
| 74 | + ->willReturnOnConsecutiveCalls( |
| 75 | + ['dirname' => 'dir', 'path' => $path], |
| 76 | + ['dirname' => '', 'path' => 'dir'] |
| 77 | + ); |
| 78 | + |
| 79 | + $this->serializerMock |
| 80 | + ->expects($this->once()) |
| 81 | + ->method('serialize') |
| 82 | + ->with($this->callback(function (array $data) use ($path) { |
| 83 | + return isset($data[$path]) |
| 84 | + && $data[$path]['type'] === 'file' |
| 85 | + && $data[$path]['size'] === 123 |
| 86 | + && $data[$path]['dirname'] === 'dir' |
| 87 | + && $data[$path]['path'] === $path; |
| 88 | + })) |
| 89 | + ->willReturn('SERIALIZED_DATA'); |
| 90 | + |
| 91 | + $this->cacheAdapterMock |
| 92 | + ->expects($this->once()) |
| 93 | + ->method('save') |
| 94 | + ->with( |
| 95 | + 'SERIALIZED_DATA', |
| 96 | + 'flysystem:' . $path, |
| 97 | + [AdapterCacheInterface::CACHE_TAG], |
| 98 | + $this->ttl |
| 99 | + ); |
| 100 | + |
| 101 | + $this->cacheAdapterMock |
| 102 | + ->expects($this->never()) |
| 103 | + ->method('load'); |
| 104 | + |
| 105 | + $this->generic->updateMetadata($path, $objectMetadata, true); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * @return void |
| 110 | + */ |
| 111 | + public function testStoreFileNotExistsMarksPathAsNonExistingAndPersists(): void |
| 112 | + { |
| 113 | + $path = 'missing/file.txt'; |
| 114 | + |
| 115 | + $this->serializerMock |
| 116 | + ->expects($this->once()) |
| 117 | + ->method('serialize') |
| 118 | + ->with([$path => false]) |
| 119 | + ->willReturn('SERIALIZED_MISSING'); |
| 120 | + |
| 121 | + $this->cacheAdapterMock |
| 122 | + ->expects($this->once()) |
| 123 | + ->method('save') |
| 124 | + ->with( |
| 125 | + 'SERIALIZED_MISSING', |
| 126 | + 'flysystem:' . $path, |
| 127 | + [AdapterCacheInterface::CACHE_TAG], |
| 128 | + $this->ttl |
| 129 | + ); |
| 130 | + |
| 131 | + $this->generic->storeFileNotExists($path); |
| 132 | + |
| 133 | + $this->cacheAdapterMock |
| 134 | + ->expects($this->never()) |
| 135 | + ->method('load'); |
| 136 | + |
| 137 | + $this->assertFalse($this->generic->exists($path)); |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * @return void |
| 142 | + */ |
| 143 | + public function testMoveFileMovesMetadataAndPurgesOldKey(): void |
| 144 | + { |
| 145 | + $oldPath = 'old/file.txt'; |
| 146 | + $newPath = 'new/file.txt'; |
| 147 | + |
| 148 | + $this->cacheAdapterMock |
| 149 | + ->expects($this->exactly(2)) |
| 150 | + ->method('load') |
| 151 | + ->willReturnOnConsecutiveCalls( |
| 152 | + 'SERIALIZED_OLD', |
| 153 | + false |
| 154 | + ); |
| 155 | + |
| 156 | + $oldMeta = [ |
| 157 | + $oldPath => [ |
| 158 | + 'path' => $oldPath, |
| 159 | + 'dirname' => 'old', |
| 160 | + 'type' => 'file', |
| 161 | + ], |
| 162 | + ]; |
| 163 | + |
| 164 | + $this->serializerMock |
| 165 | + ->expects($this->once()) |
| 166 | + ->method('unserialize') |
| 167 | + ->with('SERIALIZED_OLD') |
| 168 | + ->willReturn($oldMeta); |
| 169 | + |
| 170 | + $this->pathUtilMock |
| 171 | + ->expects($this->once()) |
| 172 | + ->method('pathInfo') |
| 173 | + ->with($newPath) |
| 174 | + ->willReturn(['dirname' => 'new', 'path' => $newPath]); |
| 175 | + |
| 176 | + $this->serializerMock |
| 177 | + ->expects($this->once()) |
| 178 | + ->method('serialize') |
| 179 | + ->with($this->callback(function (array $data) use ($newPath) { |
| 180 | + if (!isset($data[$newPath])) { |
| 181 | + return false; |
| 182 | + } |
| 183 | + $meta = $data[$newPath]; |
| 184 | + |
| 185 | + return $meta['path'] === $newPath |
| 186 | + && $meta['dirname'] === 'new' |
| 187 | + && $meta['type'] === 'file'; |
| 188 | + })) |
| 189 | + ->willReturn('SERIALIZED_NEW'); |
| 190 | + |
| 191 | + $this->cacheAdapterMock |
| 192 | + ->expects($this->once()) |
| 193 | + ->method('save') |
| 194 | + ->with( |
| 195 | + 'SERIALIZED_NEW', |
| 196 | + 'flysystem:' . $newPath, |
| 197 | + [AdapterCacheInterface::CACHE_TAG], |
| 198 | + $this->ttl |
| 199 | + ); |
| 200 | + |
| 201 | + $this->cacheAdapterMock |
| 202 | + ->expects($this->once()) |
| 203 | + ->method('remove') |
| 204 | + ->with('flysystem:' . $oldPath); |
| 205 | + |
| 206 | + $this->generic->moveFile($oldPath, $newPath); |
| 207 | + |
| 208 | + $this->assertTrue($this->generic->exists($newPath)); |
| 209 | + $this->assertNull($this->generic->exists($oldPath)); |
| 210 | + } |
| 211 | + |
54 | 212 | /** |
55 | 213 | * @param string $input |
56 | 214 | * @param array|null $expectedOutput |
@@ -117,6 +275,9 @@ public static function metaDataProvider(): array |
117 | 275 | ]; |
118 | 276 | } |
119 | 277 |
|
| 278 | + /** |
| 279 | + * @return void |
| 280 | + */ |
120 | 281 | protected function tearDown(): void |
121 | 282 | { |
122 | 283 | unset($this->generic); |
|
0 commit comments