mirror of
https://github.com/RSS-Bridge/rss-bridge.git
synced 2025-04-16 20:00:55 +00:00
test: add failing mastodon test (#3255)
* fix: refactor cache factory * test: add failing test * add null cache
This commit is contained in:
parent
787b4d7cad
commit
c27a300e02
30
caches/NullCache.php
Normal file
30
caches/NullCache.php
Normal 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)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
@ -24,8 +24,7 @@ max_filesize = 20
|
|||||||
|
|
||||||
[cache]
|
[cache]
|
||||||
|
|
||||||
; Defines the cache type used by RSS-Bridge
|
; Cache type: file, sqlite, memcached, null
|
||||||
; "file" = FileCache (default)
|
|
||||||
type = "file"
|
type = "file"
|
||||||
|
|
||||||
; Allow users to specify custom timeout for specific requests.
|
; Allow users to specify custom timeout for specific requests.
|
||||||
|
@ -1,69 +1,42 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
/**
|
declare(strict_types=1);
|
||||||
* 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
|
|
||||||
*/
|
|
||||||
|
|
||||||
class CacheFactory
|
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
|
public function create(string $name = null): CacheInterface
|
||||||
{
|
{
|
||||||
$name ??= Configuration::getConfig('cache', 'type');
|
$name ??= Configuration::getConfig('cache', 'type');
|
||||||
$name = $this->sanitizeCacheName($name) . 'Cache';
|
if (!$name) {
|
||||||
|
throw new \Exception('No cache type configured');
|
||||||
if (! preg_match('/^[A-Z][a-zA-Z0-9-]*$/', $name)) {
|
|
||||||
throw new \InvalidArgumentException('Cache name invalid!');
|
|
||||||
}
|
}
|
||||||
|
$cacheNames = [];
|
||||||
$filePath = $this->folder . $name . '.php';
|
foreach (scandir(PATH_LIB_CACHES) as $file) {
|
||||||
if (!file_exists($filePath)) {
|
if (preg_match('/^([^.]+)Cache\.php$/U', $file, $m)) {
|
||||||
throw new \Exception('Invalid cache');
|
$cacheNames[] = $m[1];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
$className = '\\' . $name;
|
|
||||||
return new $className();
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function sanitizeCacheName(string $name)
|
|
||||||
{
|
|
||||||
// Trim trailing '.php' if exists
|
// Trim trailing '.php' if exists
|
||||||
if (preg_match('/(.+)(?:\.php)/', $name, $matches)) {
|
if (preg_match('/(.+)(?:\.php)/', $name, $matches)) {
|
||||||
$name = $matches[1];
|
$name = $matches[1];
|
||||||
}
|
}
|
||||||
|
|
||||||
// Trim trailing 'Cache' if exists
|
// Trim trailing 'Cache' if exists
|
||||||
if (preg_match('/(.+)(?:Cache)$/i', $name, $matches)) {
|
if (preg_match('/(.+)(?:Cache)$/i', $name, $matches)) {
|
||||||
$name = $matches[1];
|
$name = $matches[1];
|
||||||
}
|
}
|
||||||
|
if (in_array(strtolower($name), array_map('strtolower', $cacheNames))) {
|
||||||
if (in_array(strtolower($name), array_map('strtolower', $this->cacheNames))) {
|
$index = array_search(strtolower($name), array_map('strtolower', $cacheNames));
|
||||||
$index = array_search(strtolower($name), array_map('strtolower', $this->cacheNames));
|
$name = $cacheNames[$index];
|
||||||
return $this->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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
17
tests/Bridges/MastodonBridgeTest.php
Normal file
17
tests/Bridges/MastodonBridgeTest.php
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user