diff --git a/src/FeedItem.php b/src/FeedItem.php index 80dfd66..aa84edf 100644 --- a/src/FeedItem.php +++ b/src/FeedItem.php @@ -78,6 +78,7 @@ public function build() */ public function toArray() { + return $this->build(); } diff --git a/src/Traits/ArrayHelpers.php b/src/Traits/ArrayHelpers.php index 03304ef..4d66923 100644 --- a/src/Traits/ArrayHelpers.php +++ b/src/Traits/ArrayHelpers.php @@ -12,7 +12,7 @@ trait ArrayHelpers */ protected function makeArray($items) { - return $items instanceof Collection ? $items->toArray() : $items; + return $items instanceof Collection ? $items->all() : $items; } /** diff --git a/tests/Fakes/DummyArrayableFeedItem.php b/tests/Fakes/DummyArrayableFeedItem.php new file mode 100644 index 0000000..3aed858 --- /dev/null +++ b/tests/Fakes/DummyArrayableFeedItem.php @@ -0,0 +1,14 @@ + 'bar']; + } +} diff --git a/tests/JsonFeedTest.php b/tests/JsonFeedTest.php index 4d52d30..cdb7dc8 100644 --- a/tests/JsonFeedTest.php +++ b/tests/JsonFeedTest.php @@ -3,6 +3,7 @@ use Illuminate\Support\Collection; use Mateusjatenee\JsonFeed\Exceptions\IncorrectFeedStructureException; use Mateusjatenee\JsonFeed\JsonFeed; +use Mateusjatenee\JsonFeed\Tests\Fakes\DummyArrayableFeedItem; use Mateusjatenee\JsonFeed\Tests\Fakes\DummyFeedItem; use Mateusjatenee\JsonFeed\Tests\TestCase; @@ -123,6 +124,17 @@ public function it_builds_a_complete_json_feed() $this->assertEquals($expected, json_decode($feed->toJson(), true)); } + /** @test */ + public function it_builds_a_complete_json_feed_from_an_arrayable_collection() + { + $feed = JsonFeed::start( + $config = $this->getJsonFeedConfig(), $items = collect($this->getArrayOfItems(true)) + ); + + $this->assertEquals($expected = $this->getExpectedJsonOutput($config, $items), $feed->toArray()); + $this->assertEquals($expected, json_decode($feed->toJson(), true)); + } + protected function getJsonFeedConfig() { return [ @@ -139,18 +151,18 @@ protected function getJsonFeedConfig() ]; } - protected function getArrayOfItems() + protected function getArrayOfItems($arrayable = false) { + $model = $arrayable ? DummyArrayableFeedItem::class : DummyFeedItem::class; + return [ - new DummyFeedItem, new DummyFeedItem, + new $model, new $model, ]; } protected function getExpectedJsonOutput($config, $items) { - $config = collect($config)->filter(function ($val, $key) { - return in_array($key, app('jsonFeed')->getAcceptedProperties()); - }); + $config = array_intersect_key($config, array_flip(app('jsonFeed')->getAcceptedProperties())); $items = array_map(function ($item) { return [ @@ -164,8 +176,8 @@ protected function getExpectedJsonOutput($config, $items) 'content_html' => $item->getFeedContentHtml(), 'content_text' => $item->getFeedContentText(), ]; - }, $items); + }, $items instanceof Collection ? $items->all() : $items); - return $config->put('version', app('jsonFeed')->getVersion())->put('items', $items)->toArray(); + return $config + ['version' => app('jsonFeed')->getVersion(), 'items' => $items]; } }