From 2630e06700b6ba7fa2421b5a9159704a53b55d0f Mon Sep 17 00:00:00 2001 From: Lee Driscoll Date: Sat, 13 May 2017 10:00:56 +0100 Subject: [PATCH] Pass parameter to get feed items() (#38) * pass parameter to getFeedItems() * refactor tests to include a filter being passed to a feed * refactor following pr feedback --- CHANGELOG.md | 3 +++ README.md | 2 +- src/Feed.php | 18 ++++++++++++++---- tests/DummyRepository.php | 17 +++++++++++++++++ tests/FeedTest.php | 2 +- tests/TestCase.php | 6 ++++++ ...t__all_feed_items_have_expected_data__3.xml | 17 +++++++++++++++++ ...can_render_all_feed_links_via_a_view__1.php | 1 + 8 files changed, 60 insertions(+), 6 deletions(-) create mode 100644 tests/__snapshots__/FeedTest__all_feed_items_have_expected_data__3.xml diff --git a/CHANGELOG.md b/CHANGELOG.md index 6e55824..97b6484 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ All notable changes to `laravel-feed` will be documented in this file +## 1.4.0 - 2017-05-12 +- allow a filter to be passed with items in config + ## 1.3.1 - 2017-05-12 - add a tag to publish views diff --git a/README.md b/README.md index daa500b..e9d2694 100644 --- a/README.md +++ b/README.md @@ -65,7 +65,7 @@ return [ /* * Here you can specify which class and method will return * the items that should appear in the feed. For example: - * '\App\Model@getAllFeedItems' + * '\App\Model@getAllFeedItems' or ['\App\Model@getAllFeedItems', 'filter'] */ 'items' => '', diff --git a/src/Feed.php b/src/Feed.php index 507d60c..8905398 100644 --- a/src/Feed.php +++ b/src/Feed.php @@ -14,8 +14,8 @@ class Feed public function __construct(array $feedConfiguration) { $this->feedConfiguration = $feedConfiguration; - if (! str_contains($feedConfiguration['items'], '@')) { - throw InvalidConfiguration::delimiterNotPresent($feedConfiguration['items']); + if (! str_contains($this->getFeedMethod(), '@')) { + throw InvalidConfiguration::delimiterNotPresent($this->getFeedMethod()); } } @@ -26,15 +26,25 @@ public function getFeedResponse() public function getFeedContent() { - list($class, $method) = explode('@', $this->feedConfiguration['items']); + list($class, $method) = explode('@', $this->getFeedMethod()); - $items = app($class)->{$method}(); + $items = app($class)->{$method}($this->getFeedArguments()); $meta = ['id' => url($this->feedConfiguration['url']), 'link' => url($this->feedConfiguration['url']), 'title' => $this->feedConfiguration['title'], 'updated' => $this->getLastUpdatedDate($items)]; return view('laravel-feed::feed', compact('meta', 'items'))->render(); } + protected function getFeedMethod() + { + return is_array($this->feedConfiguration['items']) ? $this->feedConfiguration['items'][0] : $this->feedConfiguration['items']; + } + + protected function getFeedArguments() + { + return is_array($this->feedConfiguration['items']) ? $this->feedConfiguration['items'][1] : null; + } + protected function getLastUpdatedDate(Collection $items) { if (! count($items)) { diff --git a/tests/DummyRepository.php b/tests/DummyRepository.php index 937556e..448859c 100644 --- a/tests/DummyRepository.php +++ b/tests/DummyRepository.php @@ -14,4 +14,21 @@ public function getAll() new DummyItem(), ]); } + + public function getAllWithArguments($filter = '') + { + if ($filter != '') { + return collect([ + new DummyItem(), + ]); + } + + return collect([ + new DummyItem(), + new DummyItem(), + new DummyItem(), + new DummyItem(), + new DummyItem(), + ]); + } } diff --git a/tests/FeedTest.php b/tests/FeedTest.php index 432ebc1..e6c9d6a 100644 --- a/tests/FeedTest.php +++ b/tests/FeedTest.php @@ -7,7 +7,7 @@ class FeedTest extends TestCase { - protected $feedNames = ['feed1', 'feed2']; + protected $feedNames = ['feed1', 'feed2', 'feed3']; /** @test */ public function all_feeds_are_available_on_their_registered_routes() diff --git a/tests/TestCase.php b/tests/TestCase.php index d7038f6..38aea34 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -39,6 +39,12 @@ protected function getEnvironmentSetUp($app) 'title' => 'Feed 2', 'description' => 'This is feed 2 from the unit tests', ], + [ + 'items' => ['Spatie\Feed\Test\DummyRepository@getAllWithArguments', 'filter'], + 'url' => '/feed3', + 'title' => 'Feed 3', + 'description' => 'This is feed 3 from the unit tests', + ], ]; $app['config']->set('laravel-feed.feeds', $feed); diff --git a/tests/__snapshots__/FeedTest__all_feed_items_have_expected_data__3.xml b/tests/__snapshots__/FeedTest__all_feed_items_have_expected_data__3.xml new file mode 100644 index 0000000..026407c --- /dev/null +++ b/tests/__snapshots__/FeedTest__all_feed_items_have_expected_data__3.xml @@ -0,0 +1,17 @@ + + + http://localhost/feedBaseUrl/feed3 + + <![CDATA[Feed 3]]> + 2016-01-01T00:00:00+01:00 + + <![CDATA[feedItemTitle]]> + + http://localhost/1 + + + + + 2016-01-01T00:00:00+01:00 + + diff --git a/tests/__snapshots__/FeedTest__it_can_render_all_feed_links_via_a_view__1.php b/tests/__snapshots__/FeedTest__it_can_render_all_feed_links_via_a_view__1.php index b1312a2..21f5ab3 100644 --- a/tests/__snapshots__/FeedTest__it_can_render_all_feed_links_via_a_view__1.php +++ b/tests/__snapshots__/FeedTest__it_can_render_all_feed_links_via_a_view__1.php @@ -2,4 +2,5 @@ return ' + ';