diff --git a/src/Writer/Version1/Renderer.php b/src/Writer/Version1/Renderer.php index ad18493..9f3798b 100644 --- a/src/Writer/Version1/Renderer.php +++ b/src/Writer/Version1/Renderer.php @@ -136,6 +136,10 @@ private function renderItem(Item $item) }, $attachments); } + if ($author = $item->getAuthor()) { + $result['author'] = $this->renderAuthor($author); + } + if ($extensions = $item->getExtensions()) { foreach ($extensions as $key => $extension) { $result['_'.$key] = $extension; diff --git a/test/Fixtures/authors.json b/test/Fixtures/authors.json new file mode 100644 index 0000000..2d71946 --- /dev/null +++ b/test/Fixtures/authors.json @@ -0,0 +1,26 @@ +{ + "version": "https://jsonfeed.org/version/1", + "title": "My Example Feed", + "feed_url": "https://example.org/feed.json", + "author": { + "name": "Global Author" + }, + "items": [ + { + "id": "2", + "content_text": "This is a second item.", + "url": "https://example.org/2", + "author": { + "name": "Author 2" + } + }, + { + "id": "1", + "content_html": "

This is the first item.

", + "url": "https://example.org/1", + "author": { + "name": "Author 1" + } + } + ] +} diff --git a/test/Reader/Version1/FeedReaderTest.php b/test/Reader/Version1/FeedReaderTest.php index 9814799..390c073 100644 --- a/test/Reader/Version1/FeedReaderTest.php +++ b/test/Reader/Version1/FeedReaderTest.php @@ -4,6 +4,7 @@ use DateTime; use JDecool\JsonFeed\Attachment; +use JDecool\JsonFeed\Author; use JDecool\JsonFeed\Item; use JDecool\JsonFeed\Reader\Version1\FeedReader; use PHPUnit\Framework\TestCase; @@ -94,6 +95,37 @@ public function testMicroblogFeed() $this->assertEquals($item, $items[0]); } + public function testAuthorsFeed() + { + $input = $this->getFixtures('authors'); + $reader = FeedReader::create(); + + $feed = $reader->readFromJson($input); + $this->assertInstanceOf('JDecool\JsonFeed\Feed', $feed); + $this->assertEquals('My Example Feed', $feed->getTitle()); + $this->assertEquals('Global Author', $feed->getAuthor()->getName()); + $this->assertEquals('https://example.org/feed.json', $feed->getFeedUrl()); + + $items = $feed->getItems(); + $this->assertCount(2, $items); + + $item2Author = new Author('Author 2'); + $item2 = new Item('2'); + $item2->setUrl('https://example.org/2'); + $item2->setContentText('This is a second item.'); + $item2->setAuthor($item2Author); + $this->assertEquals('Author 2', $item2->getAuthor()->getName()); + $this->assertEquals($item2, $items[0]); + + $item1Author = new Author('Author 1'); + $item1 = new Item('1'); + $item1->setUrl('https://example.org/1'); + $item1->setContentHtml('

This is the first item.

'); + $item1->setAuthor($item1Author); + $this->assertEquals('Author 1', $item1->getAuthor()->getName()); + $this->assertEquals($item1, $items[1]); + } + /** * @expectedException JDecool\JsonFeed\Exceptions\InvalidFeedException * @expectedExceptionMessage Invalid JSONFeed string diff --git a/test/Writer/Version1/RendererTest.php b/test/Writer/Version1/RendererTest.php index 4401c63..b3d47e7 100644 --- a/test/Writer/Version1/RendererTest.php +++ b/test/Writer/Version1/RendererTest.php @@ -93,6 +93,33 @@ public function testMicroblogFeed() $this->assertJsonStringEqualsJsonString($expected, $render->render($feed)); } + public function testAuthorsFeed() + { + $feedAuthor = new Author('Global Author'); + $feed = new Feed('My Example Feed'); + $feed->setFeedUrl('https://example.org/feed.json'); + $feed->setAuthor($feedAuthor); + + $item2Author = new Author('Author 2'); + $item2 = new Item('2'); + $item2->setUrl('https://example.org/2'); + $item2->setContentText('This is a second item.'); + $item2->setAuthor($item2Author); + $feed->addItem($item2); + + $item1Author = new Author('Author 1'); + $item1 = new Item('1'); + $item1->setUrl('https://example.org/1'); + $item1->setContentHtml('

This is the first item.

'); + $item1->setAuthor($item1Author); + $feed->addItem($item1); + + $expected = $this->getFixtures('authors'); + + $render = new Renderer(); + $this->assertJsonStringEqualsJsonString($expected, $render->render($feed)); + } + public function testRenderExtension() { $feed = new Feed('My Example Feed');