tests/Formats: Simplify by using a base class (#2779)

There is a lot of redundancy. Let’s not repeat ourselves.

Unfortunately, since we do not install PHPUnit as a project dependency on CI,
it does not use the composer’s PSR-4 autoloader and the tests are unable to find
the `BaseFormatTest` class.

Until we resolve that, let’s load the class explicitly.
This commit is contained in:
Jan Tojnar 2022-06-08 02:17:32 +02:00 committed by GitHub
parent 6582a66a2d
commit 12ddee4054
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 91 additions and 171 deletions

View File

@ -6,70 +6,22 @@
namespace RssBridge\Tests\Formats; namespace RssBridge\Tests\Formats;
use FormatFactory; require_once __DIR__ . '/BaseFormatTest.php';
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
class AtomFormatTest extends TestCase { class AtomFormatTest extends BaseFormatTest {
const PATH_SAMPLES = __DIR__ . '/samples/'; private const PATH_EXPECTED = self::PATH_SAMPLES . 'expectedAtomFormat/';
const PATH_EXPECTED = __DIR__ . '/samples/expectedAtomFormat/';
private $sample;
private $format;
private $data;
/** /**
* @dataProvider sampleProvider * @dataProvider sampleProvider
* @runInSeparateProcess * @runInSeparateProcess
*/ */
public function testOutput($path) { public function testOutput(string $name, string $path) {
$this->setSample($path); $data = $this->formatData('Atom', $this->loadSample($path));
$this->initFormat(); $this->assertNotFalse(simplexml_load_string($data));
$this->assertXmlStringEqualsXmlFile($this->sample->expected, $this->data); $expected = self::PATH_EXPECTED . $name . '.xml';
} $this->assertXmlStringEqualsXmlFile($expected, $data);
public function sampleProvider() {
$samples = array();
foreach (glob(self::PATH_SAMPLES . '*.json') as $path) {
$samples[basename($path, '.json')] = array($path);
}
return $samples;
}
private function setSample($path) {
$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);
}
$this->sample = (object)array(
'meta' => $data['meta'],
'items' => $items,
'expected' => self::PATH_EXPECTED . basename($path, '.json') . '.xml'
);
} else {
$this->fail('invalid test sample: ' . basename($path, '.json'));
}
}
private function setServerVars($list) {
$_SERVER = array_merge($_SERVER, $list);
}
private function initFormat() {
$formatFac = new FormatFactory();
$formatFac->setWorkingDir(PATH_LIB_FORMATS);
$this->format = $formatFac->create('Atom');
$this->format->setItems($this->sample->items);
$this->format->setExtraInfos($this->sample->meta);
$this->format->setLastModified(strtotime('2000-01-01 12:00:00 UTC'));
$this->data = $this->format->stringify();
$this->assertNotFalse(simplexml_load_string($this->data));
} }
} }

View File

@ -0,0 +1,64 @@
<?php
namespace RssBridge\Tests\Formats;
use PHPUnit\Framework\TestCase;
use FormatFactory;
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;
}
/**
* 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);
}
return (object)array(
'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);
}
protected function formatData(string $formatName, \stdClass $sample): string {
$formatFac = new FormatFactory();
$formatFac->setWorkingDir(PATH_LIB_FORMATS);
$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();
}
}

View File

@ -6,70 +6,22 @@
namespace RssBridge\Tests\Formats; namespace RssBridge\Tests\Formats;
use FormatFactory; require_once __DIR__ . '/BaseFormatTest.php';
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
class JsonFormatTest extends TestCase { class JsonFormatTest extends BaseFormatTest {
const PATH_SAMPLES = __DIR__ . '/samples/'; private const PATH_EXPECTED = self::PATH_SAMPLES . 'expectedJsonFormat/';
const PATH_EXPECTED = __DIR__ . '/samples/expectedJsonFormat/';
private $sample;
private $format;
private $data;
/** /**
* @dataProvider sampleProvider * @dataProvider sampleProvider
* @runInSeparateProcess * @runInSeparateProcess
*/ */
public function testOutput($path) { public function testOutput(string $name, string $path) {
$this->setSample($path); $data = $this->formatData('Json', $this->loadSample($path));
$this->initFormat(); $this->assertNotNull(json_decode($data), 'invalid JSON output: ' . json_last_error_msg());
$this->assertJsonStringEqualsJsonFile($this->sample->expected, $this->data); $expected = self::PATH_EXPECTED . $name . '.json';
} $this->assertJsonStringEqualsJsonFile($expected, $data);
public function sampleProvider() {
$samples = array();
foreach (glob(self::PATH_SAMPLES . '*.json') as $path) {
$samples[basename($path, '.json')] = array($path);
}
return $samples;
}
private function setSample($path) {
$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);
}
$this->sample = (object)array(
'meta' => $data['meta'],
'items' => $items,
'expected' => self::PATH_EXPECTED . basename($path)
);
} else {
$this->fail('invalid test sample: ' . basename($path, '.json'));
}
}
private function setServerVars($list) {
$_SERVER = array_merge($_SERVER, $list);
}
private function initFormat() {
$formatFac = new FormatFactory();
$formatFac->setWorkingDir(PATH_LIB_FORMATS);
$this->format = $formatFac->create('Json');
$this->format->setItems($this->sample->items);
$this->format->setExtraInfos($this->sample->meta);
$this->format->setLastModified(strtotime('2000-01-01 12:00:00 UTC'));
$this->data = $this->format->stringify();
$this->assertNotNull(json_decode($this->data), 'invalid JSON output: ' . json_last_error_msg());
} }
} }

View File

@ -7,70 +7,22 @@
namespace RssBridge\Tests\Formats; namespace RssBridge\Tests\Formats;
use FormatFactory; require_once __DIR__ . '/BaseFormatTest.php';
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
class MrssFormatTest extends TestCase { class MrssFormatTest extends BaseFormatTest {
const PATH_SAMPLES = __DIR__ . '/samples/'; private const PATH_EXPECTED = self::PATH_SAMPLES . 'expectedMrssFormat/';
const PATH_EXPECTED = __DIR__ . '/samples/expectedMrssFormat/';
private $sample;
private $format;
private $data;
/** /**
* @dataProvider sampleProvider * @dataProvider sampleProvider
* @runInSeparateProcess * @runInSeparateProcess
*/ */
public function testOutput($path) { public function testOutput(string $name, string $path) {
$this->setSample($path); $data = $this->formatData('Mrss', $this->loadSample($path));
$this->initFormat(); $this->assertNotFalse(simplexml_load_string($data));
$this->assertXmlStringEqualsXmlFile($this->sample->expected, $this->data); $expected = self::PATH_EXPECTED . $name . '.xml';
} $this->assertXmlStringEqualsXmlFile($expected, $data);
public function sampleProvider() {
$samples = array();
foreach (glob(self::PATH_SAMPLES . '*.json') as $path) {
$samples[basename($path, '.json')] = array($path);
}
return $samples;
}
private function setSample($path) {
$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);
}
$this->sample = (object)array(
'meta' => $data['meta'],
'items' => $items,
'expected' => self::PATH_EXPECTED . basename($path, '.json') . '.xml'
);
} else {
$this->fail('invalid test sample: ' . basename($path, '.json'));
}
}
private function setServerVars($list) {
$_SERVER = array_merge($_SERVER, $list);
}
private function initFormat() {
$formatFac = new FormatFactory();
$formatFac->setWorkingDir(PATH_LIB_FORMATS);
$this->format = $formatFac->create('Mrss');
$this->format->setItems($this->sample->items);
$this->format->setExtraInfos($this->sample->meta);
$this->format->setLastModified(strtotime('2000-01-01 12:00:00 UTC'));
$this->data = $this->format->stringify();
$this->assertNotFalse(simplexml_load_string($this->data));
} }
} }