Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🔀 Merge pull request #62 from carbontwelve/issue15-invalidate-cache-c…
…ontrol Closes #15
- Loading branch information
Showing
8 changed files
with
365 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
namespace Tapestry\Entities; | ||
|
||
class CacheStore | ||
{ | ||
private $items = []; | ||
|
||
private $hash; | ||
|
||
public function __construct($hash) | ||
{ | ||
$this->hash = $hash; | ||
} | ||
|
||
public function validate($hash) | ||
{ | ||
if ($hash !== $this->hash) { | ||
$this->reset(); | ||
} | ||
} | ||
|
||
public function setItem($key, $value) | ||
{ | ||
$this->items[$key] = $value; | ||
} | ||
|
||
public function getItem($key) | ||
{ | ||
if (isset($this->items[$key])) { | ||
return $this->items[$key]; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public function count() | ||
{ | ||
return count($this->items); | ||
} | ||
|
||
public function reset() | ||
{ | ||
$this->items = []; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,182 @@ | ||
<?php | ||
|
||
namespace Tapestry\Tests; | ||
|
||
use Symfony\Component\Console\Output\NullOutput; | ||
use Symfony\Component\Finder\Finder; | ||
use Tapestry\Entities\Cache; | ||
use Tapestry\Entities\CacheStore; | ||
use Tapestry\Entities\Project; | ||
use Tapestry\Modules\Content\ReadCache; | ||
|
||
class CacheTest extends CommandTestBase | ||
{ | ||
public function testCacheStoreValidateMethod() | ||
{ | ||
$hashA = sha1('Hello World'); | ||
$hashB = sha1('World Hello'); | ||
|
||
$store = new CacheStore($hashA); | ||
$store->setItem('A', 'B'); | ||
$store->setItem('B', 'C'); | ||
$this->assertEquals(2, $store->count()); | ||
|
||
$store->validate($hashA); | ||
$this->assertEquals(2, $store->count()); | ||
|
||
$store = new CacheStore($hashA); | ||
$store->setItem('A', 'B'); | ||
$store->setItem('B', 'C'); | ||
$this->assertEquals(2, $store->count()); | ||
|
||
$store->validate($hashB); | ||
$this->assertEquals(0, $store->count()); | ||
} | ||
|
||
public function testCacheStoreSetGet() | ||
{ | ||
$hashA = sha1('Hello World'); | ||
|
||
$store = new CacheStore($hashA, '1.0.0'); | ||
$store->setItem('A', 'B'); | ||
$store->setItem('B', 'C'); | ||
$store->setItem('D', true); | ||
$store->setItem('E', false); | ||
$store->setItem('F', null); | ||
|
||
$this->assertEquals('B', $store->getItem('A')); | ||
$this->assertEquals('C', $store->getItem('B')); | ||
$this->assertEquals(true, $store->getItem('D')); | ||
$this->assertEquals(false, $store->getItem('E')); | ||
$this->assertEquals(null, $store->getItem('F')); | ||
$this->assertEquals(null, $store->getItem('X')); | ||
$this->assertEquals(5, $store->count()); | ||
} | ||
|
||
public function testCacheStoreReset() | ||
{ | ||
$hashA = sha1('Hello World'); | ||
|
||
$store = new CacheStore($hashA, '1.0.0'); | ||
$store->setItem('A', 'B'); | ||
$store->setItem('B', 'C'); | ||
|
||
$this->assertEquals(2, $store->count()); | ||
$store->reset(); | ||
$this->assertEquals(0, $store->count()); | ||
} | ||
|
||
public function testCacheInitialLoad() | ||
{ | ||
$hashA = sha1('Hello World'); | ||
$cache = new Cache(__DIR__ . '/_tmp/cache.bin', $hashA); | ||
|
||
$cache->load(); | ||
$this->assertEquals(0, $cache->count()); | ||
} | ||
|
||
public function testCacheSaveAndLoad() | ||
{ | ||
$hashA = sha1('Hello World'); | ||
$cache = new Cache(__DIR__ . '/_tmp/cache.bin', $hashA); | ||
|
||
$cache->setItem('A', 'B'); | ||
$cache->setItem('B', 'C'); | ||
$cache->setItem('X', false); | ||
$cache->setItem('Y', null); | ||
|
||
$this->assertEquals(4, $cache->count()); | ||
|
||
$cache->save(); | ||
|
||
$cache = new Cache(__DIR__ . '/_tmp/cache.bin', $hashA); | ||
$this->assertEquals(0, $cache->count()); | ||
$cache->load(); | ||
$this->assertEquals(4, $cache->count()); | ||
|
||
$this->assertEquals('B', $cache->getItem('A')); | ||
$this->assertEquals('C', $cache->getItem('B')); | ||
$this->assertEquals(false, $cache->getItem('X')); | ||
$this->assertEquals(null, $cache->getItem('Y')); | ||
$this->assertEquals(null, $cache->getItem('Z')); | ||
} | ||
|
||
public function testCacheReset() | ||
{ | ||
$hashA = sha1('Hello World'); | ||
$cache = new Cache(__DIR__ . '/_tmp/cache.bin', $hashA); | ||
$cache->setItem('A', 'B'); | ||
$this->assertEquals(1, $cache->count()); | ||
$cache->reset(); | ||
$this->assertEquals(0, $cache->count()); | ||
} | ||
|
||
public function testCacheInvalidationByHash() | ||
{ | ||
$hashA = sha1('Hello World'); | ||
$hashB = sha1('World Hello'); | ||
$cache = new Cache(__DIR__ . '/_tmp/cache.bin', $hashA); | ||
$cache->setItem('A', 'B'); | ||
$cache->setItem('B', 'C'); | ||
|
||
$this->assertEquals(2, $cache->count()); | ||
|
||
$cache->save(); | ||
|
||
$cache = new Cache(__DIR__ . '/_tmp/cache.bin', $hashB); | ||
$cache->load(); | ||
$this->assertEquals(0, $cache->count()); | ||
} | ||
|
||
public function testReadCacheModule() | ||
{ | ||
$this->copyDirectory('/assets/build_test_21/src', '/_tmp'); | ||
|
||
$module = new ReadCache(new Finder()); | ||
$project = new Project(__DIR__ . '/_tmp', 'test'); | ||
|
||
$this->assertEquals(false, $project->has('cache')); | ||
|
||
$result = $module->__invoke($project, new NullOutput()); | ||
|
||
$this->assertEquals(true, $project->has('cache')); | ||
$this->assertEquals(true, $result); | ||
$this->assertInstanceOf(Cache::class, $project->get('cache')); | ||
|
||
$project->get('cache')->setItem('A', 'B'); | ||
$project->get('cache')->setItem('B', 'C'); | ||
$this->assertEquals(2, $project->get('cache')->count()); | ||
$project->get('cache')->save(); | ||
|
||
// Reset $module && $project variables to test loading from save | ||
|
||
unset($module, $project); | ||
$module = new ReadCache(new Finder()); | ||
$project = new Project(__DIR__ . '/_tmp', 'test'); | ||
$module->__invoke($project, new NullOutput()); | ||
$this->assertEquals(2, $project->get('cache')->count()); | ||
$this->assertEquals('B', $project->get('cache')->getItem('A')); | ||
$this->assertEquals('C', $project->get('cache')->getItem('B')); | ||
|
||
|
||
// Reset $module && $project variables and modify the src directory to test cache invalidation | ||
unset($module, $project); | ||
|
||
self::$fileSystem->copy( | ||
__DIR__.DIRECTORY_SEPARATOR.'/assets/build_test_21/src_replace/config.php', | ||
__DIR__.DIRECTORY_SEPARATOR.'/_tmp/config.php', | ||
true | ||
); | ||
|
||
self::$fileSystem->copy( | ||
__DIR__.DIRECTORY_SEPARATOR.'/assets/build_test_21/src_replace/kernel.php', | ||
__DIR__.DIRECTORY_SEPARATOR.'/_tmp/kernel.php', | ||
true | ||
); | ||
|
||
$module = new ReadCache(new Finder()); | ||
$project = new Project(__DIR__ . '/_tmp', 'test'); | ||
$module->__invoke($project, new NullOutput()); | ||
$this->assertEquals(0, $project->get('cache')->count()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?php | ||
|
||
return [ | ||
'debug' => false, | ||
'kernel' => \SiteTwentyOne\TestKernel::class, | ||
]; |
Oops, something went wrong.