Skip to content

Commit 178dc19

Browse files
mkoubikdg
authored andcommitted
tests: added tests for Cache::save() and Cache::load()
1 parent ef48fa9 commit 178dc19

File tree

3 files changed

+122
-0
lines changed

3 files changed

+122
-0
lines changed

tests/Caching/Cache.inc

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
use Nette\Caching\IStorage;
4+
5+
class TestStorage implements IStorage
6+
{
7+
private $data = array();
8+
9+
public function read($key)
10+
{
11+
return isset($this->data[$key]) ? $this->data[$key] : NULL;
12+
}
13+
14+
public function write($key, $data, array $dependencies)
15+
{
16+
$this->data[$key] = array(
17+
'data' => $data,
18+
'dependencies' => $dependencies,
19+
);
20+
}
21+
22+
public function lock($key) {}
23+
24+
public function remove($key) {}
25+
26+
public function clean(array $conditions) {}
27+
}

tests/Caching/Cache.load.phpt

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
/**
4+
* Test: Nette\Caching\Cache load().
5+
*/
6+
7+
use Nette\Caching\Cache,
8+
Tester\Assert;
9+
10+
11+
require __DIR__ . '/../bootstrap.php';
12+
13+
require __DIR__ . '/Cache.inc';
14+
15+
16+
// load twice with fallback
17+
$storage = new TestStorage();
18+
$cache = new Cache($storage, 'ns');
19+
20+
$value = $cache->load('key', function() {
21+
return 'value';
22+
});
23+
Assert::equal('value', $value);
24+
25+
$data = $cache->load('key', function() {
26+
return "won't load this value"; // will read from storage
27+
});
28+
Assert::equal('value', $data['data']);
29+
30+
31+
// load twice with fallback, pass dependencies
32+
$dependencies = array(Cache::TAGS => 'tag');
33+
$storage = new TestStorage();
34+
$cache = new Cache($storage, 'ns');
35+
36+
$value = $cache->load('key', function(& $deps) use ($dependencies) {
37+
$deps = $dependencies;
38+
return 'value';
39+
});
40+
Assert::equal('value', $value);
41+
42+
$data = $cache->load('key', function() {
43+
return "won't load this value"; // will read from storage
44+
});
45+
Assert::equal('value', $data['data']);
46+
Assert::equal($dependencies, $data['dependencies']);

tests/Caching/Cache.save.phpt

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
/**
4+
* Test: Nette\Caching\Cache save().
5+
*/
6+
7+
use Nette\Caching\Cache,
8+
Tester\Assert;
9+
10+
11+
require __DIR__ . '/../bootstrap.php';
12+
13+
require __DIR__ . '/Cache.inc';
14+
15+
16+
// save value with dependencies
17+
$storage = new testStorage();
18+
$cache = new Cache($storage, 'ns');
19+
$dependencies = array(Cache::TAGS => 'tag');
20+
21+
$cache->save('key', 'value', $dependencies);
22+
23+
Assert::equal('value', $cache['key']['data']);
24+
Assert::equal($dependencies, $cache['key']['dependencies']);
25+
26+
27+
// save callback return value
28+
$storage = new testStorage();
29+
$cache = new Cache($storage, 'ns');
30+
31+
$cache->save('key', function() {
32+
return 'value';
33+
});
34+
35+
Assert::equal('value', $cache['key']['data']);
36+
Assert::equal(array(), $cache['key']['dependencies']);
37+
38+
39+
// save callback return value with dependencies
40+
$storage = new testStorage();
41+
$cache = new Cache($storage, 'ns');
42+
$dependencies = array(Cache::TAGS => 'tag');
43+
44+
$cache->save('key', function() {
45+
return 'value';
46+
}, $dependencies);
47+
48+
Assert::equal('value', $cache['key']['data']);
49+
Assert::equal($dependencies, $cache['key']['dependencies']);

0 commit comments

Comments
 (0)