ProxyAdapterTest.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. public function testProxyfiedItem()
  30. {
  31. $this->expectException('Exception');
  32. $this->expectExceptionMessage('OK bar');
  33. $item = new CacheItem();
  34. $pool = new ProxyAdapter(new TestingArrayAdapter($item));
  35. $proxyItem = $pool->getItem('foo');
  36. $this->assertNotSame($item, $proxyItem);
  37. $pool->save($proxyItem->set('bar'));
  38. }
  39. }
  40. class TestingArrayAdapter extends ArrayAdapter
  41. {
  42. private $item;
  43. public function __construct(CacheItemInterface $item)
  44. {
  45. $this->item = $item;
  46. }
  47. public function getItem($key)
  48. {
  49. return $this->item;
  50. }
  51. public function save(CacheItemInterface $item)
  52. {
  53. if ($item === $this->item) {
  54. throw new \Exception('OK '.$item->get());
  55. }
  56. }
  57. }