Skip to content

Commit

Permalink
allow rendering of item author (#9)
Browse files Browse the repository at this point in the history
allow rendering of item author
  • Loading branch information
vintagesucks authored and jdecool committed Aug 4, 2017
1 parent 13b88cc commit 2479963
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Writer/Version1/Renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
26 changes: 26 additions & 0 deletions test/Fixtures/authors.json
Original file line number Diff line number Diff line change
@@ -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": "<p>This is the first item.</p>",
"url": "https://example.org/1",
"author": {
"name": "Author 1"
}
}
]
}
32 changes: 32 additions & 0 deletions test/Reader/Version1/FeedReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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('<p>This is the first item.</p>');
$item1->setAuthor($item1Author);
$this->assertEquals('Author 1', $item1->getAuthor()->getName());
$this->assertEquals($item1, $items[1]);
}

/**
* @expectedException JDecool\JsonFeed\Exceptions\InvalidFeedException
* @expectedExceptionMessage Invalid JSONFeed string
Expand Down
27 changes: 27 additions & 0 deletions test/Writer/Version1/RendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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('<p>This is the first item.</p>');
$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');
Expand Down

0 comments on commit 2479963

Please sign in to comment.