ProxyAdapterTest.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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\ProxyAdapter;
  14. use Symfony\Component\Cache\CacheItem;
  15. /**
  16. * @group time-sensitive
  17. */
  18. class ProxyAdapterTest extends AdapterTestCase
  19. {
  20. protected $skippedTests = [
  21. 'testDeferredSaveWithoutCommit' => 'Assumes a shared cache which ArrayAdapter is not.',
  22. 'testSaveWithoutExpire' => 'Assumes a shared cache which ArrayAdapter is not.',
  23. 'testPrune' => 'ProxyAdapter just proxies',
  24. ];
  25. public function createCachePool($defaultLifetime = 0)
  26. {
  27. return new ProxyAdapter(new ArrayAdapter(), '', $defaultLifetime);
  28. }
  29. /**
  30. * @expectedException \Exception
  31. * @expectedExceptionMessage OK bar
  32. */
  33. public function testProxyfiedItem()
  34. {
  35. $item = new CacheItem();
  36. $pool = new ProxyAdapter(new TestingArrayAdapter($item));
  37. $proxyItem = $pool->getItem('foo');
  38. $this->assertNotSame($item, $proxyItem);
  39. $pool->save($proxyItem->set('bar'));
  40. }
  41. }
  42. class TestingArrayAdapter extends ArrayAdapter
  43. {
  44. private $item;
  45. public function __construct(CacheItemInterface $item)
  46. {
  47. $this->item = $item;
  48. }
  49. public function getItem($key)
  50. {
  51. return $this->item;
  52. }
  53. public function save(CacheItemInterface $item)
  54. {
  55. if ($item === $this->item) {
  56. throw new \Exception('OK '.$item->get());
  57. }
  58. }
  59. }