Skip to content

Commit

Permalink
CachedPlatform: takes Cache instance as a dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
hrach committed Jan 23, 2016
1 parent 5e4a44b commit 17c1e7f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 22 deletions.
12 changes: 5 additions & 7 deletions src/Platforms/CachedPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
namespace Nextras\Dbal\Platforms;

use Nette\Caching\Cache;
use Nette\Caching\IStorage;
use Nextras\Dbal\Connection;


Expand All @@ -22,11 +21,10 @@ class CachedPlatform implements IPlatform
private $cache;


public function __construct(Connection $connection, IStorage $storage)
public function __construct(Connection $connection, Cache $cache)
{
$key = md5(json_encode($connection->getConfig()));
$this->platform = $connection->getPlatform();
$this->cache = new Cache($storage, "nextras.dbal.platform.$key");
$this->cache = $cache;
}


Expand All @@ -46,23 +44,23 @@ public function getTables()

public function getColumns($table)
{
return $this->cache->load('column.' . md5($table), function () use ($table) {
return $this->cache->load('columns.' . $table, function () use ($table) {
return $this->platform->getColumns($table);
});
}


public function getForeignKeys($table)
{
return $this->cache->load('fk.' . md5($table), function () use ($table) {
return $this->cache->load('foreign_keys.' . $table, function () use ($table) {
return $this->platform->getForeignKeys($table);
});
}


public function getPrimarySequenceName($table)
{
return $this->cache->load('seq.' . md5($table), function () use ($table) {
return $this->cache->load('sequence.' . $table, function () use ($table) {
return $this->platform->getPrimarySequenceName($table);
});
}
Expand Down
30 changes: 15 additions & 15 deletions tests/cases/unit/CachedPlatformTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace NextrasTests\Dbal;

use Mockery;
use Mockery\MockInterface;
use Nette\Caching\Cache;
use Nette\Caching\IStorage;
use Nextras\Dbal\Connection;
use Nextras\Dbal\Platforms\CachedPlatform;
Expand Down Expand Up @@ -35,9 +36,8 @@ class CachedPlatformTest extends TestCase
$this->storageMock = Mockery::mock(IStorage::class);
$this->platformMock = Mockery::mock(IPlatform::class);
$connection = Mockery::mock(Connection::class)->makePartial();
$connection->shouldReceive('getConfig')->once()->andReturn('config');
$connection->shouldReceive('getPlatform')->once()->andReturn($this->platformMock);
$this->platform = new CachedPlatform($connection, $this->storageMock);
$this->platform = new CachedPlatform($connection, new Cache($this->storageMock));
}


Expand All @@ -46,7 +46,7 @@ class CachedPlatformTest extends TestCase
$expectedCols = ['one', 'two'];
$this->storageMock
->shouldReceive('read')
->with("nextras.dbal.platform.b5ad707c2b9f71ed843ba3004e50b37d\x000985fd27fe963fac675ad2583368ec15")
->with("\x0005ea2f805e9af249b1ac88227bef0153")
->once()
->andReturn($expectedCols);

Expand All @@ -63,17 +63,17 @@ class CachedPlatformTest extends TestCase
$expectedCols = ['one', 'two'];
$this->storageMock
->shouldReceive('read')
->with("nextras.dbal.platform.b5ad707c2b9f71ed843ba3004e50b37d\x000985fd27fe963fac675ad2583368ec15")
->with("\x0005ea2f805e9af249b1ac88227bef0153")
->once()
->andReturnNull();
$this->storageMock
->shouldReceive('lock')
->with("nextras.dbal.platform.b5ad707c2b9f71ed843ba3004e50b37d\x000985fd27fe963fac675ad2583368ec15")
->with("\x0005ea2f805e9af249b1ac88227bef0153")
->once();
$this->storageMock
->shouldReceive('write')
->with(
"nextras.dbal.platform.b5ad707c2b9f71ed843ba3004e50b37d\x000985fd27fe963fac675ad2583368ec15",
"\x0005ea2f805e9af249b1ac88227bef0153",
$expectedCols,
[]
)
Expand All @@ -90,17 +90,17 @@ class CachedPlatformTest extends TestCase
$expectedTables = ['one', 'two'];
$this->storageMock
->shouldReceive('read')
->with("nextras.dbal.platform.b5ad707c2b9f71ed843ba3004e50b37d\x009ab2ec7ea4a2041306f7bdf150fcd453")
->with("\x009ab2ec7ea4a2041306f7bdf150fcd453")
->once()
->andReturnNull();
$this->storageMock
->shouldReceive('lock')
->with("nextras.dbal.platform.b5ad707c2b9f71ed843ba3004e50b37d\x009ab2ec7ea4a2041306f7bdf150fcd453")
->with("\x009ab2ec7ea4a2041306f7bdf150fcd453")
->once();
$this->storageMock
->shouldReceive('write')
->with(
"nextras.dbal.platform.b5ad707c2b9f71ed843ba3004e50b37d\x009ab2ec7ea4a2041306f7bdf150fcd453",
"\x009ab2ec7ea4a2041306f7bdf150fcd453",
$expectedTables,
[]
)
Expand All @@ -117,17 +117,17 @@ class CachedPlatformTest extends TestCase
$expectedFk = ['one', 'two'];
$this->storageMock
->shouldReceive('read')
->with("nextras.dbal.platform.b5ad707c2b9f71ed843ba3004e50b37d\x00d2838f5cf9b11857f7b84b4f490b4227")
->with("\x00863afe09d043892931f27823c1905607")
->once()
->andReturnNull();
$this->storageMock
->shouldReceive('lock')
->with("nextras.dbal.platform.b5ad707c2b9f71ed843ba3004e50b37d\x00d2838f5cf9b11857f7b84b4f490b4227")
->with("\x00863afe09d043892931f27823c1905607")
->once();
$this->storageMock
->shouldReceive('write')
->with(
"nextras.dbal.platform.b5ad707c2b9f71ed843ba3004e50b37d\x00d2838f5cf9b11857f7b84b4f490b4227",
"\x00863afe09d043892931f27823c1905607",
$expectedFk,
[]
)
Expand All @@ -144,17 +144,17 @@ class CachedPlatformTest extends TestCase
$expectedPs = 'ps_name';
$this->storageMock
->shouldReceive('read')
->with("nextras.dbal.platform.b5ad707c2b9f71ed843ba3004e50b37d\x00bd00c4ff6d83b5c76532c1ed83cb7855")
->with("\x007d7dae355d8345dd9301de988fd0eff7")
->once()
->andReturnNull();
$this->storageMock
->shouldReceive('lock')
->with("nextras.dbal.platform.b5ad707c2b9f71ed843ba3004e50b37d\x00bd00c4ff6d83b5c76532c1ed83cb7855")
->with("\x007d7dae355d8345dd9301de988fd0eff7")
->once();
$this->storageMock
->shouldReceive('write')
->with(
"nextras.dbal.platform.b5ad707c2b9f71ed843ba3004e50b37d\x00bd00c4ff6d83b5c76532c1ed83cb7855",
"\x007d7dae355d8345dd9301de988fd0eff7",
$expectedPs,
[]
)
Expand Down

0 comments on commit 17c1e7f

Please sign in to comment.