forked from brendt/rfc-vote
-
Notifications
You must be signed in to change notification settings - Fork 0
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 brendt#241 from joshbonnick/patch-5
Extract `getContributors` to a support class
- Loading branch information
Showing
4 changed files
with
97 additions
and
31 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
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,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'] ?? []; | ||
} | ||
} |
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,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); | ||
} | ||
} |
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,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); | ||
} | ||
} |