From a45165cb46d93fc3ee9d6b32d58fb969a9ed63e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Wr=C3=B3blewski?= Date: Sun, 21 Jan 2024 22:03:11 +0100 Subject: [PATCH] wip --- src/Persistence/CachePersistenceAdapter.php | 8 ++-- src/Resources/config/core.php | 2 +- .../CachePersistenceAdapterTest.php | 42 +++++++++++++++++++ 3 files changed, 46 insertions(+), 6 deletions(-) create mode 100644 tests/Unit/Persistence/CachePersistenceAdapterTest.php diff --git a/src/Persistence/CachePersistenceAdapter.php b/src/Persistence/CachePersistenceAdapter.php index cf3f158f..2144c2c7 100755 --- a/src/Persistence/CachePersistenceAdapter.php +++ b/src/Persistence/CachePersistenceAdapter.php @@ -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(); + ]))); } /** diff --git a/src/Resources/config/core.php b/src/Resources/config/core.php index 0fce437a..32d6668e 100755 --- a/src/Resources/config/core.php +++ b/src/Resources/config/core.php @@ -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 diff --git a/tests/Unit/Persistence/CachePersistenceAdapterTest.php b/tests/Unit/Persistence/CachePersistenceAdapterTest.php new file mode 100644 index 00000000..732786f2 --- /dev/null +++ b/tests/Unit/Persistence/CachePersistenceAdapterTest.php @@ -0,0 +1,42 @@ +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); + } +}