aboutsummaryrefslogtreecommitdiff
path: root/tests/Formats/BaseFormatTest.php
blob: 94da7b04d49829984ed16e456c5ee9297d120ea9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?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();
		$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();
	}
}