msd Backup hinzugefügt

This commit is contained in:
aschwarz
2023-07-25 19:16:12 +02:00
parent 50297c1d25
commit 1d3ed789b5
680 changed files with 103120 additions and 0 deletions

View File

@ -0,0 +1,93 @@
<?php
/**
* This file is part of the Cache package.
*
* Copyright (c) Daniel González
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @author Daniel González <daniel@desarrolla2.com>
*/
namespace Desarrolla2\Test\Cache;
use Cache\IntegrationTests\SimpleCacheTest;
use Desarrolla2\Cache\Exception\InvalidArgumentException;
/**
* AbstractCacheTest
*/
abstract class AbstractCacheTest extends SimpleCacheTest
{
/**
* @return array
*/
public function dataProviderForOptions()
{
return [
['ttl', 100],
['prefix', 'test']
];
}
/**
* @dataProvider dataProviderForOptions
*
* @param string $key
* @param mixed $value
*/
public function testWithOption($key, $value)
{
$cache = $this->cache->withOption($key, $value);
$this->assertEquals($value, $cache->getOption($key));
// Check immutability
$this->assertNotSame($this->cache, $cache);
$this->assertNotEquals($value, $this->cache->getOption($key));
}
public function testWithOptions()
{
$data = $this->dataProviderForOptions();
$options = array_combine(array_column($data, 0), array_column($data, 1));
$cache = $this->cache->withOptions($options);
foreach ($options as $key => $value) {
$this->assertEquals($value, $cache->getOption($key));
}
// Check immutability
$this->assertNotSame($this->cache, $cache);
foreach ($options as $key => $value) {
$this->assertNotEquals($value, $this->cache->getOption($key));
}
}
/**
* @return array
*/
public function dataProviderForOptionsException()
{
return [
['ttl', 0, InvalidArgumentException::class],
['foo', 'bar', InvalidArgumentException::class]
];
}
/**
* @dataProvider dataProviderForOptionsException
*
* @param string $key
* @param mixed $value
* @param string $expectedException
*/
public function testWithOptionException($key, $value, $expectedException)
{
$this->expectException($expectedException);
$this->createSimpleCache()->withOption($key, $value);
}
}

View File

@ -0,0 +1,44 @@
<?php
/*
* This file is part of the Cache package.
*
* Copyright (c) Daniel González
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @author Daniel González <daniel@desarrolla2.com>
*/
namespace Desarrolla2\Test\Cache;
use Desarrolla2\Cache\Apcu as ApcuCache;
/**
* ApcuCacheTest
*/
class ApcuCacheTest extends AbstractCacheTest
{
public static function setUpBeforeClass(): void
{
// Required to check the TTL for new entries
ini_set('apc.use_request_time', false);
}
public function createSimpleCache()
{
if (!extension_loaded('apcu')) {
$this->markTestSkipped(
'The APCu extension is not available.'
);
}
if (!ini_get('apc.enable_cli')) {
$this->markTestSkipped(
'You need to enable apc.enable_cli'
);
}
return new ApcuCache();
}
}

View File

@ -0,0 +1,216 @@
<?php
/*
* This file is part of the Cache package.
*
* Copyright (c) Daniel González
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @author Daniel González <daniel@desarrolla2.com>
*/
namespace Desarrolla2\Test\Cache;
use Desarrolla2\Cache\Chain as CacheChain;
use Desarrolla2\Cache\Memory as MemoryCache;
/**
* ChainTest
*/
class ChainTest extends AbstractCacheTest
{
public function createSimpleCache()
{
$adapters = [new MemoryCache()]; // For the general PSR-16 tests, we don't need more than 1 adapter
return new CacheChain($adapters);
}
public function tearDown(): void
{
// No need to clear cache, as the adapters don't persist between tests.
}
public function testChainSet()
{
$adapter1 = $this->createMock(MemoryCache::class);
$adapter1->expects($this->once())->method('set')->with("foo", "bar", 300);
$adapter2 = $this->createMock(MemoryCache::class);
$adapter2->expects($this->once())->method('set')->with("foo", "bar", 300);
$cache = new CacheChain([$adapter1, $adapter2]);
$cache->set("foo", "bar", 300);
}
public function testChainSetMultiple()
{
$adapter1 = $this->createMock(MemoryCache::class);
$adapter1->expects($this->once())->method('setMultiple')->with(["foo" => 1, "bar" => 2], 300);
$adapter2 = $this->createMock(MemoryCache::class);
$adapter2->expects($this->once())->method('setMultiple')->with(["foo" => 1, "bar" => 2], 300);
$cache = new CacheChain([$adapter1, $adapter2]);
$cache->setMultiple(["foo" => 1, "bar" => 2], 300);
}
public function testChainGetFirst()
{
$adapter1 = $this->createMock(MemoryCache::class);
$adapter1->expects($this->once())->method('get')->with("foo")->willReturn("bar");
$adapter2 = $this->createMock(MemoryCache::class);
$adapter2->expects($this->never())->method('get');
$cache = new CacheChain([$adapter1, $adapter2]);
$this->assertEquals("bar", $cache->get("foo", 42));
}
public function testChainGetSecond()
{
$adapter1 = $this->createMock(MemoryCache::class);
$adapter1->expects($this->once())->method('get')->with("foo")->willReturn(null);
$adapter2 = $this->createMock(MemoryCache::class);
$adapter2->expects($this->once())->method('get')->with("foo")->willReturn("car");
$cache = new CacheChain([$adapter1, $adapter2]);
$this->assertEquals("car", $cache->get("foo", 42));
}
public function testChainGetNone()
{
$adapter1 = $this->createMock(MemoryCache::class);
$adapter1->expects($this->once())->method('get')->with("foo")->willReturn(null);
$adapter2 = $this->createMock(MemoryCache::class);
$adapter2->expects($this->once())->method('get')->with("foo")->willReturn(null);
$cache = new CacheChain([$adapter1, $adapter2]);
$this->assertEquals(42, $cache->get("foo", 42));
}
public function testChainGetMultipleFirst()
{
$adapter1 = $this->createMock(MemoryCache::class);
$adapter1->expects($this->once())->method('getMultiple')->with(["foo", "bar"])
->willReturn(["foo" => 1, "bar" => 2]);
$adapter2 = $this->createMock(MemoryCache::class);
$adapter2->expects($this->never())->method('getMultiple');
$cache = new CacheChain([$adapter1, $adapter2]);
$this->assertEquals(["foo" => 1, "bar" => 2], $cache->getMultiple(["foo", "bar"]));
}
public function testChainGetMultipleMixed()
{
$adapter1 = $this->createMock(MemoryCache::class);
$adapter1->expects($this->once())->method('getMultiple')
->with($this->equalTo(["foo", "bar", "wux", "lot"]))
->willReturn(["foo" => null, "bar" => 2, "wux" => null, "lot" => null]);
$adapter2 = $this->createMock(MemoryCache::class);
$adapter2->expects($this->once())->method('getMultiple')
->with($this->equalTo(["foo", "wux", "lot"]))
->willReturn(["foo" => 11, "wux" => 15, "lot" => null]);
$cache = new CacheChain([$adapter1, $adapter2]);
$expected = ["foo" => 11, "bar" => 2, "wux" => 15, "lot" => 42];
$this->assertEquals($expected, $cache->getMultiple(["foo", "bar", "wux", "lot"], 42));
}
public function testChainHasFirst()
{
$adapter1 = $this->createMock(MemoryCache::class);
$adapter1->expects($this->once())->method('has')->with("foo")->willReturn(true);
$adapter2 = $this->createMock(MemoryCache::class);
$adapter2->expects($this->never())->method('has');
$cache = new CacheChain([$adapter1, $adapter2]);
$this->assertTrue($cache->has("foo"));
}
public function testChainHasSecond()
{
$adapter1 = $this->createMock(MemoryCache::class);
$adapter1->expects($this->once())->method('has')->with("foo")->willReturn(false);
$adapter2 = $this->createMock(MemoryCache::class);
$adapter2->expects($this->once())->method('has')->with("foo")->willReturn(true);
$cache = new CacheChain([$adapter1, $adapter2]);
$this->assertTrue($cache->has("foo"));
}
public function testChainHasNone()
{
$adapter1 = $this->createMock(MemoryCache::class);
$adapter1->expects($this->once())->method('has')->with("foo")->willReturn(false);
$adapter2 = $this->createMock(MemoryCache::class);
$adapter2->expects($this->once())->method('has')->with("foo")->willReturn(false);
$cache = new CacheChain([$adapter1, $adapter2]);
$this->assertFalse($cache->has("foo"));
}
public function testChainDelete()
{
$adapter1 = $this->createMock(MemoryCache::class);
$adapter1->expects($this->once())->method('delete')->with("foo");
$adapter2 = $this->createMock(MemoryCache::class);
$adapter2->expects($this->once())->method('delete')->with("foo");
$cache = new CacheChain([$adapter1, $adapter2]);
$cache->delete("foo");
}
public function testChainDeleteMultiple()
{
$adapter1 = $this->createMock(MemoryCache::class);
$adapter1->expects($this->once())->method('deleteMultiple')->with(["foo", "bar"]);
$adapter2 = $this->createMock(MemoryCache::class);
$adapter2->expects($this->once())->method('deleteMultiple')->with(["foo", "bar"]);
$cache = new CacheChain([$adapter1, $adapter2]);
$cache->deleteMultiple(["foo", "bar"]);
}
public function testChainClear()
{
$adapter1 = $this->createMock(MemoryCache::class);
$adapter1->expects($this->once())->method('clear');
$adapter2 = $this->createMock(MemoryCache::class);
$adapter2->expects($this->once())->method('clear');
$cache = new CacheChain([$adapter1, $adapter2]);
$cache->clear();
}
}

View File

@ -0,0 +1,40 @@
<?php
/*
* This file is part of the Cache package.
*
* Copyright (c) Daniel González
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @author Daniel González <daniel@desarrolla2.com>
*/
namespace Desarrolla2\Test\Cache;
use Desarrolla2\Cache\File as FileCache;
use org\bovigo\vfs\vfsStream;
use org\bovigo\vfs\vfsStreamDirectory;
/**
* FileTest
*/
class FileTest extends AbstractCacheTest
{
/**
* @var vfsStreamDirectory
*/
private $root;
protected $skippedTests = [
'testBasicUsageWithLongKey' => 'Only support keys up to 64 bytes'
];
public function createSimpleCache()
{
$this->root = vfsStream::setup('cache');
return new FileCache(vfsStream::url('cache'));
}
}

View File

@ -0,0 +1,43 @@
<?php
/**
* This file is part of the Cache package.
*
* Copyright (c) Daniel González
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @author Daniel González <daniel@desarrolla2.com>
* @author Arnold Daniels <arnold@jasny.net>
*/
namespace Desarrolla2\Test\Cache;
use Desarrolla2\Cache\File as FileCache;
use Desarrolla2\Cache\File\TrieFilename;
use org\bovigo\vfs\vfsStream;
use org\bovigo\vfs\vfsStreamDirectory;
/**
* FileTest with Trie structure
*/
class FileTrieTest extends AbstractCacheTest
{
/**
* @var vfsStreamDirectory
*/
private $root;
protected $skippedTests = [
'testBasicUsageWithLongKey' => 'Only support keys up to 64 bytes'
];
public function createSimpleCache()
{
$this->root = vfsStream::setup('cache');
return (new FileCache(vfsStream::url('cache')))
->withOption('filename', new TrieFilename('%s.php.cache',4));
}
}

View File

@ -0,0 +1,42 @@
<?php
/**
* This file is part of the Cache package.
*
* Copyright (c) Daniel González
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @author Daniel González <daniel@desarrolla2.com>
* @author Arnold Daniels <arnold@jasny.net>
*/
namespace Desarrolla2\Test\Cache;
use Desarrolla2\Cache\File as FileCache;
use org\bovigo\vfs\vfsStream;
use org\bovigo\vfs\vfsStreamDirectory;
/**
* FileTest
*/
class FileTtlFileTest extends AbstractCacheTest
{
/**
* @var vfsStreamDirectory
*/
private $root;
protected $skippedTests = [
'testBasicUsageWithLongKey' => 'Only support keys up to 64 bytes'
];
public function createSimpleCache()
{
$this->root = vfsStream::setup('cache');
return (new FileCache(vfsStream::url('cache')))
->withOption('ttl-strategy', 'file');
}
}

View File

@ -0,0 +1,47 @@
<?php
/*
* This file is part of the Cache package.
*
* Copyright (c) Daniel González
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @author Daniel González <daniel@desarrolla2.com>
*/
namespace Desarrolla2\Test\Cache;
use Desarrolla2\Cache\Memcached as MemcachedCache;
use Memcached;
/**
* MemcachedTest
*/
class MemcachedTest extends AbstractCacheTest
{
protected $skippedTests = [
'testBasicUsageWithLongKey' => 'Only support keys up to 250 bytes'
];
public function createSimpleCache()
{
if (!extension_loaded('memcached') || !class_exists('\Memcached')) {
$this->markTestSkipped(
'The Memcached extension is not available.'
);
}
list($host, $port) = explode(':', CACHE_TESTS_MEMCACHED_SERVER) + [1 => 11211];
$adapter = new Memcached();
$adapter->addServer($host, (int)$port);
if (!$adapter->flush()) {
$this->markTestSkipped("Unable to flush Memcached; not running?");
}
return new MemcachedCache($adapter);
}
}

View File

@ -0,0 +1,44 @@
<?php
/*
* This file is part of the Cache package.
*
* Copyright (c) Daniel González
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @author Daniel González <daniel@desarrolla2.com>
*/
namespace Desarrolla2\Test\Cache;
use Desarrolla2\Cache\Memory as MemoryCache;
/**
* MemoryTest
*/
class MemoryTest extends AbstractCacheTest
{
public function createSimpleCache()
{
return new MemoryCache();
}
public function tearDown(): void
{
// No need to clear cache, as the adapters don't persist between tests.
}
public function testExceededLimit()
{
$cache = $this->createSimpleCache()->withOption('limit', 1);
$cache->set('foo', 1);
$this->assertTrue($cache->has('foo'));
$cache->set('bar', 1);
$this->assertFalse($cache->has('foo'));
$this->assertTrue($cache->has('bar'));
}
}

View File

@ -0,0 +1,53 @@
<?php
/*
* This file is part of the Cache package.
*
* Copyright (c) Daniel González
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @author Daniel González <daniel@desarrolla2.com>
*/
namespace Desarrolla2\Test\Cache;
use Desarrolla2\Cache\MongoDB as MongoDBCache;
use MongoDB\Client;
/**
* MongoDBTest
*/
class MongoDBTest extends AbstractCacheTest
{
/**
* @var Client
*/
protected static $client;
/**
* Use one client per test, as the MongoDB extension leaves connections open
*/
public static function setUpBeforeClass(): void
{
if (!extension_loaded('mongodb')) {
return;
}
self::$client = new Client(CACHE_TESTS_MONGO_DSN);
self::$client->listDatabases(); // Fail if unable to connect
}
public function createSimpleCache()
{
if (!isset(self::$client)) {
$this->markTestSkipped('The mongodb extension is not available');
}
$collection = self::$client->selectCollection(CACHE_TESTS_MONGO_DATABASE, 'cache');
return (new MongoDBCache($collection))
->withOption('initialize', false);
}
}

View File

@ -0,0 +1,75 @@
<?php
/**
* This file is part of the Cache package.
*
* Copyright (c) Daniel González
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @author Daniel González <daniel@desarrolla2.com>
*/
namespace Desarrolla2\Test\Cache;
use Desarrolla2\Cache\Mysqli as MysqliCache;
/**
* MysqliTest
*/
class MysqliTest extends AbstractCacheTest
{
/**
* @var \mysqli
*/
protected static $mysqli;
protected $skippedTests = [
'testBasicUsageWithLongKey' => 'Only support keys up to 255 bytes'
];
public static function setUpBeforeClass(): void
{
if (class_exists('mysqli')) {
static::$mysqli = new \mysqli(
ini_get('mysqli.default_host') ?: 'localhost',
ini_get('mysqli.default_user') ?: 'root'
);
}
}
public function init(): void
{
if (!class_exists('mysqli')) {
$this->markTestSkipped("mysqli extension not loaded");
}
try {
static::$mysqli->query('CREATE DATABASE IF NOT EXISTS `' . CACHE_TESTS_MYSQLI_DATABASE . '`');
static::$mysqli->select_db(CACHE_TESTS_MYSQLI_DATABASE);
static::$mysqli->query("CREATE TABLE IF NOT EXISTS `cache` "
."( `key` VARCHAR(255), `value` BLOB, `ttl` INT UNSIGNED, PRIMARY KEY (`key`) )");
} catch (\Exception $e) {
$this->markTestSkipped("skipping mysqli test; " . $e->getMessage());
}
if (static::$mysqli->error) {
$this->markTestSkipped(static::$mysqli->error);
}
}
public function createSimpleCache()
{
$this->init();
return (new MysqliCache(static::$mysqli))
->withOption('initialize', false);
}
public static function tearDownAfterClass(): void
{
static::$mysqli->query('DROP DATABASE IF EXISTS `' . CACHE_TESTS_MYSQLI_DATABASE . '`');
static::$mysqli->close();
}
}

View File

@ -0,0 +1,88 @@
<?php
/*
* This file is part of the Cache package.
*
* Copyright (c) Daniel González
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @author Daniel González <daniel@desarrolla2.com>
*/
namespace Desarrolla2\Test\Cache;
use Desarrolla2\Cache\NotCache as NotCache;
use PHPUnit\Framework\TestCase;
/**
* NotCacheTest
*/
class NotCacheTest extends TestCase
{
/**
* @var \Desarrolla2\Cache\NotCache
*/
protected $cache;
public function setUp(): void
{
$this->cache = new NotCache();
}
/**
* @return array
*/
public function dataProvider()
{
return array(
array(),
);
}
/**
* @dataProvider dataProvider
*/
public function testHas()
{
$this->cache->set('key', 'value');
$this->assertFalse($this->cache->has('key'));
}
/**
* @dataProvider dataProvider
*/
public function testGet()
{
$this->cache->set('key', 'value');
$this->assertFalse($this->cache->get('key', false));
}
/**
* @dataProvider dataProvider
*/
public function testSet()
{
$this->assertFalse($this->cache->set('key', 'value'));
}
/**
* @dataProvider dataProvider
*/
public function testDelete()
{
$this->assertTrue($this->cache->delete('key'));
}
/**
* @dataProvider dataProvider
*/
public function testWithOption()
{
$cache = $this->cache->withOption('ttl', 3600);
$this->assertSame(3600, $cache->getOption('ttl'));
$this->assertNotSame($this->cache, $cache);
}
}

View File

@ -0,0 +1,40 @@
<?php
/*
* This file is part of the Cache package.
*
* Copyright (c) Daniel González
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @author Daniel González <daniel@desarrolla2.com>
*/
namespace Desarrolla2\Test\Cache;
use Desarrolla2\Cache\PhpFile as PhpFileCache;
use org\bovigo\vfs\vfsStream;
use org\bovigo\vfs\vfsStreamDirectory;
/**
* FileTest with PhpPacker
*/
class PhpFileTest extends AbstractCacheTest
{
/**
* @var vfsStreamDirectory
*/
private $root;
protected $skippedTests = [
'testBasicUsageWithLongKey' => 'Only support keys up to 64 bytes'
];
public function createSimpleCache()
{
$this->root = vfsStream::setup('cache');
return new PhpFileCache(vfsStream::url('cache'));
}
}

View File

@ -0,0 +1,52 @@
<?php
/*
* This file is part of the Cache package.
*
* Copyright (c) Daniel González
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @author Daniel González <daniel@desarrolla2.com>
*/
namespace Desarrolla2\Test\Cache;
use Desarrolla2\Cache\Predis as PredisCache;
use Predis\Client;
use Predis\Connection\ConnectionException;
/**
* PredisTest
*/
class PredisTest extends AbstractCacheTest
{
/**
* @var Client
*/
protected $client;
public function createSimpleCache()
{
if (!class_exists('Predis\Client')) {
$this->markTestSkipped('The predis library is not available');
}
try {
$this->client = new Client(CACHE_TESTS_PREDIS_DSN, ['exceptions' => false]);
$this->client->connect();
} catch (ConnectionException $e) {
$this->markTestSkipped($e->getMessage());
}
return new PredisCache($this->client);
}
public function tearDown(): void
{
parent::tearDown();
$this->client->disconnect();
}
}

View File

@ -0,0 +1,21 @@
<?php
/*
* This file is part of the Cache package.
*
* Copyright (c) Daniel González
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @author Daniel González <daniel@desarrolla2.com>
*/
require_once __DIR__.'/../bootstrap.php';
use Desarrolla2\Cache\Cache;
use Desarrolla2\Cache\Adapter\Apcu;
$cache = new Cache(new Apcu());
require_once __DIR__.'/common.php';

View File

@ -0,0 +1,21 @@
<?php
/*
* This file is part of the Cache package.
*
* Copyright (c) Daniel González
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @author Daniel González <daniel@desarrolla2.com>
*/
require_once __DIR__.'/../bootstrap.php';
use Desarrolla2\Cache\Cache;
use Desarrolla2\Cache\Adapter\File;
$cache = new Cache(new File('/tmp'));
require_once __DIR__.'/common.php';

View File

@ -0,0 +1,21 @@
<?php
/*
* This file is part of the Cache package.
*
* Copyright (c) Daniel González
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @author Daniel González <daniel@desarrolla2.com>
*/
require_once __DIR__.'/../bootstrap.php';
use Desarrolla2\Cache\Cache;
use Desarrolla2\Cache\Adapter\Mongo;
$cache = new Cache(new Mongo('mongodb://localhost:27017'));
require_once __DIR__.'/common.php';

View File

@ -0,0 +1,21 @@
<?php
/*
* This file is part of the Cache package.
*
* Copyright (c) Daniel González
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @author Daniel González <daniel@desarrolla2.com>
*/
require_once __DIR__.'/../bootstrap.php';
use Desarrolla2\Cache\Cache;
use Desarrolla2\Cache\Adapter\NotCache;
$cache = new Cache(new NotCache());
require_once __DIR__.'/common.php';

View File

@ -0,0 +1,43 @@
<?php
/*
* This file is part of the Cache package.
*
* Copyright (c) Daniel González
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @author Daniel González <daniel@desarrolla2.com>
*/
//build test data outside of timing loop
$data = [];
for ($i = 1; $i <= 10000; $i++) {
$data[$i] = md5($i);
}
$timer = new \Desarrolla2\Timer\Timer(new \Desarrolla2\Timer\Formatter\Human());
for ($i = 1; $i <= 10000; $i++) {
$cache->set($data[$i], $data[$i], 3600);
}
$timer->mark('10.000 set');
for ($i = 1; $i <= 10000; $i++) {
$cache->has($data[$i]);
}
$timer->mark('10.000 has');
for ($i = 1; $i <= 10000; $i++) {
$cache->get($data[$i]);
}
$timer->mark('10.000 get');
for ($i = 1; $i <= 10000; $i++) {
$cache->has($data[$i]);
$cache->get($data[$i]);
}
$timer->mark('10.000 has+get combos');
$benchmarks = $timer->getAll();
foreach ($benchmarks as $benchmark) {
ld($benchmark);
}