diff --git a/.gitignore b/.gitignore index 47f36f6..31df0e3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .DS_Store -vendor \ No newline at end of file +vendor +tests/tmp/* \ No newline at end of file diff --git a/src/Storage/FilesystemJsonStorage.php b/src/Storage/FilesystemJsonStorage.php index ddff741..be10d8e 100644 --- a/src/Storage/FilesystemJsonStorage.php +++ b/src/Storage/FilesystemJsonStorage.php @@ -50,7 +50,7 @@ class FilesystemJsonStorage implements TokenStorageInterface { public function put(string $key, array $data): bool { // Ensure that the containing folder exists. - mkdir($this->path, 0777, true); + @mkdir($this->path, 0777, true); return file_put_contents($this->getPath($key), json_encode($data)) !== false; } diff --git a/tests/FilesystemJsonStorageTest.php b/tests/FilesystemJsonStorageTest.php new file mode 100644 index 0000000..c5b6c18 --- /dev/null +++ b/tests/FilesystemJsonStorageTest.php @@ -0,0 +1,56 @@ +isFile()) { + unlink($fileInfo->getPathname()); + } + } + } + + protected function tearDown(): void { + // Clean tmp dir. + foreach (new DirectoryIterator(TMP_DIR) as $fileInfo) { + if ($fileInfo->isFile()) { + unlink($fileInfo->getPathname()); + } + } + } + + public function testCrud() { + $s = new FilesystemJsonStorage(TMP_DIR, 0, false); + + $t1data = ['example' => 'data']; + $t1path = TMP_DIR . '/t1.json'; + + $this->assertTrue($s->put('t1', $t1data), "Saving t1 data failed"); + + $this->assertFileExists($t1path, "t1 was not stored to $t1path"); + + $this->assertEquals($t1data, $s->get('t1'), "The result of getting t1 did not match the saved data."); + + $s->delete('t1'); + + $this->assertFileDoesNotExist($t1path, "t1 was not successfully deleted."); + + $this->assertNull($s->get('t1'), "Getting a nonexistent key did not return null"); + } + + public function testCleanUp() { + $s = new FilesystemJsonStorage(TMP_DIR, 1, false); + $s->put('t1', ['example' => 'data']); + sleep(2); + $s->cleanUp(); + $this->assertNull($s->get('t1'), 't1 was not cleaned up after expiring.'); + } +}