-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #233 from art-institute-of-chicago/feature/feature…
…d-tours-json Add featured tours to JSON
- Loading branch information
Showing
9 changed files
with
187 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
namespace App\Models\Transformers; | ||
|
||
use A17\Twill\Models\Contracts\TwillModelContract; | ||
use League\Fractal\TransformerAbstract; | ||
|
||
class FeaturedTourTransformer extends TransformerAbstract | ||
{ | ||
public function transform(TwillModelContract $tour) | ||
{ | ||
return [ | ||
(string) $tour->id, | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace App\Repositories\Serializers; | ||
|
||
/** | ||
* Due to legacy constraints, the dashboard serializer is essentially a wrapper | ||
* around the featured tour serializer with an additional unused | ||
* `featured_exhibitions` key. | ||
*/ | ||
class DashboardSerializer | ||
{ | ||
public function serialize($featuredTours) | ||
{ | ||
$featuredTourSerializer = new FeaturedTourSerializer(); | ||
return [ | ||
'dashboard' => | ||
array_merge( | ||
$featuredTourSerializer->serialize($featuredTours), | ||
['featured_exhibitions' => []], // Legacy from Drupal | ||
) | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
namespace App\Repositories\Serializers; | ||
|
||
use App\Models\Transformers\FeaturedTourTransformer; | ||
use League\Fractal\Manager; | ||
use League\Fractal\Resource; | ||
|
||
class FeaturedTourSerializer | ||
{ | ||
protected ?Manager $manager = null; | ||
|
||
public function __construct() | ||
{ | ||
$this->manager = new Manager(); | ||
$this->manager->setSerializer(new FlatArraySerializer()); | ||
} | ||
|
||
public function serialize($tours) | ||
{ | ||
$tours = collect($tours)->sortBy('position'); | ||
$resource = new Resource\Collection($tours, new FeaturedTourTransformer(), 'featured_tours'); | ||
return $this->manager->createData($resource)->toArray(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
namespace App\Repositories\Serializers; | ||
|
||
use League\Fractal\Serializer\ArraySerializer; | ||
|
||
class FlatArraySerializer extends ArraySerializer | ||
{ | ||
public function collection($resourceKey, array $data): array | ||
{ | ||
return [$resourceKey => collect($data)->flatten()]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace Tests\Feature; | ||
|
||
use App\Models\Tour; | ||
use App\Repositories\Serializers\DashboardSerializer; | ||
use Illuminate\Foundation\Testing\RefreshDatabase; | ||
use Tests\TestCase; | ||
|
||
class DashboardSerializerTest extends TestCase | ||
{ | ||
use RefreshDatabase; | ||
|
||
public function test_serialize(): void | ||
{ | ||
$serializer = new DashboardSerializer(); | ||
$featuredTours = Tour::factory(['featured' => true])->count(2)->create(); | ||
$serialized = $serializer->serialize($featuredTours); | ||
|
||
$this->assertArrayHasKey('dashboard', $serialized); | ||
$this->assertArrayHasKey('featured_tours', $serialized['dashboard']); | ||
$this->assertNotEmpty($serialized['dashboard']['featured_tours']); | ||
$this->assertArrayHasKey('featured_exhibitions', $serialized['dashboard']); | ||
$this->assertEmpty($serialized['dashboard']['featured_exhibitions']); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
namespace Tests\Feature; | ||
|
||
use App\Models\Tour; | ||
use App\Repositories\Serializers\FeaturedTourSerializer; | ||
use Illuminate\Database\Eloquent\Factories\Sequence; | ||
use Illuminate\Foundation\Testing\RefreshDatabase; | ||
use Tests\TestCase; | ||
|
||
class FeaturedTourSerializerTest extends TestCase | ||
{ | ||
use RefreshDatabase; | ||
|
||
public function test_serialize(): void | ||
{ | ||
$serializer = new FeaturedTourSerializer(); | ||
Tour::factory()->count(3)->sequence(['featured' => true], ['featured' => false])->create(); | ||
$featuredTours = Tour::featured()->get(); | ||
$serialized = $serializer->serialize($featuredTours); | ||
|
||
$this->assertArrayHasKey('featured_tours', $serialized); | ||
$this->assertCount($featuredTours->count(), $serialized['featured_tours']); | ||
foreach ($serialized['featured_tours'] as $id) { | ||
$this->assertIsString($id); | ||
$this->assertContains((int) $id, $featuredTours->pluck('id')); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
namespace Tests\Unit; | ||
|
||
use App\Repositories\Serializers\FlatArraySerializer; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class FlatArraySerializerTest extends TestCase | ||
{ | ||
public function test_collection_returns_a_flattened_array(): void | ||
{ | ||
$data = [ | ||
[ | ||
[ | ||
'123', | ||
], | ||
], | ||
[ | ||
[ | ||
'456', | ||
], | ||
], | ||
[ | ||
[ | ||
'789', | ||
], | ||
], | ||
]; | ||
$serializer = new FlatArraySerializer(); | ||
$collection = $serializer->collection('test', $data); | ||
$this->assertArrayHasKey('test', $collection, 'The collection is serialized with the correct key'); | ||
foreach ($data as $datum) { | ||
foreach ($datum as $record) { | ||
$id = current($record); | ||
$this->assertContains( | ||
$id, | ||
$collection['test'], | ||
'The serialized collection is flattened array of ids', | ||
); | ||
} | ||
} | ||
} | ||
} |