test: add failing mastodon test (#3255)

* fix: refactor cache factory

* test: add failing test

* add null cache
This commit is contained in:
Dag 2023-02-15 21:22:37 +01:00 committed by GitHub
parent 787b4d7cad
commit c27a300e02
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 69 additions and 50 deletions

30
caches/NullCache.php Normal file
View File

@ -0,0 +1,30 @@
<?php
declare(strict_types=1);
class NullCache implements CacheInterface
{
public function setScope($scope)
{
}
public function setKey($key)
{
}
public function loadData()
{
}
public function saveData($data)
{
}
public function getTime()
{
}
public function purgeCache($seconds)
{
}
}

View File

@ -24,8 +24,7 @@ max_filesize = 20
[cache]
; Defines the cache type used by RSS-Bridge
; "file" = FileCache (default)
; Cache type: file, sqlite, memcached, null
type = "file"
; Allow users to specify custom timeout for specific requests.

View File

@ -1,69 +1,42 @@
<?php
/**
* This file is part of RSS-Bridge, a PHP project capable of generating RSS and
* Atom feeds for websites that don't have one.
*
* For the full license information, please view the UNLICENSE file distributed
* with this source code.
*
* @package Core
* @license http://unlicense.org/ UNLICENSE
* @link https://github.com/rss-bridge/rss-bridge
*/
declare(strict_types=1);
class CacheFactory
{
private $folder;
private $cacheNames;
public function __construct(string $folder = PATH_LIB_CACHES)
{
$this->folder = $folder;
// create cache names
foreach (scandir($this->folder) as $file) {
if (preg_match('/^([^.]+)Cache\.php$/U', $file, $m)) {
$this->cacheNames[] = $m[1];
}
}
}
/**
* @param string|null $name The name of the cache e.g. "File", "Memcached" or "SQLite"
*/
public function create(string $name = null): CacheInterface
{
$name ??= Configuration::getConfig('cache', 'type');
$name = $this->sanitizeCacheName($name) . 'Cache';
if (! preg_match('/^[A-Z][a-zA-Z0-9-]*$/', $name)) {
throw new \InvalidArgumentException('Cache name invalid!');
if (!$name) {
throw new \Exception('No cache type configured');
}
$filePath = $this->folder . $name . '.php';
if (!file_exists($filePath)) {
throw new \Exception('Invalid cache');
$cacheNames = [];
foreach (scandir(PATH_LIB_CACHES) as $file) {
if (preg_match('/^([^.]+)Cache\.php$/U', $file, $m)) {
$cacheNames[] = $m[1];
}
$className = '\\' . $name;
return new $className();
}
protected function sanitizeCacheName(string $name)
{
// Trim trailing '.php' if exists
if (preg_match('/(.+)(?:\.php)/', $name, $matches)) {
$name = $matches[1];
}
// Trim trailing 'Cache' if exists
if (preg_match('/(.+)(?:Cache)$/i', $name, $matches)) {
$name = $matches[1];
}
if (in_array(strtolower($name), array_map('strtolower', $this->cacheNames))) {
$index = array_search(strtolower($name), array_map('strtolower', $this->cacheNames));
return $this->cacheNames[$index];
if (in_array(strtolower($name), array_map('strtolower', $cacheNames))) {
$index = array_search(strtolower($name), array_map('strtolower', $cacheNames));
$name = $cacheNames[$index];
} else {
throw new \InvalidArgumentException(sprintf('Invalid cache name: "%s"', $name));
}
return null;
if (! preg_match('/^[A-Z][a-zA-Z0-9-]*$/', $name)) {
throw new \InvalidArgumentException(sprintf('Invalid cache name: "%s"', $name));
}
$className = $name . 'Cache';
if (!file_exists(PATH_LIB_CACHES . $className . '.php')) {
throw new \Exception('Unable to find the cache file');
}
return new $className();
}
}

View File

@ -0,0 +1,17 @@
<?php
namespace RssBridge\Tests\Bridges;
use PHPUnit\Framework\TestCase;
class MastodonBridgeTest extends TestCase
{
public function test()
{
\Configuration::loadConfiguration(['cache' => ['type' => 'null']]);
$b = new \MastodonBridge();
// https://bird.makeup/users/asmongold/remote_follow
$b->setDatas(['canusername' => '@asmongold@bird.makeup']);
$b->collectData();
}
}