This repository has been archived by the owner on Nov 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
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 #95 from davorminchorov/implement-the-about-me-res…
…t-api-with-tests [#27] Implement the About Me REST API with feature tests
- Loading branch information
Showing
12 changed files
with
277 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,27 @@ | ||
<?php | ||
|
||
namespace DavorMinchorov\AboutMe\Actions; | ||
|
||
use DavorMinchorov\AboutMe\Models\AboutMe; | ||
use DavorMinchorov\AboutMe\Queries\GetAboutMeQuery; | ||
|
||
class GetAboutMeAction | ||
{ | ||
/** | ||
* GetAboutMeAction constructor. | ||
* | ||
* @param GetAboutMeQuery $getAboutMeQuery | ||
*/ | ||
public function __construct(private GetAboutMeQuery $getAboutMeQuery) | ||
{ | ||
// | ||
} | ||
|
||
/** | ||
* Gets a list of blog tags. | ||
*/ | ||
public function __invoke(): AboutMe | ||
{ | ||
return ($this->getAboutMeQuery)(); | ||
} | ||
} |
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 DavorMinchorov\AboutMe\Api\V1\ApiResources; | ||
|
||
use Illuminate\Http\Resources\Json\JsonResource; | ||
|
||
class AboutMeJsonResource extends JsonResource | ||
{ | ||
/** | ||
* Transform the resource into an array. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* | ||
* @return array | ||
*/ | ||
public function toArray($request): array | ||
{ | ||
return [ | ||
'id' => $this->resource->uuid, | ||
'type' => 'aboutMe', | ||
'attributes' => [ | ||
'content' => $this->resource->content, | ||
], | ||
]; | ||
} | ||
} |
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,30 @@ | ||
<?php | ||
|
||
namespace DavorMinchorov\AboutMe\Api\V1\Controllers; | ||
|
||
use DavorMinchorov\AboutMe\Actions\GetAboutMeAction; | ||
use DavorMinchorov\AboutMe\Api\V1\ApiResources\AboutMeJsonResource; | ||
use Illuminate\Http\Resources\Json\JsonResource; | ||
|
||
class AboutMeController | ||
{ | ||
/** | ||
* AboutMeController constructor. | ||
* | ||
* @param GetAboutMeAction $getAboutMeAction | ||
*/ | ||
public function __construct(private GetAboutMeAction $getAboutMeAction) | ||
{ | ||
// | ||
} | ||
|
||
/** | ||
* Gets the about me information for Davor Minchorov | ||
*/ | ||
public function __invoke(): JsonResource | ||
{ | ||
return AboutMeJsonResource::make( | ||
resource: ($this->getAboutMeAction)() | ||
); | ||
} | ||
} |
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 DavorMinchorov\AboutMe\Factories; | ||
|
||
use DavorMinchorov\AboutMe\Models\AboutMe; | ||
use Illuminate\Database\Eloquent\Factories\Factory; | ||
|
||
class AboutMeFactory extends Factory | ||
{ | ||
/** | ||
* The name of the factory's corresponding model. | ||
* | ||
* @var string|null | ||
*/ | ||
protected $model = AboutMe::class; | ||
|
||
/** | ||
* Define the model's default state. | ||
* | ||
* @return array | ||
*/ | ||
public function definition(): array | ||
{ | ||
return [ | ||
'content' => $this->faker->paragraphs(nb: 10, asText: true), | ||
]; | ||
} | ||
} |
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,44 @@ | ||
<?php | ||
|
||
namespace DavorMinchorov\AboutMe\Models; | ||
|
||
use Dyrynda\Database\Casts\EfficientUuid; | ||
use Dyrynda\Database\Support\GeneratesUuid; | ||
use Illuminate\Database\Eloquent\Factories\HasFactory; | ||
use Illuminate\Database\Eloquent\Model; | ||
|
||
/** | ||
* DavorMinchorov\AboutMe\Models\AboutMe | ||
* | ||
* @method static \DavorMinchorov\AboutMe\Factories\AboutMeFactory factory(...$parameters) | ||
* @method static \Illuminate\Database\Eloquent\Builder|AboutMe newModelQuery() | ||
* @method static \Illuminate\Database\Eloquent\Builder|AboutMe newQuery() | ||
* @method static \Illuminate\Database\Eloquent\Builder|AboutMe query() | ||
* @method static \Illuminate\Database\Eloquent\Builder|AboutMe whereUuid($uuid, $uuidColumn = null) | ||
* @mixin \Eloquent | ||
*/ | ||
class AboutMe extends Model | ||
{ | ||
use HasFactory, GeneratesUuid; | ||
|
||
protected $table = 'about_me'; | ||
|
||
/** | ||
* The attributes that should be cast to native types. | ||
* | ||
* @var array | ||
*/ | ||
protected $casts = [ | ||
'uuid' => EfficientUuid::class, | ||
]; | ||
|
||
/** | ||
* The attributes that are mass assignable. | ||
* | ||
* @var array<string> | ||
*/ | ||
protected $fillable = [ | ||
'uuid', | ||
'content', | ||
]; | ||
} |
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,31 @@ | ||
<?php | ||
|
||
namespace DavorMinchorov\AboutMe\Queries; | ||
|
||
use DavorMinchorov\AboutMe\Models\AboutMe; | ||
|
||
class GetAboutMeQuery | ||
{ | ||
/** | ||
* GetAboutMeQuery constructor. | ||
* | ||
* @param AboutMe $aboutMe | ||
*/ | ||
public function __construct(private AboutMe $aboutMe) | ||
{ | ||
|
||
} | ||
|
||
/** | ||
* Gets the about me information about Davor Minchorov. | ||
* | ||
* @return AboutMe | ||
*/ | ||
public function __invoke(): AboutMe | ||
{ | ||
$aboutMe = $this->aboutMe->newQuery()->first(); | ||
|
||
/** @var AboutMe|null $aboutMe */ | ||
return $aboutMe ?? $this->aboutMe; | ||
} | ||
} |
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,7 @@ | ||
<?php | ||
|
||
/** @var \Illuminate\Routing\Router $router */ | ||
|
||
use DavorMinchorov\AboutMe\Api\V1\Controllers\AboutMeController; | ||
|
||
$router->get(uri: '/', action: [AboutMeController::class, '__invoke']); |
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,37 @@ | ||
<?php | ||
|
||
namespace DavorMinchorov\AboutMe\Tests\Feature; | ||
|
||
use DavorMinchorov\AboutMe\Models\AboutMe; | ||
use DavorMinchorov\Framework\Tests\TestCase; | ||
use Illuminate\Foundation\Testing\RefreshDatabase; | ||
|
||
class AboutMeTest extends TestCase | ||
{ | ||
use RefreshDatabase; | ||
|
||
private string $aboutMeRouteName = 'v1.aboutMe'; | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function it_shows_the_about_me_information(): void | ||
{ | ||
$aboutMe = AboutMe::factory()->create(); | ||
|
||
$response = $this->getJson(route($this->aboutMeRouteName)); | ||
|
||
$response->assertExactJson([ | ||
'data' => [ | ||
'id' => $aboutMe->uuid, | ||
'type' => 'aboutMe', | ||
'attributes' => [ | ||
'content' => $aboutMe->content, | ||
], | ||
], | ||
]); | ||
|
||
$response->assertOk(); | ||
} | ||
|
||
} |
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
33 changes: 33 additions & 0 deletions
33
database/migrations/2021_11_13_173212_create_about_me_table.php
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,33 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
class CreateAboutMeTable extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function up(): void | ||
{ | ||
Schema::create(table: 'about_me', callback: function (Blueprint $table) { | ||
$table->id(); | ||
$table->efficientUuid(column: 'uuid'); | ||
$table->text(column: 'content'); | ||
$table->timestamps(); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function down(): void | ||
{ | ||
Schema::dropIfExists(table: 'about_me'); | ||
} | ||
} |
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