Skip to content

Commit

Permalink
fix bug where collections where not being correctly transformed
Browse files Browse the repository at this point in the history
  • Loading branch information
mateusjatenee committed May 23, 2017
1 parent 19ae6b2 commit f8c1781
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/FeedItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public function build()
*/
public function toArray()
{

return $this->build();
}

Expand Down
2 changes: 1 addition & 1 deletion src/Traits/ArrayHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ trait ArrayHelpers
*/
protected function makeArray($items)
{
return $items instanceof Collection ? $items->toArray() : $items;
return $items instanceof Collection ? $items->all() : $items;
}

/**
Expand Down
14 changes: 14 additions & 0 deletions tests/Fakes/DummyArrayableFeedItem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Mateusjatenee\JsonFeed\Tests\Fakes;

use Illuminate\Contracts\Support\Arrayable;
use Mateusjatenee\JsonFeed\Tests\Fakes\DummyFeedItem;

class DummyArrayableFeedItem extends DummyFeedItem implements Arrayable
{
public function toArray()
{
return ['foo' => 'bar'];
}
}
26 changes: 19 additions & 7 deletions tests/JsonFeedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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 [
Expand All @@ -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 [
Expand All @@ -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];
}
}

0 comments on commit f8c1781

Please sign in to comment.