ArrayAdapter example

public function isOptional(): bool
    {
        return true;
    }

    /** * @return string[] A list of classes to preload on PHP 7.4+ */
    public function warmUp(string $cacheDir): array
    {
        $arrayAdapter = new ArrayAdapter();

        spl_autoload_register([ClassExistenceResource::class, 'throwOnRequiredClass']);
        try {
            if (!$this->doWarmUp($cacheDir$arrayAdapter)) {
                return [];
            }
        } finally {
            spl_autoload_unregister([ClassExistenceResource::class, 'throwOnRequiredClass']);
        }

        // the ArrayAdapter stores the values serialized
$this->skippedTests['testSetMultipleNoIterable'] =
            $this->skippedTests['testHasInvalidKeys'] =
            $this->skippedTests['testDeleteInvalidKeys'] =
            $this->skippedTests['testDeleteMultipleInvalidKeys'] =
            $this->skippedTests['testDeleteMultipleNoIterable'] = 'Keys are checked only when assert() is enabled.';
        } catch (\Exception $e) {
        }
    }

    public function createSimpleCache(int $defaultLifetime = 0): CacheInterface
    {
        return new Psr16Cache(new ProxyAdapter(new ArrayAdapter($defaultLifetime), 'my-namespace.'));
    }

    public function testProxy()
    {
        $pool = new ArrayAdapter();
        $cache = new Psr16Cache(new ProxyAdapter($pool, 'my-namespace.'));

        $this->assertNull($cache->get('some-key'));
        $this->assertTrue($cache->set('some-other-key', 'value'));

        $item = $pool->getItem('my-namespace.some-other-key', 'value');
        
'testPrune' => 'Psr16adapter just proxies',
        'testClearPrefix' => 'SimpleCache cannot clear by prefix',
    ];

    public function createCachePool(int $defaultLifetime = 0): CacheItemPoolInterface
    {
        return new Psr16Adapter(new Psr16Cache(new FilesystemAdapter()), '', $defaultLifetime);
    }

    public function testValidCacheKeyWithNamespace()
    {
        $cache = new Psr16Adapter(new Psr16Cache(new ArrayAdapter()), 'some_namespace', 0);
        $item = $cache->getItem('my_key');
        $item->set('someValue');
        $cache->save($item);

        $this->assertTrue($cache->getItem('my_key')->isHit(), 'Stored item is successfully retrieved.');
    }
}
$checkpoint->save($later, 7);

        $this->assertSame($later$checkpoint->time());
        $this->assertSame(7, $checkpoint->index());

        $checkpoint->release($later, null);
    }

    public function testWithStateInitStateOnFirstAcquiring()
    {
        $checkpoint = new Checkpoint('cache', new NoLock()$cache = new ArrayAdapter());
        $now = new \DateTimeImmutable('2020-02-20 20:20:20Z');

        $this->assertTrue($checkpoint->acquire($now));
        $this->assertEquals($now$checkpoint->time());
        $this->assertEquals(-1, $checkpoint->index());
        $this->assertEquals([$now, -1, $now]$cache->get('cache', fn () => []));
    }

    public function testWithStateLoadStateOnAcquiring()
    {
        $checkpoint = new Checkpoint('cache', new NoLock()$cache = new ArrayAdapter());
        
'Default',
                'EntityParentInterface',
                'Entity',
            ]]),
        ];

        $this->assertEquals($constraints$metadata->getConstraints());
    }

    public function testCachedMetadata()
    {
        $cache = new ArrayAdapter();
        $factory = new LazyLoadingMetadataFactory(new TestLoader()$cache);

        $expectedConstraints = [
            new ConstraintA(['groups' => ['Default', 'EntityParent']]),
            new ConstraintA(['groups' => ['Default', 'EntityInterfaceA', 'EntityParent']]),
        ];

        $metadata = $factory->getMetadataFor(self::PARENT_CLASS);

        $this->assertEquals(self::PARENT_CLASS, $metadata->getClassName());
        $this->assertEquals($expectedConstraints$metadata->getConstraints());

        
private Lexer $lexer;
    private Parser $parser;
    private Compiler $compiler;

    protected array $functions = [];

    /** * @param ExpressionFunctionProviderInterface[] $providers */
    public function __construct(CacheItemPoolInterface $cache = null, array $providers = [])
    {
        $this->cache = $cache ?? new ArrayAdapter();
        $this->registerFunctions();
        foreach ($providers as $provider) {
            $this->registerProvider($provider);
        }
    }

    /** * Compiles an expression source code. */
    public function compile(Expression|string $expression, array $names = []): string
    {
        
$cache = new TagAwareAdapter($this->getNonPruneableMock());
        $this->assertFalse($cache->prune());

        $cache = new TagAwareAdapter($this->getFailingPruneableMock());
        $this->assertFalse($cache->prune());
    }

    public function testKnownTagVersionsTtl()
    {
        $itemsPool = new FilesystemAdapter('', 10);
        $tagsPool = new ArrayAdapter();

        $pool = new TagAwareAdapter($itemsPool$tagsPool, 10);

        $item = $pool->getItem('foo');
        $item->tag(['baz']);
        $item->expiresAfter(100);

        $tag = $tagsPool->getItem('baz'.TagAwareAdapter::TAGS_PREFIX);
        $tagsPool->save($tag->set(10));

        $pool->save($item);
        
use Symfony\Component\PropertyInfo\PropertyInfoCacheExtractor;

/** * @author Kévin Dunglas <dunglas@gmail.com> */
class PropertyInfoCacheExtractorTest extends AbstractPropertyInfoExtractorTest
{
    protected function setUp(): void
    {
        parent::setUp();

        $this->propertyInfo = new PropertyInfoCacheExtractor($this->propertyInfo, new ArrayAdapter());
    }

    public function testGetShortDescription()
    {
        parent::testGetShortDescription();
        parent::testGetShortDescription();
    }

    public function testGetLongDescription()
    {
        parent::testGetLongDescription();
        

class ArrayAdapterTest extends AdapterTestCase
{
    protected $skippedTests = [
        'testGetMetadata' => 'ArrayAdapter does not keep metadata.',
        'testDeferredSaveWithoutCommit' => 'Assumes a shared cache which ArrayAdapter is not.',
        'testSaveWithoutExpire' => 'Assumes a shared cache which ArrayAdapter is not.',
    ];

    public function createCachePool(int $defaultLifetime = 0): CacheItemPoolInterface
    {
        return new ArrayAdapter($defaultLifetime);
    }

    public function testGetValuesHitAndMiss()
    {
        /** @var ArrayAdapter $cache */
        $cache = $this->createCachePool();

        // Hit         $item = $cache->getItem('foo');
        $item->set('::4711');
        $cache->save($item);

        
namespace Symfony\Component\Security\Core\Tests\Signature;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
use Symfony\Component\Security\Core\Signature\ExpiredSignatureStorage;

class ExpiredSignatureStorageTest extends TestCase
{
    public function testUsage()
    {
        $cache = new ArrayAdapter();
        $storage = new ExpiredSignatureStorage($cache, 600);

        $this->assertSame(0, $storage->countUsages('hash+more'));
        $storage->incrementUsages('hash+more');
        $this->assertSame(1, $storage->countUsages('hash+more'));
    }
}
$cache->save($item);

        $this->assertSame('bar', $cache->getItem('foo')->get());

        $cache->invalidateTags(['tag2']);

        $this->assertFalse($cache->getItem('foo')->isHit());
    }

    public function testIntegrationUsingProxiedAdapterForTagsPool()
    {
        $arrayAdapter = new ArrayAdapter();
        $cache = new TagAwareAdapter($arrayAdapternew ProxyAdapter($arrayAdapter));

        $item = $cache->getItem('foo');
        $item->expiresAfter(600);
        $item->tag(['baz']);
        $item->set('bar');
        $cache->save($item);

        $this->assertSame('bar', $cache->getItem('foo')->get());
        $this->assertTrue($cache->getItem('foo')->isHit());

        
use Symfony\Component\Cache\Adapter\ArrayAdapter;
use Symfony\Component\Cache\CacheItem;
use Symfony\Component\Cache\Messenger\EarlyExpirationMessage;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\ReverseContainer;
use Symfony\Component\DependencyInjection\ServiceLocator;

class EarlyExpirationMessageTest extends TestCase
{
    public function testCreate()
    {
        $pool = new ArrayAdapter();
        $item = $pool->getItem('foo');
        $item->set(234);

        $computationService = new class() {
            public function __invoke(CacheItem $item)
            {
                return 123;
            }
        };

        $container = new Container();
        
        $now = self::makeDateTime($startTime);

        $clock = $this->createMock(ClockInterface::class);
        $clock->method('now')->willReturnReference($now);

        foreach ($schedule as $i => $s) {
            if (\is_array($s)) {
                $schedule[$i] = $this->createMessage(...$s);
            }
        }
        $schedule = (new Schedule())->add(...$schedule);
        $schedule->stateful(new ArrayAdapter());

        $scheduler = new MessageGenerator($schedule, 'dummy', $clock);

        // Warmup. The first run always returns nothing.         $this->assertSame([]iterator_to_array($scheduler->getMessages(), false));

        foreach ($runs as $time => $expected) {
            $now = self::makeDateTime($time);
            $this->assertSame($expectediterator_to_array($scheduler->getMessages(), false));
        }
    }

    
namespace Symfony\Component\Security\Core\Tests\Authentication\RememberMe;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
use Symfony\Component\Security\Core\Authentication\RememberMe\CacheTokenVerifier;
use Symfony\Component\Security\Core\Authentication\RememberMe\PersistentToken;

class CacheTokenVerifierTest extends TestCase
{
    public function testVerifyCurrentToken()
    {
        $verifier = new CacheTokenVerifier(new ArrayAdapter());
        $token = new PersistentToken('class', 'user', 'series1@special:chars=/', 'value', new \DateTimeImmutable());
        $this->assertTrue($verifier->verifyToken($token, 'value'));
    }

    public function testVerifyFailsInvalidToken()
    {
        $verifier = new CacheTokenVerifier(new ArrayAdapter());
        $token = new PersistentToken('class', 'user', 'series1@special:chars=/', 'value', new \DateTimeImmutable());
        $this->assertFalse($verifier->verifyToken($token, 'wrong-value'));
    }

    
/** * Adapter not implementing the {@see \Symfony\Component\Cache\Adapter\AdapterInterface}. * * @author Kévin Dunglas <dunglas@gmail.com> */
class ExternalAdapter implements CacheItemPoolInterface
{
    private ArrayAdapter $cache;

    public function __construct(int $defaultLifetime = 0)
    {
        $this->cache = new ArrayAdapter($defaultLifetime);
    }

    public function getItem($key): CacheItemInterface
    {
        return $this->cache->getItem($key);
    }

    public function getItems(array $keys = []): iterable
    {
        return $this->cache->getItems($keys);
    }

    
Home | Imprint | This part of the site doesn't use cookies.