aboutsummaryrefslogtreecommitdiff
path: root/tests/Formats/BaseFormatTest.php
diff options
context:
space:
mode:
authorGravatar Jan Tojnar <jtojnar@gmail.com> 2022-06-08 02:17:32 +0200
committerGravatar GitHub <noreply@github.com> 2022-06-08 02:17:32 +0200
commit12ddee4054f58fb467cb58b9b74cec21aaad80c7 (patch)
tree7f2d5e929051874c5a2d57f3252c557bb4b99ae9 /tests/Formats/BaseFormatTest.php
parent6582a66a2d800c899acb0c88880c46a7288f9e21 (diff)
downloadrss-bridge-12ddee4054f58fb467cb58b9b74cec21aaad80c7.tar.gz
rss-bridge-12ddee4054f58fb467cb58b9b74cec21aaad80c7.tar.zst
rss-bridge-12ddee4054f58fb467cb58b9b74cec21aaad80c7.zip
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.
Diffstat (limited to 'tests/Formats/BaseFormatTest.php')
-rw-r--r--tests/Formats/BaseFormatTest.php64
1 files changed, 64 insertions, 0 deletions
diff --git a/tests/Formats/BaseFormatTest.php b/tests/Formats/BaseFormatTest.php
new file mode 100644
index 00000000..dac0e341
--- /dev/null
+++ b/tests/Formats/BaseFormatTest.php
@@ -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();
+ }
+}