Skip to content
This repository was archived by the owner on Jun 29, 2021. It is now read-only.

Commit 3ce3905

Browse files
author
José Postiga
committed
feat(Tags): add tags' index endpoint
1 parent adfe721 commit 3ce3905

File tree

10 files changed

+177
-0
lines changed

10 files changed

+177
-0
lines changed

config/app.php

+4
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,10 @@
175175
App\Providers\EventServiceProvider::class,
176176
App\Providers\RouteServiceProvider::class,
177177

178+
/*
179+
* Domains Service Providers...
180+
*/
181+
\Domains\Tags\TagsServiceProvider::class,
178182
],
179183

180184
/*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Domains\Tags\Controllers;
4+
5+
use App\Http\Controllers\Controller;
6+
use Domains\Tags\Models\Tag;
7+
use Domains\Tags\Resources\TagResource;
8+
9+
class TagsIndexController extends Controller
10+
{
11+
public function __invoke(Tag $tags)
12+
{
13+
return TagResource::collection($tags->all());
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
use Carbon\Carbon;
4+
use Domains\Tags\Models\Tag;
5+
6+
/** @var \Illuminate\Database\Eloquent\Factory $factory */
7+
$factory->define(Tag::class, static fn (\Faker\Generator $faker) => [
8+
'name' => $faker->name,
9+
'created_at' => Carbon::now(),
10+
]);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
class AddTagsTable extends Migration
8+
{
9+
public function up()
10+
{
11+
Schema::create('tags', static function (Blueprint $table) {
12+
$table->id();
13+
$table->string('name');
14+
$table->timestamps();
15+
$table->softDeletes();
16+
});
17+
}
18+
}

domains/Tags/Models/Tag.php

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Domains\Tags\Models;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
use Illuminate\Database\Eloquent\SoftDeletes;
7+
8+
class Tag extends Model
9+
{
10+
use SoftDeletes;
11+
}
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Domains\Tags\Resources;
4+
5+
use Illuminate\Http\Resources\Json\JsonResource;
6+
7+
class TagResource extends JsonResource
8+
{
9+
public function toArray($request): array
10+
{
11+
return [
12+
'id' => $this->id,
13+
'name' => $this->name,
14+
'created_at' => $this->created_at,
15+
'updated_at' => $this->updated_at,
16+
'deleted_at' => $this->deleted_at,
17+
];
18+
}
19+
}

domains/Tags/Routes/api.php

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
use Domains\Tags\Controllers\TagsIndexController;
4+
5+
Route::get('/tags', TagsIndexController::class)->name('tags.index');

domains/Tags/TagsServiceProvider.php

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Domains\Tags;
4+
5+
use Illuminate\Support\ServiceProvider;
6+
7+
class TagsServiceProvider extends ServiceProvider
8+
{
9+
public function boot(): void
10+
{
11+
$this->loadFactoriesFrom(__DIR__ . '/Database/Factories');
12+
13+
$this->loadMigrationsFrom(__DIR__ . '/Database/Migrations');
14+
15+
$this->loadRoutesFrom(__DIR__ . '/Routes/api.php');
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace Domains\Tags\Tests\Features;
4+
5+
use Domains\Tags\Models\Tag;
6+
use Illuminate\Foundation\Testing\DatabaseMigrations;
7+
use Tests\TestCase;
8+
9+
class TagsIndexTest extends TestCase
10+
{
11+
use DatabaseMigrations;
12+
13+
protected function setUp(): void
14+
{
15+
parent::setUp();
16+
17+
factory(Tag::class, 2)->create();
18+
}
19+
20+
/** @test */
21+
public function it_lists_all_resources(): void
22+
{
23+
$response = $this->get('/tags')
24+
->assertSuccessful()
25+
->assertJsonStructure([
26+
'data' => [
27+
[
28+
'id', 'name', 'created_at',
29+
],
30+
],
31+
])
32+
->decodeResponseJson();
33+
34+
$this->assertCount(2, $response['data']);
35+
}
36+
}
+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace Domains\Tags\Tests\Unit;
4+
5+
use Carbon\Carbon;
6+
use Domains\Tags\Models\Tag;
7+
use Illuminate\Database\Eloquent\SoftDeletingScope;
8+
use Tests\TestCase;
9+
10+
class TagModelTest extends TestCase
11+
{
12+
private Tag $model;
13+
14+
protected function setUp(): void
15+
{
16+
parent::setUp();
17+
18+
$this->model = factory(Tag::class)->make();
19+
}
20+
21+
/** @test */
22+
public function it_contains_required_properties(): void
23+
{
24+
$this->assertNotNull($this->model->name);
25+
$this->assertIsString($this->model->name);
26+
27+
$this->assertNotNull($this->model->created_at);
28+
$this->assertInstanceOf(Carbon::class, $this->model->created_at);
29+
}
30+
31+
/** @test */
32+
public function it_uses_correct_table_name(): void
33+
{
34+
$this->assertEquals('tags', $this->model->getTable());
35+
}
36+
37+
/** @test */
38+
public function it_uses_soft_deletes(): void
39+
{
40+
$this->assertArrayHasKey(SoftDeletingScope::class, $this->model->getGlobalScopes());
41+
}
42+
}

0 commit comments

Comments
 (0)