|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Tests\Clockwork\Storage; |
| 6 | + |
| 7 | +use Clockwork\Request\Request; |
| 8 | +use Clockwork\Storage\FileStorage; |
| 9 | +use Clockwork\Storage\StorageInterface; |
| 10 | +use PHPUnit\Framework\TestCase; |
| 11 | + |
| 12 | +class FileStorageTest extends TestCase |
| 13 | +{ |
| 14 | + private const STORAGE_ROOT_DIR = __DIR__ . '/storage'; |
| 15 | + private const STORAGE_PERMISSIONS = 0700; |
| 16 | + private const STORAGE_EXPIRATION = 10; |
| 17 | + |
| 18 | + private string $storageDir; |
| 19 | + private FileStorage $storage; |
| 20 | + |
| 21 | + public function setUp(): void |
| 22 | + { |
| 23 | + parent::setUp(); |
| 24 | + |
| 25 | + $this->storageDir = static::STORAGE_ROOT_DIR . '/' . uniqid(); |
| 26 | + |
| 27 | + $this->storage = new FileStorage( |
| 28 | + $this->storageDir, |
| 29 | + static::STORAGE_PERMISSIONS, |
| 30 | + static::STORAGE_EXPIRATION |
| 31 | + ); |
| 32 | + } |
| 33 | + |
| 34 | + public function tearDown(): void |
| 35 | + { |
| 36 | + if (file_exists($this->storageDir)) { |
| 37 | + static::rmdirRecursive($this->storageDir); |
| 38 | + } |
| 39 | + |
| 40 | + parent::tearDown(); |
| 41 | + } |
| 42 | + |
| 43 | + public static function tearDownAfterClass(): void |
| 44 | + { |
| 45 | + if (file_exists(static::STORAGE_ROOT_DIR)) { |
| 46 | + static::rmdirRecursive(static::STORAGE_ROOT_DIR); |
| 47 | + } |
| 48 | + |
| 49 | + parent::tearDownAfterClass(); |
| 50 | + } |
| 51 | + |
| 52 | + private static function rmdirRecursive(string $dir): void |
| 53 | + { |
| 54 | + if (!is_dir($dir)) { |
| 55 | + if (is_file($dir)) { |
| 56 | + unlink($dir); |
| 57 | + return; |
| 58 | + } |
| 59 | + return; |
| 60 | + } |
| 61 | + $iter = dir($dir); |
| 62 | + while (false !== ($entry = $iter->read())) { |
| 63 | + if (in_array($entry, ['..', '.'])) { |
| 64 | + continue; |
| 65 | + } |
| 66 | + static::rmdirRecursive($dir . '/' . $entry); |
| 67 | + } |
| 68 | + rmdir($dir); |
| 69 | + } |
| 70 | + |
| 71 | + public function testInterface(): void |
| 72 | + { |
| 73 | + static::assertInstanceOf(StorageInterface::class, $this->storage); |
| 74 | + } |
| 75 | + |
| 76 | + public function testStore(): void |
| 77 | + { |
| 78 | + $request = new Request([ |
| 79 | + 'id' => '12345', |
| 80 | + 'version' => 1, |
| 81 | + 'type' => 'request', |
| 82 | + 'responseDuration' => -12345678.9, |
| 83 | + 'updateToken' => 'abcd1234', |
| 84 | + ]); |
| 85 | + |
| 86 | + // ensure the request doesn't exist yet |
| 87 | + static::assertNull($this->storage->find($request->id)); |
| 88 | + |
| 89 | + // store request |
| 90 | + $this->storage->store($request); |
| 91 | + |
| 92 | + // ensure it can be loaded by ID |
| 93 | + static::assertRequestEquals( |
| 94 | + $request, |
| 95 | + $this->storage->find($request->id) |
| 96 | + ); |
| 97 | + |
| 98 | + // update request |
| 99 | + $request->addEmail('test', 'nobody@example.com'); |
| 100 | + $this->storage->update($request); |
| 101 | + |
| 102 | + // ensure it can be loaded by ID |
| 103 | + static::assertRequestEquals( |
| 104 | + $request, |
| 105 | + $this->storage->find($request->id) |
| 106 | + ); |
| 107 | + } |
| 108 | + |
| 109 | + public function testCleanup(): void |
| 110 | + { |
| 111 | + $now = microtime(true); |
| 112 | + |
| 113 | + $request1 = new Request([ |
| 114 | + 'id' => '12345', |
| 115 | + 'time' => $now - static::STORAGE_EXPIRATION * 60 - 1, |
| 116 | + ]); |
| 117 | + $request2 = new Request([ |
| 118 | + 'id' => '67890', |
| 119 | + 'time' => $now, |
| 120 | + ]); |
| 121 | + |
| 122 | + // store requests |
| 123 | + // Note: This randomly triggers a cleanup already, so it might be |
| 124 | + // that request 1 is gone even before we trigger one ourselves. |
| 125 | + $this->storage->store($request1); |
| 126 | + $this->storage->store($request2); |
| 127 | + |
| 128 | + // trigger cleanup |
| 129 | + $this->storage->cleanup(true); |
| 130 | + |
| 131 | + // ensure request 1 was purged |
| 132 | + static::assertNull($this->storage->find($request1->id)); |
| 133 | + // ensure request 2 was kept |
| 134 | + static::assertNotNull($this->storage->find($request2->id)); |
| 135 | + } |
| 136 | + |
| 137 | + public function testStore2(): void |
| 138 | + { |
| 139 | + $request1 = new Request([ |
| 140 | + 'id' => '12345', |
| 141 | + ]); |
| 142 | + $request2 = new Request([ |
| 143 | + 'id' => '67890', |
| 144 | + ]); |
| 145 | + |
| 146 | + // store requests |
| 147 | + $this->storage->store($request1); |
| 148 | + $this->storage->store($request2); |
| 149 | + |
| 150 | + // ensure both can be loaded |
| 151 | + $allRequests = $this->storage->all(); |
| 152 | + static::assertContainsOnlyInstancesOf(Request::class, $allRequests); |
| 153 | + static::assertCount(2, $allRequests); |
| 154 | + |
| 155 | + // ensure both can be loaded by ID |
| 156 | + static::assertRequestEquals( |
| 157 | + $request1, |
| 158 | + $this->storage->find($request1->id) |
| 159 | + ); |
| 160 | + static::assertRequestEquals( |
| 161 | + $request2, |
| 162 | + $this->storage->find($request2->id) |
| 163 | + ); |
| 164 | + |
| 165 | + // ensure the next after the first is the second |
| 166 | + $next = $this->storage->next($request1->id); |
| 167 | + static::assertContainsOnlyInstancesOf(Request::class, $next); |
| 168 | + static::assertCount(1, $next); |
| 169 | + static::assertRequestEquals( |
| 170 | + $request2, |
| 171 | + $next[0] |
| 172 | + ); |
| 173 | + |
| 174 | + // ensure the previous before the second is the first |
| 175 | + $previous = $this->storage->previous($request2->id); |
| 176 | + static::assertContainsOnlyInstancesOf(Request::class, $previous); |
| 177 | + static::assertCount(1, $previous); |
| 178 | + static::assertRequestEquals( |
| 179 | + $request1, |
| 180 | + $previous[0] |
| 181 | + ); |
| 182 | + } |
| 183 | + |
| 184 | + /** |
| 185 | + * compare requests |
| 186 | + * |
| 187 | + * This ensures that a result is actually a request instance and with |
| 188 | + * the expected properties. |
| 189 | + */ |
| 190 | + private static function assertRequestEquals(Request $expected, $actual): void |
| 191 | + { |
| 192 | + static::assertInstanceOf(Request::class, $actual); |
| 193 | + |
| 194 | + $expectedData = array_filter($expected->toArray()); |
| 195 | + $actualData = array_filter($actual->toArray()); |
| 196 | + |
| 197 | + static::assertEquals($expectedData, $actualData); |
| 198 | + } |
| 199 | +} |
0 commit comments