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

Commit

Permalink
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
12 changes: 4 additions & 8 deletions src/Strategy/FeedStrategy.php
Expand Up @@ -85,25 +85,20 @@ public function selectRenderer(ViewEvent $e)
{
$model = $e->getModel();

if ($model instanceof Model\FeedModel) {
// FeedModel found
return $this->renderer;
}

$request = $e->getRequest();
if (!$request instanceof HttpRequest) {
// Not an HTTP request; cannot autodetermine
return;
return ($model instanceof Model\FeedModel) ? $this->renderer : null;
}

$headers = $request->getHeaders();
if (!$headers->has('accept')) {
return;
return ($model instanceof Model\FeedModel) ? $this->renderer : null;
}

$accept = $headers->get('accept');
if (($match = $accept->match('application/rss+xml, application/atom+xml')) == false) {
return;
return ($model instanceof Model\FeedModel) ? $this->renderer : null;
}

if ($match->getTypeString() == 'application/rss+xml') {
Expand All @@ -116,6 +111,7 @@ public function selectRenderer(ViewEvent $e)
return $this->renderer;
}

return ($model instanceof Model\FeedModel) ? $this->renderer : null;
}

/**
Expand Down
21 changes: 21 additions & 0 deletions test/Strategy/FeedStrategyTest.php
Expand Up @@ -62,6 +62,27 @@ public function testAtomAcceptHeaderSelectsFeedStrategy()
$this->assertSame($this->renderer, $result);
}

public function testViewModelMatchedAcceptHeaderMatchSelectsFeedStrategy()
{
$this->event->setModel(new FeedModel());
$request = new HttpRequest();
$request->getHeaders()->addHeaderLine('Accept', '*/*');
$this->event->setRequest($request);
$result = $this->strategy->selectRenderer($this->event);
$this->assertSame($this->renderer, $result);
}

public function testViewModelAcceptHeaderSelectsFeedStrategyAndSetsFeedtype()
{
$this->event->setModel(new FeedModel());
$request = new HttpRequest();
$request->getHeaders()->addHeaderLine('Accept', 'application/atom+xml');
$this->event->setRequest($request);
$result = $this->strategy->selectRenderer($this->event);
$this->assertSame($this->renderer, $result);
$this->assertSame('atom', $result->getFeedType());
}

public function testLackOfFeedModelOrAcceptHeaderDoesNotSelectFeedStrategy()
{
$result = $this->strategy->selectRenderer($this->event);
Expand Down

0 comments on commit a41bbf0

Please sign in to comment.