mirror of
https://github.com/RSS-Bridge/rss-bridge.git
synced 2025-08-07 00:56:34 +02:00
Reformat codebase v4 (#2872)
Reformat code base to PSR12 Co-authored-by: rssbridge <noreply@github.com>
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* AtomFormat - RFC 4287: The Atom Syndication Format
|
||||
* https://tools.ietf.org/html/rfc4287
|
||||
@@ -10,18 +11,20 @@ require_once __DIR__ . '/BaseFormatTest.php';
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class AtomFormatTest extends BaseFormatTest {
|
||||
private const PATH_EXPECTED = self::PATH_SAMPLES . 'expectedAtomFormat/';
|
||||
class AtomFormatTest extends BaseFormatTest
|
||||
{
|
||||
private const PATH_EXPECTED = self::PATH_SAMPLES . 'expectedAtomFormat/';
|
||||
|
||||
/**
|
||||
* @dataProvider sampleProvider
|
||||
* @runInSeparateProcess
|
||||
*/
|
||||
public function testOutput(string $name, string $path) {
|
||||
$data = $this->formatData('Atom', $this->loadSample($path));
|
||||
$this->assertNotFalse(simplexml_load_string($data));
|
||||
/**
|
||||
* @dataProvider sampleProvider
|
||||
* @runInSeparateProcess
|
||||
*/
|
||||
public function testOutput(string $name, string $path)
|
||||
{
|
||||
$data = $this->formatData('Atom', $this->loadSample($path));
|
||||
$this->assertNotFalse(simplexml_load_string($data));
|
||||
|
||||
$expected = self::PATH_EXPECTED . $name . '.xml';
|
||||
$this->assertXmlStringEqualsXmlFile($expected, $data);
|
||||
}
|
||||
$expected = self::PATH_EXPECTED . $name . '.xml';
|
||||
$this->assertXmlStringEqualsXmlFile($expected, $data);
|
||||
}
|
||||
}
|
||||
|
@@ -5,59 +5,65 @@ namespace RssBridge\Tests\Formats;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use FormatFactory;
|
||||
|
||||
abstract class BaseFormatTest extends TestCase {
|
||||
protected const PATH_SAMPLES = __DIR__ . '/samples/';
|
||||
abstract class BaseFormatTest extends TestCase
|
||||
{
|
||||
protected const PATH_SAMPLES = __DIR__ . '/samples/';
|
||||
|
||||
/**
|
||||
* @return array<string, array{string, string}>
|
||||
*/
|
||||
public function sampleProvider() {
|
||||
$samples = [];
|
||||
foreach (glob(self::PATH_SAMPLES . '*.json') as $path) {
|
||||
$name = basename($path, '.json');
|
||||
$samples[$name] = [
|
||||
$name,
|
||||
$path,
|
||||
];
|
||||
}
|
||||
return $samples;
|
||||
}
|
||||
/**
|
||||
* @return array<string, array{string, string}>
|
||||
*/
|
||||
public function sampleProvider()
|
||||
{
|
||||
$samples = [];
|
||||
foreach (glob(self::PATH_SAMPLES . '*.json') as $path) {
|
||||
$name = basename($path, '.json');
|
||||
$samples[$name] = [
|
||||
$name,
|
||||
$path,
|
||||
];
|
||||
}
|
||||
return $samples;
|
||||
}
|
||||
|
||||
/**
|
||||
* Cannot be part of the sample returned by sampleProvider since this modifies $_SERVER
|
||||
* and thus needs to be run in a separate process to avoid side effects.
|
||||
*/
|
||||
protected function loadSample(string $path): \stdClass {
|
||||
$data = json_decode(file_get_contents($path), true);
|
||||
if (isset($data['meta']) && isset($data['items'])) {
|
||||
if (!empty($data['server']))
|
||||
$this->setServerVars($data['server']);
|
||||
/**
|
||||
* Cannot be part of the sample returned by sampleProvider since this modifies $_SERVER
|
||||
* and thus needs to be run in a separate process to avoid side effects.
|
||||
*/
|
||||
protected function loadSample(string $path): \stdClass
|
||||
{
|
||||
$data = json_decode(file_get_contents($path), true);
|
||||
if (isset($data['meta']) && isset($data['items'])) {
|
||||
if (!empty($data['server'])) {
|
||||
$this->setServerVars($data['server']);
|
||||
}
|
||||
|
||||
$items = array();
|
||||
foreach($data['items'] as $item) {
|
||||
$items[] = new \FeedItem($item);
|
||||
}
|
||||
$items = [];
|
||||
foreach ($data['items'] as $item) {
|
||||
$items[] = new \FeedItem($item);
|
||||
}
|
||||
|
||||
return (object)array(
|
||||
'meta' => $data['meta'],
|
||||
'items' => $items,
|
||||
);
|
||||
} else {
|
||||
$this->fail('invalid test sample: ' . basename($path, '.json'));
|
||||
}
|
||||
}
|
||||
return (object)[
|
||||
'meta' => $data['meta'],
|
||||
'items' => $items,
|
||||
];
|
||||
} else {
|
||||
$this->fail('invalid test sample: ' . basename($path, '.json'));
|
||||
}
|
||||
}
|
||||
|
||||
private function setServerVars(array $list): void {
|
||||
$_SERVER = array_merge($_SERVER, $list);
|
||||
}
|
||||
private function setServerVars(array $list): void
|
||||
{
|
||||
$_SERVER = array_merge($_SERVER, $list);
|
||||
}
|
||||
|
||||
protected function formatData(string $formatName, \stdClass $sample): string {
|
||||
$formatFac = new FormatFactory();
|
||||
$format = $formatFac->create($formatName);
|
||||
$format->setItems($sample->items);
|
||||
$format->setExtraInfos($sample->meta);
|
||||
$format->setLastModified(strtotime('2000-01-01 12:00:00 UTC'));
|
||||
protected function formatData(string $formatName, \stdClass $sample): string
|
||||
{
|
||||
$formatFac = new FormatFactory();
|
||||
$format = $formatFac->create($formatName);
|
||||
$format->setItems($sample->items);
|
||||
$format->setExtraInfos($sample->meta);
|
||||
$format->setLastModified(strtotime('2000-01-01 12:00:00 UTC'));
|
||||
|
||||
return $format->stringify();
|
||||
}
|
||||
return $format->stringify();
|
||||
}
|
||||
}
|
||||
|
@@ -2,39 +2,44 @@
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class FormatImplementationTest extends TestCase {
|
||||
private $class;
|
||||
private $obj;
|
||||
class FormatImplementationTest extends TestCase
|
||||
{
|
||||
private $class;
|
||||
private $obj;
|
||||
|
||||
/**
|
||||
* @dataProvider dataFormatsProvider
|
||||
*/
|
||||
public function testClassName($path) {
|
||||
$this->setFormat($path);
|
||||
$this->assertTrue($this->class === ucfirst($this->class), 'class name must start with uppercase character');
|
||||
$this->assertEquals(0, substr_count($this->class, ' '), 'class name must not contain spaces');
|
||||
$this->assertStringEndsWith('Format', $this->class, 'class name must end with "Format"');
|
||||
}
|
||||
/**
|
||||
* @dataProvider dataFormatsProvider
|
||||
*/
|
||||
public function testClassName($path)
|
||||
{
|
||||
$this->setFormat($path);
|
||||
$this->assertTrue($this->class === ucfirst($this->class), 'class name must start with uppercase character');
|
||||
$this->assertEquals(0, substr_count($this->class, ' '), 'class name must not contain spaces');
|
||||
$this->assertStringEndsWith('Format', $this->class, 'class name must end with "Format"');
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider dataFormatsProvider
|
||||
*/
|
||||
public function testClassType($path) {
|
||||
$this->setFormat($path);
|
||||
$this->assertInstanceOf(FormatInterface::class, $this->obj);
|
||||
}
|
||||
/**
|
||||
* @dataProvider dataFormatsProvider
|
||||
*/
|
||||
public function testClassType($path)
|
||||
{
|
||||
$this->setFormat($path);
|
||||
$this->assertInstanceOf(FormatInterface::class, $this->obj);
|
||||
}
|
||||
|
||||
public function dataFormatsProvider() {
|
||||
$formats = array();
|
||||
foreach (glob(PATH_LIB_FORMATS . '*.php') as $path) {
|
||||
$formats[basename($path, '.php')] = array($path);
|
||||
}
|
||||
return $formats;
|
||||
}
|
||||
public function dataFormatsProvider()
|
||||
{
|
||||
$formats = [];
|
||||
foreach (glob(PATH_LIB_FORMATS . '*.php') as $path) {
|
||||
$formats[basename($path, '.php')] = [$path];
|
||||
}
|
||||
return $formats;
|
||||
}
|
||||
|
||||
private function setFormat($path) {
|
||||
$this->class = basename($path, '.php');
|
||||
$this->assertTrue(class_exists($this->class), 'class ' . $this->class . ' doesn\'t exist');
|
||||
$this->obj = new $this->class();
|
||||
}
|
||||
private function setFormat($path)
|
||||
{
|
||||
$this->class = basename($path, '.php');
|
||||
$this->assertTrue(class_exists($this->class), 'class ' . $this->class . ' doesn\'t exist');
|
||||
$this->obj = new $this->class();
|
||||
}
|
||||
}
|
||||
|
@@ -1,4 +1,5 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* JsonFormat - JSON Feed Version 1
|
||||
* https://jsonfeed.org/version/1
|
||||
@@ -10,18 +11,20 @@ require_once __DIR__ . '/BaseFormatTest.php';
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class JsonFormatTest extends BaseFormatTest {
|
||||
private const PATH_EXPECTED = self::PATH_SAMPLES . 'expectedJsonFormat/';
|
||||
class JsonFormatTest extends BaseFormatTest
|
||||
{
|
||||
private const PATH_EXPECTED = self::PATH_SAMPLES . 'expectedJsonFormat/';
|
||||
|
||||
/**
|
||||
* @dataProvider sampleProvider
|
||||
* @runInSeparateProcess
|
||||
*/
|
||||
public function testOutput(string $name, string $path) {
|
||||
$data = $this->formatData('Json', $this->loadSample($path));
|
||||
$this->assertNotNull(json_decode($data), 'invalid JSON output: ' . json_last_error_msg());
|
||||
/**
|
||||
* @dataProvider sampleProvider
|
||||
* @runInSeparateProcess
|
||||
*/
|
||||
public function testOutput(string $name, string $path)
|
||||
{
|
||||
$data = $this->formatData('Json', $this->loadSample($path));
|
||||
$this->assertNotNull(json_decode($data), 'invalid JSON output: ' . json_last_error_msg());
|
||||
|
||||
$expected = self::PATH_EXPECTED . $name . '.json';
|
||||
$this->assertJsonStringEqualsJsonFile($expected, $data);
|
||||
}
|
||||
$expected = self::PATH_EXPECTED . $name . '.json';
|
||||
$this->assertJsonStringEqualsJsonFile($expected, $data);
|
||||
}
|
||||
}
|
||||
|
@@ -1,4 +1,5 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* MrssFormat - RSS 2.0 + Media RSS
|
||||
* http://www.rssboard.org/rss-specification
|
||||
@@ -11,18 +12,20 @@ require_once __DIR__ . '/BaseFormatTest.php';
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class MrssFormatTest extends BaseFormatTest {
|
||||
private const PATH_EXPECTED = self::PATH_SAMPLES . 'expectedMrssFormat/';
|
||||
class MrssFormatTest extends BaseFormatTest
|
||||
{
|
||||
private const PATH_EXPECTED = self::PATH_SAMPLES . 'expectedMrssFormat/';
|
||||
|
||||
/**
|
||||
* @dataProvider sampleProvider
|
||||
* @runInSeparateProcess
|
||||
*/
|
||||
public function testOutput(string $name, string $path) {
|
||||
$data = $this->formatData('Mrss', $this->loadSample($path));
|
||||
$this->assertNotFalse(simplexml_load_string($data));
|
||||
/**
|
||||
* @dataProvider sampleProvider
|
||||
* @runInSeparateProcess
|
||||
*/
|
||||
public function testOutput(string $name, string $path)
|
||||
{
|
||||
$data = $this->formatData('Mrss', $this->loadSample($path));
|
||||
$this->assertNotFalse(simplexml_load_string($data));
|
||||
|
||||
$expected = self::PATH_EXPECTED . $name . '.xml';
|
||||
$this->assertXmlStringEqualsXmlFile($expected, $data);
|
||||
}
|
||||
$expected = self::PATH_EXPECTED . $name . '.xml';
|
||||
$this->assertXmlStringEqualsXmlFile($expected, $data);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user