mirror of
https://github.com/RSS-Bridge/rss-bridge.git
synced 2025-02-16 15:19:55 +03: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
4 changed files with 69 additions and 50 deletions
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]
|
||||
|
||||
; 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.
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
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…
Add table
Reference in a new issue