Initial commit: apps.prometeysev.ru

This commit is contained in:
root committed 2026-10-22 00:22:57 +03:00
commit 96ccabcd8b
6561 files changed
+1270349

No files matched your search

@@ -0,0 +1,46 @@
<?php
/*
* This file is part of twig-cache-extension.
*
* (c) Alexander <iam.asm89@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Asm89\Twig\CacheExtension\Tests\CacheProvider;
use Asm89\Twig\CacheExtension\CacheProvider\DoctrineCacheAdapter;
class DoctrineCacheAdapterTest extends \PHPUnit_Framework_TestCase
{
public function testFetch()
{
$cacheMock = $this->createCacheMock();
$cacheMock->expects($this->any())
->method('fetch')
->will($this->returnValue('fromcache'));
$cache = new DoctrineCacheAdapter($cacheMock);
$this->assertEquals('fromcache', $cache->fetch('test'));
}
public function testSave()
{
$cacheMock = $this->createCacheMock();
$cacheMock->expects($this->once())
->method('save')
->with('key', 'value', 42);
$cache = new DoctrineCacheAdapter($cacheMock);
$cache->save('key', 'value', 42);
}
public function createCacheMock()
{
return $this->createMock('Doctrine\Common\Cache\Cache');
}
}
@@ -0,0 +1,80 @@
<?php
/*
* This file is part of twig-cache-extension.
*
* (c) Alexander <iam.asm89@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Asm89\Twig\CacheExtension\Tests\CacheStrategy;
use Asm89\Twig\CacheExtension\CacheStrategy\GenerationalCacheStrategy;
class GenerationalCacheStrategyTest extends \PHPUnit_Framework_TestCase
{
private $keyGeneratorMock;
private $cacheProviderMock;
public function createCacheStrategy($lifetime = 0)
{
$this->keyGeneratorMock = $this->createKeyGeneratorMock();
$this->cacheProviderMock = $this->createCacheProviderMock();
return new GenerationalCacheStrategy($this->cacheProviderMock, $this->keyGeneratorMock, $lifetime);
}
public function testGenerateKeyContainsAnnotation()
{
$strategy = $this->createCacheStrategy();
$this->keyGeneratorMock->expects($this->any())
->method('generateKey')
->will($this->returnValue('foo'));
$this->assertEquals('v42__GCS__foo', $strategy->generateKey('v42', 'value'));
}
/**
* @expectedException \Asm89\Twig\CacheExtension\Exception\InvalidCacheKeyException
*/
public function testGenerationKeyThrowsExceptionWhenKeyGeneratorReturnsNull()
{
$strategy = $this->createCacheStrategy();
$strategy->generateKey('v42', 'value');
}
/**
* @dataProvider getLifeTimes
*/
public function testSaveBlockUsesConfiguredLifetime($lifetime)
{
$strategy = $this->createCacheStrategy($lifetime);
$this->cacheProviderMock->expects($this->any())
->method('save')
->with('key', 'value', $lifetime)
->will($this->returnValue('foo'));
$strategy->saveBlock('key', 'value');
}
public function getLifetimes()
{
return array(
array(0),
array(60),
);
}
public function createKeyGeneratorMock()
{
return $this->createMock('Asm89\Twig\CacheExtension\CacheStrategy\KeyGeneratorInterface');
}
public function createCacheProviderMock()
{
return $this->createMock('Asm89\Twig\CacheExtension\CacheProviderInterface');
}
}
@@ -0,0 +1,80 @@
<?php
/*
* This file is part of twig-cache-extension.
*
* (c) Alexander <iam.asm89@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Asm89\Twig\CacheExtension\Tests\CacheStrategy;
use Asm89\Twig\CacheExtension\CacheStrategy\IndexedChainingCacheStrategy;
class IndexedChainingCacheStrategyTest extends \PHPUnit_Framework_TestCase
{
private $cacheStrategyMocks = array();
public function createCacheStrategy()
{
foreach($this->getStrategies() as $config) {
list($key, $return) = $config;
$cacheStrategyMock = $this->createCacheStrategyMock();
$cacheStrategyMock->expects($this->any())
->method('generateKey')
->will($this->returnValue($return));
$this->cacheStrategyMocks[$key] = $cacheStrategyMock;
}
return new IndexedChainingCacheStrategy($this->cacheStrategyMocks);
}
/**
* @dataProvider getStrategies
*/
public function testGenerateKeyProxiesToAppropriateStrategy($key, $return)
{
$strategy = $this->createCacheStrategy();
$generatedKey = $strategy->generateKey('v42', array($key => 'proxied_value'));
$this->assertEquals($return, $generatedKey['key']);
$this->assertEquals($key, $generatedKey['strategyKey']);
}
/**
* @expectedException \Asm89\Twig\CacheExtension\Exception\NonExistingStrategyKeyException
*/
public function testGenerateKeyThrowsExceptionOnMissingKey()
{
$strategy = $this->createCacheStrategy();
$strategy->generateKey('v42', 'proxied_value');
}
/**
* @expectedException \Asm89\Twig\CacheExtension\Exception\NonExistingStrategyException
* @expectedExceptionMessage No strategy configured with key "unknown"
*/
public function testGenerateKeyThrowsExceptionOnUnknownKey()
{
$strategy = $this->createCacheStrategy();
$strategy->generateKey('v42', array('unknown' => 'proxied_value'));
}
public function getStrategies()
{
return array(
array('foo', 'foo_key'),
array('bar', 'bar_key'),
);
}
public function createCacheStrategyMock()
{
return $this->createMock('Asm89\Twig\CacheExtension\CacheStrategyInterface');
}
}
@@ -0,0 +1,68 @@
<?php
/*
* This file is part of twig-cache-extension.
*
* (c) Alexander <iam.asm89@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Asm89\Twig\CacheExtension\Tests\CacheStrategy;
use Asm89\Twig\CacheExtension\CacheStrategy\LifetimeCacheStrategy;
class LifetimeCacheStrategyTest extends \PHPUnit_Framework_TestCase
{
private $cacheProviderMock;
public function createCacheStrategy()
{
$this->cacheProviderMock = $this->createCacheProviderMock();
return new LifetimeCacheStrategy($this->cacheProviderMock);
}
public function testGenerateKeyUsesGivenLifetime()
{
$strategy = $this->createCacheStrategy();
$key = $strategy->generateKey('v42', 42);
$this->assertEquals(42, $key['lifetime']);
}
public function testGenerateKeyAnnotatesKey()
{
$strategy = $this->createCacheStrategy();
$key = $strategy->generateKey('the_annotation', 42);
$this->assertContains('the_annotation', $key['key']);
}
/**
* @dataProvider getInvalidLifetimeValues
* @expectedException \Asm89\Twig\CacheExtension\Exception\InvalidCacheLifetimeException
*/
public function testGenerateKeyThrowsExceptionWhenNoLifetimeProvided($value)
{
$strategy = $this->createCacheStrategy();
$strategy->generateKey('v42', $value);
}
public function getInvalidLifetimeValues()
{
return array(
array('foo'),
array(array('foo')),
);
}
public function createCacheProviderMock()
{
return $this->createMock('Asm89\Twig\CacheExtension\CacheProviderInterface');
}
}
@@ -0,0 +1,182 @@
<?php
/*
* This file is part of twig-cache-extension.
*
* (c) Alexander <iam.asm89@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Asm89\Twig\CacheExtension\Tests;
use Asm89\Twig\CacheExtension\CacheProvider\DoctrineCacheAdapter;
use Asm89\Twig\CacheExtension\CacheStrategy\KeyGeneratorInterface;
use Asm89\Twig\CacheExtension\CacheStrategy\GenerationalCacheStrategy;
use Asm89\Twig\CacheExtension\CacheStrategy\IndexedChainingCacheStrategy;
use Asm89\Twig\CacheExtension\CacheStrategy\LifetimeCacheStrategy;
use Asm89\Twig\CacheExtension\Extension;
use Doctrine\Common\Cache\ArrayCache;
use Twig_Loader_Filesystem;
use Twig_Environment;
class FunctionalExtensionTest extends \PHPUnit_Framework_TestCase
{
private $cache;
protected function createCacheProvider()
{
$this->cache = new ArrayCache();
return new DoctrineCacheAdapter($this->cache);
}
protected function createCacheStrategy($name = null)
{
$cacheProvider = $this->createCacheProvider();
$keyGenerator = $this->createKeyGenerator();
$lifetime = 0;
switch ($name) {
case 'time':
return new LifetimeCacheStrategy($cacheProvider);
case 'indexed':
return new IndexedChainingCacheStrategy(array(
'gcs' => new GenerationalCacheStrategy($cacheProvider, $keyGenerator, $lifetime),
'time' => new LifetimeCacheStrategy($cacheProvider),
));
default:
return new GenerationalCacheStrategy($cacheProvider, $keyGenerator, $lifetime);
}
}
protected function createKeyGenerator()
{
return new KeyGenerator();
}
protected function createTwig($cacheStrategyName = null)
{
$loader = new Twig_Loader_Filesystem(__DIR__ . '/fixtures/');
$twig = new Twig_Environment($loader);
$cacheExtension = new Extension($this->createCacheStrategy($cacheStrategyName));
$twig->addExtension($cacheExtension);
return $twig;
}
protected function getValue($value, $updatedAt)
{
return new Value($value, $updatedAt);
}
public function testCachesWithSameCacheKey()
{
$twig = $this->createTwig();
$rendered = $twig->render('gcs_value.twig', array('value' => $this->getValue('asm89', 1)));
$this->assertEquals('Hello asm89!', $rendered);
$rendered2 = $twig->render('gcs_value.twig', array('value' => $this->getValue('fabpot', 1)));
$this->assertEquals('Hello asm89!', $rendered2);
}
public function testDifferentCacheOnDifferentAnnotation()
{
$twig = $this->createTwig();
$rendered = $twig->render('gcs_value.twig', array('value' => $this->getValue('asm89', 1)));
$this->assertEquals('Hello asm89!', $rendered);
$rendered2 = $twig->render('gcs_value.twig', array('value' => $this->getValue('fabpot', 1)));
$this->assertEquals('Hello asm89!', $rendered2);
$rendered3 = $twig->render('gcs_value_v2.twig', array('value' => $this->getValue('fabpot', 1)));
$this->assertEquals('Hello fabpot!', $rendered3);
}
public function testLifetimeCacheStrategy()
{
$twig = $this->createTwig('time');
$rendered = $twig->render('lcs_value.twig', array('value' => $this->getValue('asm89', 1)));
$this->assertEquals('Hello asm89!', $rendered);
$rendered2 = $twig->render('lcs_value.twig', array('value' => $this->getValue('fabpot', 1)));
$this->assertEquals('Hello asm89!', $rendered2);
$this->cache->flushAll();
$rendered3 = $twig->render('lcs_value.twig', array('value' => $this->getValue('fabpot', 1)));
$this->assertEquals('Hello fabpot!', $rendered3);
}
public function testIndexedChainingStrategy()
{
$twig = $this->createTwig('indexed');
$rendered = $twig->render('ics_value.twig', array('value' => $this->getValue('asm89', 1)));
$this->assertEquals('Hello asm89!', $rendered);
$rendered2 = $twig->render('ics_value.twig', array('value' => $this->getValue('fabpot', 1)));
$this->assertEquals('Hello asm89!', $rendered2);
$this->cache->flushAll();
$rendered3 = $twig->render('ics_value.twig', array('value' => $this->getValue('fabpot', 1)));
$this->assertEquals('Hello fabpot!', $rendered3);
}
/**
* @expectedException Twig_Error_Runtime
* @expectedExceptionMessage An exception has been thrown during the rendering of a template ("No strategy key found in value.")
*/
public function testIndexedChainingStrategyNeedsKey()
{
$twig = $this->createTwig('indexed');
$twig->render('ics_no_key.twig', array('value' => $this->getValue('asm89', 1)));
}
public function testAnnotationExpression()
{
$twig = $this->createTwig('indexed');
$rendered = $twig->render('annotation_expression.twig', array('value' => $this->getValue('asm89', 1), 'version' => 1));
$this->assertEquals('Hello asm89!Hello asm89!', $rendered);
}
}
class KeyGenerator implements KeyGeneratorInterface
{
public function generateKey($value)
{
return get_class($value) . '_' . $value->getUpdatedAt();
}
}
class Value
{
private $value;
private $updatedAt;
public function __construct($value, $updatedAt)
{
$this->value = $value;
$this->updatedAt = $updatedAt;
}
public function getUpdatedAt()
{
return $this->updatedAt;
}
public function __toString()
{
return $this->value;
}
}
@@ -0,0 +1,3 @@
{%- set k = "v'" ~ version -%}
{%- cache k {time: 10} -%}Hello {{ value }}!{%- endcache -%}
{%- cache 'bob'|capitalize {gcs: value} %}Hello {{ value }}!{%- endcache -%}
@@ -0,0 +1,2 @@
{% set annotation = 'v1' %}
{% cache annotation value %}Hello {{ value }}!{% endcache %}
@@ -0,0 +1 @@
{% cache 'v1' value %}Hello {{ value }}!{% endcache %}
@@ -0,0 +1 @@
{% cache 'v2' value %}Hello {{ value }}!{% endcache %}
@@ -0,0 +1 @@
{% cache 'v1' 10 %}Hello {{ value }}!{% endcache %}
@@ -0,0 +1 @@
{% cache 'v1' {time: 10} %}Hello {{ value }}!{% endcache %}
@@ -0,0 +1 @@
{% cache 'v1' 10 %}Hello {{ value }}!{% endcache %}