Skip to content

Commit

Permalink
Merge pull request brendt#241 from joshbonnick/patch-5
Browse files Browse the repository at this point in the history
Extract `getContributors` to a support class
  • Loading branch information
brendt authored Aug 31, 2023
2 parents 1b34ec1 + 9b3e4a1 commit d96c7b9
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 31 deletions.
34 changes: 3 additions & 31 deletions app/Http/Controllers/AboutController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,18 @@

namespace App\Http\Controllers;

use App\Models\Contributor;
use App\Support\FetchContributors;
use Illuminate\Contracts\View\View;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\File;
use JsonException;

final readonly class AboutController
{
private const DAY = 60 * 60 * 24;

public function __invoke(): View
public function __invoke(FetchContributors $contributors): View
{
$contributors = Cache::remember('contributors', self::DAY, $this->getContributors(...));
$contributors = Cache::remember('contributors', now()->addDay(), $contributors->getContributors(...));

shuffle($contributors);

return view('about', compact('contributors'));
}

/**
* @return Contributor[]
*
* @throws JsonException
*/
private function getContributors(): array
{
/**
* @var array{
* contributors: array<int, array{
* id: int,
* name: string,
* url: string,
* contributions: array<int, string>,
* }>
* } $contributors
*/
$contributors = File::json(__DIR__.'/../../../contributors.json', JSON_THROW_ON_ERROR);

return collect($contributors['contributors'])
->map(fn (array $contributor) => new Contributor(...$contributor))
->all();
}
}
34 changes: 34 additions & 0 deletions app/Support/FetchContributors.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace App\Support;

use App\Models\Contributor;
use Illuminate\Support\Facades\File;

class FetchContributors
{
/**
* @return array<int, Contributor>
*/
public function getContributors(): array
{
return collect($this->getJson())
->map(fn (array $contributor) => new Contributor(...$contributor))
->all();
}

/**
* @return array<int, array{
* id: int,
* name: string,
* url: string,
* contributions: array<int, string>,
* }>
*/
public function getJson(): array
{
$contributors = File::json(base_path('contributors.json'), JSON_THROW_ON_ERROR);

return $contributors['contributors'] ?? [];
}
}
32 changes: 32 additions & 0 deletions tests/Feature/AboutTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Tests\Feature;

use App\Http\Controllers\AboutController;
use App\Models\Contributor;
use App\Support\FetchContributors;
use Mockery\MockInterface;
use Tests\TestCase;

class AboutTest extends TestCase
{
public function test_it_has_contributors(): void
{
$contributors = [
new Contributor(...[
'id' => 6905297,
'name' => 'Brent',
'url' => 'https://github.com/brendt',
'contributions' => ['Frontend', 'Backend'],
]),
];

$this->mock(FetchContributors::class, function (MockInterface $mock) use ($contributors) {
$mock->shouldReceive('getContributors')->andReturn($contributors);
});

$response = $this->get(action(AboutController::class));
$response->assertStatus(200);
$response->assertViewHas('contributors', $contributors);
}
}
28 changes: 28 additions & 0 deletions tests/Feature/Support/FetchContributorsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Tests\Feature\Support;

use App\Models\Contributor;
use App\Support\FetchContributors;
use Illuminate\Support\Arr;
use Tests\TestCase;

class FetchContributorsTest extends TestCase
{
public function test_no_duplicates_in_contributors_json()
{
$contributors = app(FetchContributors::class)->getJson();

$this->assertCount(0,
$duplicates = collect($contributors)->duplicates('id'),
sprintf('ID %s is in contributors.json more than once.', Arr::first($duplicates))
);
}

public function test_get_contributors_returns_an_array_of_contributor_models()
{
$contributors = app(FetchContributors::class)->getContributors();

$this->assertContainsOnlyInstancesOf(Contributor::class, $contributors);
}
}

0 comments on commit d96c7b9

Please sign in to comment.