Skip to content

Commit 4e61e27

Browse files
committed
FileCache: fixed cache directory creation
1 parent d6606d0 commit 4e61e27

3 files changed

Lines changed: 49 additions & 1 deletion

File tree

src/Github/Storages/FileCache.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,18 @@ class FileCache extends Github\Sanity implements ICache
2323
*/
2424
public function __construct($tempDir)
2525
{
26+
if (!is_dir($tempDir)) {
27+
throw new MissingDirectoryException("Directory '$tempDir' is missing.");
28+
}
29+
2630
$dir = $tempDir . DIRECTORY_SEPARATOR . 'milo.github-api';
2731

2832
if (!is_dir($dir)) {
2933
set_error_handler(function($severity, $message, $file, $line) use ($dir, & $valid) {
3034
restore_error_handler();
31-
throw new MissingDirectoryException("Temporary directory '$dir' is missing.'", 0, new \ErrorException($message, 0, $severity, $file, $line));
35+
if (!is_dir($dir)) {
36+
throw new MissingDirectoryException("Cannot create '$dir' directory.", 0, new \ErrorException($message, 0, $severity, $file, $line));
37+
}
3238
});
3339
mkdir($dir);
3440
restore_error_handler();

tests/Github/Storages/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/temp.FileCache
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
/**
4+
* @author Miloslav Hůla
5+
*/
6+
7+
require __DIR__ . '/../../bootstrap.php';
8+
9+
10+
$e = Assert::exception(function() {
11+
new Milo\Github\Storages\FileCache(__DIR__ . DIRECTORY_SEPARATOR . 'non-exists');
12+
}, 'Milo\Github\Storages\MissingDirectoryException', "Directory '%a%non-exists' is missing.");
13+
14+
Assert::null($e->getPrevious());
15+
16+
17+
define('TEMP_DIR', __DIR__ . '/temp.FileCache');
18+
@mkdir(TEMP_DIR); # @ = directory may exist
19+
Tester\Helpers::purge(TEMP_DIR);
20+
21+
$cache = new Milo\Github\Storages\FileCache(TEMP_DIR);
22+
23+
Assert::null($cache->load('undefined'));
24+
25+
$value = $cache->save('key-1', NULL);
26+
Assert::null($cache->load('key-1'));
27+
28+
$value = $cache->save('key-2', TRUE);
29+
Assert::true($cache->load('key-2'));
30+
31+
$value = $cache->save('key-3', FALSE);
32+
Assert::false($cache->load('key-3'));
33+
34+
$value = $cache->save('key-4', []);
35+
Assert::same([], $cache->load('key-4'));
36+
37+
$value = $cache->save('key-5', [0, 'a', []]);
38+
Assert::same([0, 'a', []], $cache->load('key-5'));
39+
40+
$value = $cache->save('key-6', new stdClass);
41+
Assert::equal(new stdClass, $cache->load('key-6'));

0 commit comments

Comments
 (0)