ProxyAdapterTest.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Cache\Tests\Adapter;
  11. use Psr\Cache\CacheItemInterface;
  12. use Symfony\Component\Cache\Adapter\ArrayAdapter;
  13. use Symfony\Component\Cache\Adapter\FilesystemAdapter;
  14. use Symfony\Component\Cache\Adapter\ProxyAdapter;
  15. use Symfony\Component\Cache\CacheItem;
  16. /**
  17. * @group time-sensitive
  18. */
  19. class ProxyAdapterTest extends AdapterTestCase
  20. {
  21. protected $skippedTests = [
  22. 'testDeferredSaveWithoutCommit' => 'Assumes a shared cache which ArrayAdapter is not.',
  23. 'testSaveWithoutExpire' => 'Assumes a shared cache which ArrayAdapter is not.',
  24. 'testPrune' => 'ProxyAdapter just proxies',
  25. ];
  26. public function createCachePool($defaultLifetime = 0, $testMethod = null)
  27. {
  28. if ('testGetMetadata' === $testMethod) {
  29. return new ProxyAdapter(new FilesystemAdapter(), '', $defaultLifetime);
  30. }
  31. return new ProxyAdapter(new ArrayAdapter(), '', $defaultLifetime);
  32. }
  33. public function testProxyfiedItem()
  34. {
  35. $this->expectException('Exception');
  36. $this->expectExceptionMessage('OK bar');
  37. $item = new CacheItem();
  38. $pool = new ProxyAdapter(new TestingArrayAdapter($item));
  39. $proxyItem = $pool->getItem('foo');
  40. $this->assertNotSame($item, $proxyItem);
  41. $pool->save($proxyItem->set('bar'));
  42. }
  43. }
  44. class TestingArrayAdapter extends ArrayAdapter
  45. {
  46. private $item;
  47. public function __construct(CacheItemInterface $item)
  48. {
  49. $this->item = $item;
  50. }
  51. public function getItem($key)
  52. {
  53. return $this->item;
  54. }
  55. public function save(CacheItemInterface $item)
  56. {
  57. if ($item === $this->item) {
  58. throw new \Exception('OK '.$item->get());
  59. }
  60. }
  61. }