Skip to content
This repository has been archived by the owner on Nov 19, 2022. It is now read-only.

Commit

Permalink
Merge pull request #95 from davorminchorov/implement-the-about-me-res…
Browse files Browse the repository at this point in the history
…t-api-with-tests

[#27] Implement the About Me REST API with feature tests
  • Loading branch information
davorminchorov authored Nov 13, 2021
2 parents 229b834 + b871757 commit 850826c
Show file tree
Hide file tree
Showing 12 changed files with 277 additions and 3 deletions.
27 changes: 27 additions & 0 deletions app/AboutMe/Actions/GetAboutMeAction.php
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)();
}
}
26 changes: 26 additions & 0 deletions app/AboutMe/Api/V1/ApiResources/AboutMeJsonResource.php
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,
],
];
}
}
30 changes: 30 additions & 0 deletions app/AboutMe/Api/V1/Controllers/AboutMeController.php
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)()
);
}
}
28 changes: 28 additions & 0 deletions app/AboutMe/Factories/AboutMeFactory.php
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),
];
}
}
44 changes: 44 additions & 0 deletions app/AboutMe/Models/AboutMe.php
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',
];
}
31 changes: 31 additions & 0 deletions app/AboutMe/Queries/GetAboutMeQuery.php
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;
}
}
7 changes: 7 additions & 0 deletions app/AboutMe/Routes/api.php
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']);
37 changes: 37 additions & 0 deletions app/AboutMe/Tests/Feature/AboutMeTest.php
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();
}

}
5 changes: 2 additions & 3 deletions app/Blog/Models/BlogPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace DavorMinchorov\Blog\Models;

use DavorMinchorov\Blog\Factories\BlogPostFactory;
use DavorMinchorov\Blog\Factories\AboutMeFactory;
use DavorMinchorov\Blog\QueryBuilders\BlogPostQueryBuilder;
use Dyrynda\Database\Casts\EfficientUuid;
use Dyrynda\Database\Support\GeneratesUuid;
Expand All @@ -15,7 +15,7 @@
/**
* DavorMinchorov\Blog\Models\BlogPost
*
* @method static BlogPostFactory factory(...$parameters)
* @method static AboutMeFactory factory(...$parameters)
* @method static BlogPostQueryBuilder|BlogPost newModelQuery()
* @method static BlogPostQueryBuilder|BlogPost newQuery()
* @method static BlogPostQueryBuilder|BlogPost query()
Expand All @@ -25,7 +25,6 @@
* @method static BlogPostQueryBuilder|BlogPost whereUuid($uuid, $uuidColumn = null)
* @mixin Eloquent
* @property mixed $uuid
* @property mixed $user_uuid
* @property string $title
* @property string $slug
* @property string $excerpt
Expand Down
9 changes: 9 additions & 0 deletions config/modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@
'api_namespace' => 'DavorMinchorov\Contact\Api\V1\Controllers',
],

'aboutMe' => [
'name' => 'AboutMe',
'api_routes_path' => 'app/AboutMe/Routes/api.php',
'api_prefix' => 'v1/about-me',
'api_route_name_prefix' => 'v1.aboutMe',
'api_middleware' => ['api'],
'api_namespace' => 'DavorMinchorov\AboutMe\Api\V1\Controllers',
],

'blog' => [
'name' => 'Blog',
'api_routes_path' => 'app/Blog/Routes/api.php',
Expand Down
33 changes: 33 additions & 0 deletions database/migrations/2021_11_13_173212_create_about_me_table.php
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');
}
}
3 changes: 3 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
<testsuite name="BlogFeatureTests">
<directory suffix="Test.php">./app/Blog/Tests/Feature</directory>
</testsuite>
<testsuite name="AboutMeFeatureTests">
<directory suffix="Test.php">./app/AboutMe/Tests/Feature</directory>
</testsuite>
</testsuites>
<coverage processUncoveredFiles="true">
<include>
Expand Down

0 comments on commit 850826c

Please sign in to comment.