Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Kreyu committed Jan 21, 2024
1 parent e351b38 commit a45165c
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 6 deletions.
8 changes: 3 additions & 5 deletions src/Persistence/CachePersistenceAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,11 @@ public function read(DataTableInterface $dataTable, PersistenceSubjectInterface

private function getCacheKey(DataTableInterface $dataTable, PersistenceSubjectInterface $subject): string
{
$parts = [
$dataTable->getConfig()->getName(),
return urlencode(implode('_', array_filter([
$dataTable->getName(),
$this->prefix,
$subject->getDataTablePersistenceIdentifier(),
];

return u(implode('_', array_filter($parts)))->snake()->toString();
])));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Resources/config/core.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@

$services
->set('kreyu_data_table.proxy_query.factory.doctrine_orm', DoctrineOrmProxyQueryFactory::class)
->tag('kreyu_data_table.proxy_query.factory', ['priority' => -1])
->tag('kreyu_data_table.proxy_query.factory')
;

$services
Expand Down
42 changes: 42 additions & 0 deletions tests/Unit/Persistence/CachePersistenceAdapterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

namespace Kreyu\Bundle\DataTableBundle\Tests\Unit\Persistence;

use Kreyu\Bundle\DataTableBundle\DataTableInterface;
use Kreyu\Bundle\DataTableBundle\Persistence\CachePersistenceAdapter;
use Kreyu\Bundle\DataTableBundle\Persistence\PersistenceSubjectInterface;
use PHPUnit\Framework\TestCase;
use Symfony\Contracts\Cache\CacheInterface;

class CachePersistenceAdapterTest extends TestCase
{
public function testGetTagName()
{
$persistenceSubject = $this->createMock(PersistenceSubjectInterface::class);
$persistenceSubject->method('getDataTablePersistenceIdentifier')->willReturn('foo');

$this->assertEquals('kreyu_data_table_persistence_foo', CachePersistenceAdapter::getTagName($persistenceSubject));
}

public function testItUrlEncodesCacheKeyToPreventReservedCharactersError()
{
// The '%' is not reserved, but it should be encoded anyway to prevent overlapping of identifiers.
// This would happen when, for example, one subject had identifier set to "@", and another had "%40" (already encoded, probably an edge case).
$reservedCharacters = '{}()/\\@%';

$cache = $this->createMock(CacheInterface::class);
$cache->expects($this->exactly(2))->method('get')->with('products_%7B%7D%28%29%2F%5C%40%25_foo_%7B%7D%28%29%2F%5C%40%25_id_%7B%7D%28%29%2F%5C%40%25');

$dataTable = $this->createMock(DataTableInterface::class);
$dataTable->method('getName')->willReturn("products_$reservedCharacters");

$persistenceSubject = $this->createMock(PersistenceSubjectInterface::class);
$persistenceSubject->method('getDataTablePersistenceIdentifier')->willReturn("id_$reservedCharacters");

$adapter = new CachePersistenceAdapter($cache, "foo_$reservedCharacters");
$adapter->write($dataTable, $persistenceSubject, null);
$adapter->read($dataTable, $persistenceSubject);
}
}

0 comments on commit a45165c

Please sign in to comment.