diff options
author | 2019-01-05 13:20:11 +0100 | |
---|---|---|
committer | 2019-01-05 13:20:11 +0100 | |
commit | 8801ac9e64fe325dae8ef21ebc3b1526d1a5befc (patch) | |
tree | d9205c797af59ce7d1c785d1c1e26667e7ba101a /tests/JsonFormatTest.php | |
parent | 288d4de218dec81035ffa06d2326ca15d9598e4e (diff) | |
download | rss-bridge-8801ac9e64fe325dae8ef21ebc3b1526d1a5befc.tar.gz rss-bridge-8801ac9e64fe325dae8ef21ebc3b1526d1a5befc.tar.zst rss-bridge-8801ac9e64fe325dae8ef21ebc3b1526d1a5befc.zip |
format: Refactor JsonFormat to JSON Feed version 1 (#988)
JsonFormat now implements https://jsonfeed.org/version/1
Closes #618
Diffstat (limited to 'tests/JsonFormatTest.php')
-rw-r--r-- | tests/JsonFormatTest.php | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/tests/JsonFormatTest.php b/tests/JsonFormatTest.php new file mode 100644 index 00000000..24d09067 --- /dev/null +++ b/tests/JsonFormatTest.php @@ -0,0 +1,89 @@ +<?php +/** + * JsonFormat - JSON Feed Version 1 + * https://jsonfeed.org/version/1 + */ +require_once __DIR__ . '/../lib/rssbridge.php'; + +use PHPUnit\Framework\TestCase; + +class JsonFormatTest extends TestCase { + const PATH_SAMPLES = __DIR__ . '/samples/'; + const PATH_EXPECTED = __DIR__ . '/samples/expectedJsonFormat/'; + + private $sample; + private $format; + private $data; + + /** + * @dataProvider sampleProvider + * @runInSeparateProcess + * @requires function xdebug_get_headers + */ + public function testHeaders($path) { + $this->setSample($path); + $this->initFormat(); + + $this->assertContains( + 'Content-Type: application/json; charset=' . $this->format->getCharset(), + xdebug_get_headers() + ); + } + + /** + * @dataProvider sampleProvider + * @runInSeparateProcess + */ + public function testOutput($path) { + $this->setSample($path); + $this->initFormat(); + + $this->assertJsonStringEqualsJsonFile($this->sample->expected, $this->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() { + $this->format = \Format::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->getActualOutput($this->format->display()); + $this->assertNotNull(json_decode($this->data), 'invalid JSON output: ' . json_last_error_msg()); + ob_clean(); + } +} |