Skip to content
This repository was archived by the owner on Jan 31, 2025. It is now read-only.

Commit 2479963

Browse files
vintagesucksjdecool
authored andcommitted
allow rendering of item author (#9)
allow rendering of item author
1 parent 13b88cc commit 2479963

File tree

4 files changed

+89
-0
lines changed

4 files changed

+89
-0
lines changed

src/Writer/Version1/Renderer.php

+4
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,10 @@ private function renderItem(Item $item)
136136
}, $attachments);
137137
}
138138

139+
if ($author = $item->getAuthor()) {
140+
$result['author'] = $this->renderAuthor($author);
141+
}
142+
139143
if ($extensions = $item->getExtensions()) {
140144
foreach ($extensions as $key => $extension) {
141145
$result['_'.$key] = $extension;

test/Fixtures/authors.json

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"version": "https://jsonfeed.org/version/1",
3+
"title": "My Example Feed",
4+
"feed_url": "https://example.org/feed.json",
5+
"author": {
6+
"name": "Global Author"
7+
},
8+
"items": [
9+
{
10+
"id": "2",
11+
"content_text": "This is a second item.",
12+
"url": "https://example.org/2",
13+
"author": {
14+
"name": "Author 2"
15+
}
16+
},
17+
{
18+
"id": "1",
19+
"content_html": "<p>This is the first item.</p>",
20+
"url": "https://example.org/1",
21+
"author": {
22+
"name": "Author 1"
23+
}
24+
}
25+
]
26+
}

test/Reader/Version1/FeedReaderTest.php

+32
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use DateTime;
66
use JDecool\JsonFeed\Attachment;
7+
use JDecool\JsonFeed\Author;
78
use JDecool\JsonFeed\Item;
89
use JDecool\JsonFeed\Reader\Version1\FeedReader;
910
use PHPUnit\Framework\TestCase;
@@ -94,6 +95,37 @@ public function testMicroblogFeed()
9495
$this->assertEquals($item, $items[0]);
9596
}
9697

98+
public function testAuthorsFeed()
99+
{
100+
$input = $this->getFixtures('authors');
101+
$reader = FeedReader::create();
102+
103+
$feed = $reader->readFromJson($input);
104+
$this->assertInstanceOf('JDecool\JsonFeed\Feed', $feed);
105+
$this->assertEquals('My Example Feed', $feed->getTitle());
106+
$this->assertEquals('Global Author', $feed->getAuthor()->getName());
107+
$this->assertEquals('https://example.org/feed.json', $feed->getFeedUrl());
108+
109+
$items = $feed->getItems();
110+
$this->assertCount(2, $items);
111+
112+
$item2Author = new Author('Author 2');
113+
$item2 = new Item('2');
114+
$item2->setUrl('https://example.org/2');
115+
$item2->setContentText('This is a second item.');
116+
$item2->setAuthor($item2Author);
117+
$this->assertEquals('Author 2', $item2->getAuthor()->getName());
118+
$this->assertEquals($item2, $items[0]);
119+
120+
$item1Author = new Author('Author 1');
121+
$item1 = new Item('1');
122+
$item1->setUrl('https://example.org/1');
123+
$item1->setContentHtml('<p>This is the first item.</p>');
124+
$item1->setAuthor($item1Author);
125+
$this->assertEquals('Author 1', $item1->getAuthor()->getName());
126+
$this->assertEquals($item1, $items[1]);
127+
}
128+
97129
/**
98130
* @expectedException JDecool\JsonFeed\Exceptions\InvalidFeedException
99131
* @expectedExceptionMessage Invalid JSONFeed string

test/Writer/Version1/RendererTest.php

+27
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,33 @@ public function testMicroblogFeed()
9393
$this->assertJsonStringEqualsJsonString($expected, $render->render($feed));
9494
}
9595

96+
public function testAuthorsFeed()
97+
{
98+
$feedAuthor = new Author('Global Author');
99+
$feed = new Feed('My Example Feed');
100+
$feed->setFeedUrl('https://example.org/feed.json');
101+
$feed->setAuthor($feedAuthor);
102+
103+
$item2Author = new Author('Author 2');
104+
$item2 = new Item('2');
105+
$item2->setUrl('https://example.org/2');
106+
$item2->setContentText('This is a second item.');
107+
$item2->setAuthor($item2Author);
108+
$feed->addItem($item2);
109+
110+
$item1Author = new Author('Author 1');
111+
$item1 = new Item('1');
112+
$item1->setUrl('https://example.org/1');
113+
$item1->setContentHtml('<p>This is the first item.</p>');
114+
$item1->setAuthor($item1Author);
115+
$feed->addItem($item1);
116+
117+
$expected = $this->getFixtures('authors');
118+
119+
$render = new Renderer();
120+
$this->assertJsonStringEqualsJsonString($expected, $render->render($feed));
121+
}
122+
96123
public function testRenderExtension()
97124
{
98125
$feed = new Feed('My Example Feed');

0 commit comments

Comments
 (0)