|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Kreyu\Bundle\DataTableBundle\Tests\Unit\Persistence; |
| 6 | + |
| 7 | +use Kreyu\Bundle\DataTableBundle\DataTableInterface; |
| 8 | +use Kreyu\Bundle\DataTableBundle\Persistence\CachePersistenceAdapter; |
| 9 | +use Kreyu\Bundle\DataTableBundle\Persistence\PersistenceSubjectInterface; |
| 10 | +use PHPUnit\Framework\TestCase; |
| 11 | +use Symfony\Contracts\Cache\CacheInterface; |
| 12 | + |
| 13 | +class CachePersistenceAdapterTest extends TestCase |
| 14 | +{ |
| 15 | + public function testGetTagName() |
| 16 | + { |
| 17 | + $persistenceSubject = $this->createMock(PersistenceSubjectInterface::class); |
| 18 | + $persistenceSubject->method('getDataTablePersistenceIdentifier')->willReturn('foo'); |
| 19 | + |
| 20 | + $this->assertEquals('kreyu_data_table_persistence_foo', CachePersistenceAdapter::getTagName($persistenceSubject)); |
| 21 | + } |
| 22 | + |
| 23 | + public function testItUrlEncodesCacheKeyToPreventReservedCharactersError() |
| 24 | + { |
| 25 | + // The '%' is not reserved, but it should be encoded anyway to prevent overlapping of identifiers. |
| 26 | + // This would happen when, for example, one subject had identifier set to "@", and another had "%40" (already encoded, probably an edge case). |
| 27 | + $reservedCharacters = '{}()/\\@%'; |
| 28 | + |
| 29 | + $cache = $this->createMock(CacheInterface::class); |
| 30 | + $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'); |
| 31 | + |
| 32 | + $dataTable = $this->createMock(DataTableInterface::class); |
| 33 | + $dataTable->method('getName')->willReturn("products_$reservedCharacters"); |
| 34 | + |
| 35 | + $persistenceSubject = $this->createMock(PersistenceSubjectInterface::class); |
| 36 | + $persistenceSubject->method('getDataTablePersistenceIdentifier')->willReturn("id_$reservedCharacters"); |
| 37 | + |
| 38 | + $adapter = new CachePersistenceAdapter($cache, "foo_$reservedCharacters"); |
| 39 | + $adapter->write($dataTable, $persistenceSubject, null); |
| 40 | + $adapter->read($dataTable, $persistenceSubject); |
| 41 | + } |
| 42 | +} |
0 commit comments