Skip to content

Commit

Permalink
Created second domain for Video
Browse files Browse the repository at this point in the history
  • Loading branch information
damian-nowak committed Jul 14, 2019
1 parent c521b2a commit c2d9a86
Show file tree
Hide file tree
Showing 10 changed files with 353 additions and 0 deletions.
11 changes: 11 additions & 0 deletions database/factories/VideoFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

/* @var $factory \Illuminate\Database\Eloquent\Factory */
use Domain\Videos\Video;
use Faker\Generator as Faker;

$factory->define(Video::class, function (Faker $faker) {
return [
'title' => ucfirst($faker->words(3, true))
];
});
32 changes: 32 additions & 0 deletions database/migrations/2019_07_14_124511_create_videos_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateVideosTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('videos', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('title');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('videos');
}
}
1 change: 1 addition & 0 deletions database/seeds/DatabaseSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ class DatabaseSeeder extends Seeder
public function run()
{
$this->call(ClientsTableSeeder::class);
$this->call(VideosTableSeeder::class);
}
}
17 changes: 17 additions & 0 deletions database/seeds/VideosTableSeeder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

use Domain\Videos\Video;
use Illuminate\Database\Seeder;

class VideosTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$videos = factory(Video::class, 5)->create();
}
}
69 changes: 69 additions & 0 deletions domain/Videos/Http/Controllers/VideoController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace Domain\Videos\Http\Controllers;

use App\Core\Http\Controllers\Controller;
use Domain\Videos\Video;
use Domain\Videos\Http\Requests\VideoUpdateRequest;
use Domain\Videos\Http\Requests\VideoCreateRequest;
use Illuminate\Http\JsonResponse;

class VideoController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
return Video::all();
}

/**
* Store a newly created resource in storage.
*
* @param VideoCreateRequest $request
* @return \Illuminate\Http\Response
*/
public function store(VideoCreateRequest $request)
{
return Video::create($request->all());
}

/**
* Display the specified resource.
*
* @param Video $video
* @return \Illuminate\Http\Response
*/
public function show(Video $video)
{
return $video;
}

/**
* Update the specified resource in storage.
*
* @param VideoUpdateRequest $request
* @param Video $video
* @return \Illuminate\Http\Response
*/
public function update(VideoUpdateRequest $request, Video $video)
{
$video->update($request->all());
return $video;
}

/**
* Remove the specified resource from storage.
*
* @param Video $video
* @return \Illuminate\Http\Response
*/
public function destroy(Video $video)
{
$video->delete();
return new JsonResponse('', 204);
}
}
30 changes: 30 additions & 0 deletions domain/Videos/Http/Requests/VideoCreateRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Domain\Videos\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class VideoCreateRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}

/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'title' => 'string'
];
}
}
30 changes: 30 additions & 0 deletions domain/Videos/Http/Requests/VideoUpdateRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Domain\Videos\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class VideoUpdateRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}

/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'title' => 'string'
];
}
}
17 changes: 17 additions & 0 deletions domain/Videos/Video.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Domain\Videos;

use Illuminate\Database\Eloquent\Model;

class Video extends Model
{
public $timestamps = true;

/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['title'];
}
7 changes: 7 additions & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use Domain\Clients\Http\Controllers\ClientController;
use Domain\Videos\Http\Controllers\VideoController;
use Illuminate\Http\Request;

/*
Expand All @@ -24,4 +25,10 @@
Route::post('clients', [ClientController::class, 'store']);
Route::put('clients/{client}', [ClientController::class, 'update']);
Route::delete('clients/{client}', [ClientController::class, 'destroy']);

Route::get('videos', [VideoController::class, 'index']);
Route::get('videos/{video}', [VideoController::class, 'show']);
Route::post('videos', [VideoController::class, 'store']);
Route::put('videos/{video}', [VideoController::class, 'update']);
Route::delete('videos/{video}', [VideoController::class, 'destroy']);
});
139 changes: 139 additions & 0 deletions tests/Feature/VideoTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
<?php

namespace Tests\Feature;

use Domain\Videos\Video;
use Tests\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Faker\Factory;


/**
* @group latest
*/
class VideoTest extends TestCase
{
use RefreshDatabase;

public function setUp(): void
{
parent::setUp();
$this->seed('DatabaseSeeder');
}

/**
* @test
*/
public function should_return_all_videos()
{
$response = $this->getJson('/api/v1/videos')->assertStatus(200);

$responseData = $response->json();

$this->assertArrayHasKey('id', $responseData[0]);
$this->assertSame(5, count($responseData));
}

/**
* @test
*/
public function should_return_needed_video()
{
$response = $this->getJson('/api/v1/videos/1')->assertStatus(200);
$video = Video::find(1);

$responseData = $response->json();

$this->assertArrayHasKey('id', $responseData);
$this->assertSame($video->title, $responseData['title']);
}

/**
* @test
*/
public function should_return_error_when_video_not_found()
{
$response = $this->getJson('/api/v1/videos/6')->assertStatus(404);
$responseData = $response->json();

$this->assertArrayHasKey('data', $responseData);
$this->assertSame("Resource not found", $responseData['data']);
}

/**
* @test
*/
public function should_delete_video()
{
$videosBefore = Video::all()->count();
$this->assertSame(5, $videosBefore);

$this->deleteJson('/api/v1/videos/1')->assertStatus(204);
$videosAfter = Video::all()->count();
$this->assertSame(4, $videosAfter);

$this->getJson('/api/v1/videos/1')->assertStatus(404);
}

/**
* @test
*/
public function should_not_delete_when_video_not_found()
{
$response = $this->deleteJson('/api/v1/videos/6')->assertStatus(404);
$responseData = $response->json();

$this->assertArrayHasKey('data', $responseData);
$this->assertSame("Resource not found", $responseData['data']);
}

/**
* @test
*/
public function should_update_video()
{
$faker = Factory::create();
$payload = [
'title' => ucfirst($faker->words(3, true))
];

$response = $this->putJson('/api/v1/videos/1', $payload)->assertStatus(200);
$responseData = $response->json();

$this->assertSame($payload['title'], $responseData['title']);
}

/**
* @test
*/
public function should_not_update_video_with_wrong_data()
{
$faker = Factory::create();
$payload = [
'title' => 1111
];

$response = $this->putJson('/api/v1/videos/1', $payload)->assertStatus(422);
$responseData = $response->json();

$this->assertArrayHasKey('message', $responseData);
$this->assertSame('The title must be a string.', $responseData['errors']['title'][0]);
}

/**
* @test
*/
public function should_create_video()
{
$videosCountBefore = Video::all()->count();
$faker = Factory::create();
$payload = [
'title' => ucfirst($faker->words(3, true))
];

$this->postJson('/api/v1/videos/', $payload)->assertStatus(201);
$videosCountAfter = Video::all()->count();

$this->assertGreaterThan($videosCountBefore, $videosCountAfter);
}
}

0 comments on commit c2d9a86

Please sign in to comment.