Skip to content

Commit 6f3508d

Browse files
authored
[Namespace] Added test to prove correctness. (#160)
* Added test to prove correctness. * cs * Added test for getting items * Use "use" statement * Applied changes from StyleCI * Updated version
1 parent eb4271d commit 6f3508d

File tree

3 files changed

+102
-2
lines changed

3 files changed

+102
-2
lines changed

Tests/IntegrationTest.php

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
3+
/*
4+
* This file is part of php-cache organization.
5+
*
6+
* (c) 2015 Aaron Scherer <[email protected]>, Tobias Nyholm <[email protected]>
7+
*
8+
* This source file is subject to the MIT license that is bundled
9+
* with this source code in the file LICENSE.
10+
*/
11+
12+
namespace Cache\Namespaced\Tests;
13+
14+
use Cache\Adapter\Memcached\MemcachedCachePool;
15+
use Cache\Hierarchy\HierarchicalPoolInterface;
16+
use Cache\Namespaced\NamespacedCachePool;
17+
use Memcached;
18+
use Psr\Cache\CacheItemPoolInterface;
19+
20+
/**
21+
* @author Tobias Nyholm <[email protected]>
22+
*/
23+
class IntegrationTest extends \PHPUnit_Framework_TestCase
24+
{
25+
/**
26+
* @type CacheItemPoolInterface|HierarchicalPoolInterface
27+
*/
28+
private $cache;
29+
30+
protected function setUp()
31+
{
32+
$cache = new Memcached();
33+
$cache->addServer('localhost', 11211);
34+
35+
$this->cache = new MemcachedCachePool($cache);
36+
}
37+
38+
protected function tearDown()
39+
{
40+
if ($this->cache !== null) {
41+
$this->cache->clear();
42+
}
43+
}
44+
45+
public function testGetItem()
46+
{
47+
$namespace = 'ns';
48+
$nsPool = new NamespacedCachePool($this->cache, $namespace);
49+
50+
$item = $nsPool->getItem('key');
51+
$this->assertEquals("|$namespace|key", $item->getKey());
52+
}
53+
54+
public function testGetItems()
55+
{
56+
$namespace = 'ns';
57+
$nsPool = new NamespacedCachePool($this->cache, $namespace);
58+
59+
$items = $nsPool->getItems(['key0', 'key1']);
60+
61+
$str = "|$namespace|key0";
62+
$this->assertTrue(isset($items[$str]));
63+
$this->assertEquals($str, $items[$str]->getKey());
64+
65+
$str = "|$namespace|key1";
66+
$this->assertTrue(isset($items[$str]));
67+
$this->assertEquals($str, $items[$str]->getKey());
68+
}
69+
70+
public function testSave()
71+
{
72+
$namespace = 'ns';
73+
$nsPool = new NamespacedCachePool($this->cache, $namespace);
74+
75+
$item = $nsPool->getItem('key');
76+
$item->set('foo');
77+
$nsPool->save($item);
78+
79+
$this->assertTrue($nsPool->hasItem('key'));
80+
$this->assertFalse($this->cache->hasItem('key'));
81+
}
82+
83+
public function testSaveDeferred()
84+
{
85+
$namespace = 'ns';
86+
$nsPool = new NamespacedCachePool($this->cache, $namespace);
87+
88+
$item = $nsPool->getItem('key');
89+
$item->set('foo');
90+
$nsPool->saveDeferred($item);
91+
92+
$this->assertTrue($nsPool->hasItem('key'));
93+
$this->assertFalse($this->cache->hasItem('key'));
94+
95+
$nsPool->commit();
96+
$this->assertTrue($nsPool->hasItem('key'));
97+
$this->assertFalse($this->cache->hasItem('key'));
98+
}
99+
}

Tests/NamespacedCachePoolTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public function testSave()
124124
$this->assertEquals($returnValue, $pool->save($item));
125125
}
126126

127-
public function testSaveDeffered()
127+
public function testSaveDeferred()
128128
{
129129
$item = $this->getMockBuilder(CacheItemInterface::class)->getMock();
130130
$namespace = 'ns';

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
"cache/hierarchical-cache": "^1.0"
2525
},
2626
"require-dev": {
27-
"phpunit/phpunit": "^5.7.21"
27+
"phpunit/phpunit": "^5.7.21",
28+
"cache/memcached-adapter": "^1.0"
2829
},
2930
"autoload": {
3031
"psr-4": {

0 commit comments

Comments
 (0)