Skip to content

Commit

Permalink
[Namespace] Added test to prove correctness. (#160)
Browse files Browse the repository at this point in the history
* Added test to prove correctness.

* cs

* Added test for getting items

* Use "use" statement

* Applied changes from StyleCI

* Updated version
  • Loading branch information
Nyholm authored Jul 16, 2017
1 parent eb4271d commit 6f3508d
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 2 deletions.
99 changes: 99 additions & 0 deletions Tests/IntegrationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php

/*
* This file is part of php-cache organization.
*
* (c) 2015 Aaron Scherer <[email protected]>, Tobias Nyholm <[email protected]>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Cache\Namespaced\Tests;

use Cache\Adapter\Memcached\MemcachedCachePool;
use Cache\Hierarchy\HierarchicalPoolInterface;
use Cache\Namespaced\NamespacedCachePool;
use Memcached;
use Psr\Cache\CacheItemPoolInterface;

/**
* @author Tobias Nyholm <[email protected]>
*/
class IntegrationTest extends \PHPUnit_Framework_TestCase
{
/**
* @type CacheItemPoolInterface|HierarchicalPoolInterface
*/
private $cache;

protected function setUp()
{
$cache = new Memcached();
$cache->addServer('localhost', 11211);

$this->cache = new MemcachedCachePool($cache);
}

protected function tearDown()
{
if ($this->cache !== null) {
$this->cache->clear();
}
}

public function testGetItem()
{
$namespace = 'ns';
$nsPool = new NamespacedCachePool($this->cache, $namespace);

$item = $nsPool->getItem('key');
$this->assertEquals("|$namespace|key", $item->getKey());
}

public function testGetItems()
{
$namespace = 'ns';
$nsPool = new NamespacedCachePool($this->cache, $namespace);

$items = $nsPool->getItems(['key0', 'key1']);

$str = "|$namespace|key0";
$this->assertTrue(isset($items[$str]));
$this->assertEquals($str, $items[$str]->getKey());

$str = "|$namespace|key1";
$this->assertTrue(isset($items[$str]));
$this->assertEquals($str, $items[$str]->getKey());
}

public function testSave()
{
$namespace = 'ns';
$nsPool = new NamespacedCachePool($this->cache, $namespace);

$item = $nsPool->getItem('key');
$item->set('foo');
$nsPool->save($item);

$this->assertTrue($nsPool->hasItem('key'));
$this->assertFalse($this->cache->hasItem('key'));
}

public function testSaveDeferred()
{
$namespace = 'ns';
$nsPool = new NamespacedCachePool($this->cache, $namespace);

$item = $nsPool->getItem('key');
$item->set('foo');
$nsPool->saveDeferred($item);

$this->assertTrue($nsPool->hasItem('key'));
$this->assertFalse($this->cache->hasItem('key'));

$nsPool->commit();
$this->assertTrue($nsPool->hasItem('key'));
$this->assertFalse($this->cache->hasItem('key'));
}
}
2 changes: 1 addition & 1 deletion Tests/NamespacedCachePoolTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public function testSave()
$this->assertEquals($returnValue, $pool->save($item));
}

public function testSaveDeffered()
public function testSaveDeferred()
{
$item = $this->getMockBuilder(CacheItemInterface::class)->getMock();
$namespace = 'ns';
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
"cache/hierarchical-cache": "^1.0"
},
"require-dev": {
"phpunit/phpunit": "^5.7.21"
"phpunit/phpunit": "^5.7.21",
"cache/memcached-adapter": "^1.0"
},
"autoload": {
"psr-4": {
Expand Down

0 comments on commit 6f3508d

Please sign in to comment.